diff --git a/tekton/ci-workspace/jobs/kustomization.yaml b/tekton/ci-workspace/jobs/kustomization.yaml index 05bbf0893..8f5efb0a2 100644 --- a/tekton/ci-workspace/jobs/kustomization.yaml +++ b/tekton/ci-workspace/jobs/kustomization.yaml @@ -7,4 +7,5 @@ resources: - tekton-org-validation.yaml - tekton-python-unit-tests.yaml - tekton-catalog-diff-task.yaml -- e2e-kind.yaml \ No newline at end of file +- e2e-kind.yaml +- tekton-kind-label.yaml diff --git a/tekton/ci-workspace/jobs/tekton-kind-label.yaml b/tekton/ci-workspace/jobs/tekton-kind-label.yaml new file mode 100644 index 000000000..1099d642f --- /dev/null +++ b/tekton/ci-workspace/jobs/tekton-kind-label.yaml @@ -0,0 +1,98 @@ +apiVersion: tekton.dev/v1beta1 +kind: Task +metadata: + name: kind-label + namespace: tektonci + description: | + Verifies that a PR has one valid kind label +spec: + params: + - name: labels + description: The labels attached to the Pull Request + volumes: + - name: label-config-v2 + configMap: + name: label-config-v2 + stepTemplate: + env: + - name: HOME + value: /tekton/home + steps: + - name: install-pyyaml + image: python:3-alpine + script: | + pip install pyyaml --user + - name: check-labels + image: python:3-alpine + volumeMounts: + - name: label-config-v2 + mountPath: /etc/config + script: | + #!/usr/bin/env python + + import json + import yaml + import sys + + prLabelsText = """$(params.labels)""" + prLabels = json.loads(prLabelsText) + + availableLabels = None + with open("/etc/config/labels.yaml", "r") as stream: + availableLabels = yaml.safe_load(stream)["default"]["labels"] + + availableKindLabels = {x.get("name"):x.get("description") for x in availableLabels if x.get("name").startswith("kind/")} + foundKindLabels = set([x.get("name") for x in prLabels if x.get("name").startswith("kind/") and x.get("name")]) + validKindLabels = set([x for x in foundKindLabels if x in availableKindLabels]) + + # Check that we have one and only one kind label + foundLabels = len(validKindLabels) + if (foundLabels > 1 or foundLabels == 0): + msg = "Error: {} valid \"kind/*\" labels found".format(foundLabels) + if foundLabels > 1: + msg += "({})".format(validKindLabels) + msg += ", expecting exactly one." + invalidKindLabels = foundKindLabels - validKindLabels + if len(invalidKindLabels) > 0: + msg += " Invalid labels found: {}".format(invalidKindLabels) + print(msg) + print("\nAvailable \"kind/*\" labels are:") + for label, description in availableKindLabels.items(): + print("\t{}: {}".format(label, description)) + + # Check failed. Return exit code 1. + sys.exit(1) + else: + print("Exactly one \"kind/*\" label found: {}".format(validKindLabels)) +--- +apiVersion: tekton.dev/v1beta1 +kind: Pipeline +metadata: + name: tekton-kind-label +spec: + params: + - name: checkName + description: The name of the GitHub check that this pipeline is used for + - name: gitHubCommand + description: The command that was used to trigger testing + - name: labels + description: The labels attached to the Pull Request + tasks: + - name: check-name-matches + taskRef: + name: check-name-matches + params: + - name: gitHubCommand + value: $(params.gitHubCommand) + - name: checkName + value: $(params.checkName) + - name: kind-label + when: # implicit dependency on the check tasks + - input: $(tasks.check-name-matches.results.check) + operator: in + values: ["passed"] + taskRef: + name: "kind-label" + params: + - name: labels + value: $(params.labels) diff --git a/tekton/ci-workspace/shared/template.yaml b/tekton/ci-workspace/shared/template.yaml index d1ff1e1d3..bd70e635f 100644 --- a/tekton/ci-workspace/shared/template.yaml +++ b/tekton/ci-workspace/shared/template.yaml @@ -71,4 +71,58 @@ spec: # Start with docs, ends with .md or *.rst, but not in vendor/ or thirdparty/ value: "(^docs\\/.*|^(?!vendor\\/)(?!thirdparty\\/).*\\.(md|rst)$)" - name: package - value: $(tt.params.package) \ No newline at end of file + value: $(tt.params.package) +--- +apiVersion: triggers.tekton.dev/v1alpha1 +kind: TriggerTemplate +metadata: + name: label-check + annotations: + triggers.tekton.dev/old-escape-quotes: "true" +spec: + params: + - name: buildUUID + description: UUID used to track a CI Pipeline Run in logs + - name: package + description: org/repo + - name: pullRequestNumber + description: The pullRequestNumber + - name: pullRequestUrl + description: The HTML URL for the pull request + - name: gitRepository + description: The git repository that hosts context and Dockerfile + - name: gitRevision + description: The Git revision to be used. + - name: gitCloneDepth + description: Number of commits in the change + 1 + - name: gitHubCommand + description: | + The GitHub command that was used a trigger. This is only available when + this template is triggered by a comment. The default value is for the + case of a pull_request event. + default: "" + - name: labels + description: List of labels currently on the Pull Request + resourcetemplates: + - apiVersion: tekton.dev/v1beta1 + kind: PipelineRun + metadata: + generateName: check-pr-labels- + labels: + prow.k8s.io/build-id: $(tt.params.buildUUID) + tekton.dev/check-name: check-pr-has-kind-label + tekton.dev/kind: ci + tekton.dev/pr-number: $(tt.params.pullRequestNumber) + annotations: + tekton.dev/gitRevision: "$(tt.params.gitRevision)" + tekton.dev/gitURL: "$(tt.params.gitRepository)" + spec: + pipelineRef: + name: tekton-kind-label + params: + - name: labels + value: $(tt.params.labels) + - name: checkName + value: check-pr-has-kind-label + - name: gitHubCommand + value: $(tt.params.gitHubCommand) \ No newline at end of file diff --git a/tekton/ci-workspace/shared/trigger.yaml b/tekton/ci-workspace/shared/trigger.yaml index 5cd14d30f..b674931e4 100644 --- a/tekton/ci-workspace/shared/trigger.yaml +++ b/tekton/ci-workspace/shared/trigger.yaml @@ -22,4 +22,69 @@ spec: - ref: tekton-ci-webhook-pr-labels - ref: tekton-ci-clone-depth template: - ref: tekton-doc-reviews \ No newline at end of file + ref: tekton-doc-reviews +--- +apiVersion: triggers.tekton.dev/v1alpha1 +kind: Trigger +metadata: + name: pull-request-label-check +spec: + interceptors: + - github: + secretRef: + secretName: ci-webhook + secretKey: secret + eventTypes: + - pull_request + - cel: + filter: >- + body.repository.full_name.startsWith('tektoncd/') && + body.repository.name in ['plumbing', 'pipeline', 'triggers', 'cli', 'dashboard', 'hub'] && + body.action in ['opened', 'synchronize', 'labeled', 'unlabeled'] + bindings: + - ref: tekton-ci-github-base + - ref: tekton-ci-webhook-pull-request + - ref: tekton-ci-webhook-pr-labels + template: + ref: label-check +--- +apiVersion: triggers.tekton.dev/v1alpha1 +kind: Trigger +metadata: + name: comment-label-check +spec: + interceptors: + - github: + secretRef: + secretName: ci-webhook + secretKey: secret + eventTypes: + - pull_request + - cel: + filter: >- + body.repository.full_name.startsWith('tektoncd/') && + body.repository.name in ['plumbing', 'pipeline', 'triggers', 'cli', 'dashboard', 'hub'] && + body.action == 'created' && + 'pull_request' in body.issue && + body.issue.state == 'open' && + body.comment.body.matches('^/test($| [^ ]*[ ]*$)') + overlays: + - key: add_pr_body.pull_request_url + expression: "body.issue.pull_request.url" + - webhook: + objectRef: + kind: Service + name: add-pr-body + apiVersion: v1 + namespace: tektonci + - cel: + overlays: + - key: git_clone_depth + expression: "string(body.extensions.add_pr_body.pull_request_body.commits + 1.0)" + bindings: + - ref: tekton-ci-github-base + - ref: tekton-ci-webhook-comment + - ref: tekton-ci-clone-depth + - ref: tekton-ci-webhook-issue-labels + template: + ref: label-check \ No newline at end of file diff --git a/tekton/ci/eventlistener.yaml b/tekton/ci/eventlistener.yaml index a93587a29..c1add6e5a 100644 --- a/tekton/ci/eventlistener.yaml +++ b/tekton/ci/eventlistener.yaml @@ -64,64 +64,6 @@ spec: - ref: tekton-ci-webhook-issue-labels template: ref: tekton-plumbing-ci-pipeline - - name: all-comment-ci - interceptors: - - github: - secretRef: - secretName: ci-webhook - secretKey: secret - eventTypes: - - issue_comment - - cel: - filter: >- - body.repository.full_name.startsWith('tektoncd/') && - body.repository.name in ['plumbing', 'pipeline', 'triggers', 'cli', 'dashboard', 'hub'] && - body.action == 'created' && - 'pull_request' in body.issue && - body.issue.state == 'open' && - body.comment.body.matches('^/test($| [^ ]*[ ]*$)') - overlays: - - key: add_pr_body.pull_request_url - expression: "body.issue.pull_request.url" - - webhook: - objectRef: - kind: Service - name: add-pr-body - apiVersion: v1 - namespace: tektonci - - cel: - overlays: - - key: git_clone_depth - expression: "string(body.extensions.add_pr_body.pull_request_body.commits + 1.0)" - bindings: - - ref: tekton-ci-github-base - - ref: tekton-ci-webhook-comment - - ref: tekton-ci-clone-depth - - ref: tekton-ci-webhook-issue-labels - template: - ref: tekton-all-ci-pipeline - - name: all-pull-request-ci - interceptors: - - github: - secretRef: - secretName: ci-webhook - secretKey: secret - eventTypes: - - pull_request - - cel: - filter: >- - body.repository.full_name.startsWith('tektoncd/') && - body.repository.name in ['plumbing', 'pipeline', 'triggers', 'cli', 'dashboard', 'hub'] && - (body.action == 'opened' || - body.action == 'synchronize' || - body.action == 'labeled' || - body.action == 'unlabeled') - bindings: - - ref: tekton-ci-github-base - - ref: tekton-ci-webhook-pull-request - - ref: tekton-ci-webhook-pr-labels - template: - ref: tekton-all-ci-pipeline - name: cd-trigger interceptors: - github: @@ -155,61 +97,4 @@ spec: - ref: tekton-ci-github-base - ref: tekton-ci-webhook-tektoncd-comment template: - ref: tekton-cd-triggers - - name: website-pull-request-ci - interceptors: - - github: - secretRef: - secretName: ci-webhook - secretKey: secret - eventTypes: - - pull_request - - cel: - filter: >- - body.repository.full_name == 'tektoncd/website' && - (body.action == 'opened' || body.action == 'synchronize') - overlays: - - key: git_clone_depth - expression: "string(body.pull_request.commits + 1.0)" - bindings: - - ref: tekton-ci-github-base - - ref: tekton-ci-webhook-pull-request - - ref: tekton-ci-clone-depth - - ref: tekton-ci-webhook-pr-labels - template: - ref: tekton-website-ci-pipeline - - name: website-comment-ci - interceptors: - - github: - secretRef: - secretName: ci-webhook - secretKey: secret - eventTypes: - - issue_comment - - cel: - filter: >- - body.repository.full_name == 'tektoncd/website' && - body.action == 'created' && - 'pull_request' in body.issue && - body.issue.state == 'open' && - body.comment.body.matches('^/test($| [^ ]*[ ]*$)') - overlays: - - key: add_pr_body.pull_request_url - expression: "body.issue.pull_request.url" - - webhook: - objectRef: - kind: Service - name: add-pr-body - apiVersion: v1 - namespace: tektonci - - cel: - overlays: - - key: git_clone_depth - expression: "string(body.extensions.add_pr_body.pull_request_body.commits + 1.0)" - bindings: - - ref: tekton-ci-github-base - - ref: tekton-ci-webhook-comment - - ref: tekton-ci-clone-depth - - ref: tekton-ci-webhook-issue-labels - template: - ref: tekton-website-ci-pipeline \ No newline at end of file + ref: tekton-cd-triggers \ No newline at end of file diff --git a/tekton/ci/jobs/tasks.yaml b/tekton/ci/jobs/tasks.yaml index c73ac2363..0f7af4e76 100644 --- a/tekton/ci/jobs/tasks.yaml +++ b/tekton/ci/jobs/tasks.yaml @@ -65,70 +65,3 @@ spec: #!/bin/sh set -ex yamllint $(params.folders) ---- -apiVersion: tekton.dev/v1beta1 -kind: Task -metadata: - name: kind-label - namespace: tektonci - description: | - Verifies that a PR has one valid kind label -spec: - params: - - name: labels - description: The labels attached to the Pull Request - volumes: - - name: label-config-v2 - configMap: - name: label-config-v2 - stepTemplate: - env: - - name: HOME - value: /tekton/home - steps: - - name: install-pyyaml - image: python:3-alpine - script: | - pip install pyyaml --user - - name: check-labels - image: python:3-alpine - volumeMounts: - - name: label-config-v2 - mountPath: /etc/config - script: | - #!/usr/bin/env python - - import json - import yaml - import sys - - prLabelsText = """$(params.labels)""" - prLabels = json.loads(prLabelsText) - - availableLabels = None - with open("/etc/config/labels.yaml", "r") as stream: - availableLabels = yaml.safe_load(stream)["default"]["labels"] - - availableKindLabels = {x.get("name"):x.get("description") for x in availableLabels if x.get("name").startswith("kind/")} - foundKindLabels = set([x.get("name") for x in prLabels if x.get("name").startswith("kind/") and x.get("name")]) - validKindLabels = set([x for x in foundKindLabels if x in availableKindLabels]) - - # Check that we have one and only one kind label - foundLabels = len(validKindLabels) - if (foundLabels > 1 or foundLabels == 0): - msg = "Error: {} valid \"kind/*\" labels found".format(foundLabels) - if foundLabels > 1: - msg += "({})".format(validKindLabels) - msg += ", expecting exactly one." - invalidKindLabels = foundKindLabels - validKindLabels - if len(invalidKindLabels) > 0: - msg += " Invalid labels found: {}".format(invalidKindLabels) - print(msg) - print("\nAvailable \"kind/*\" labels are:") - for label, description in availableKindLabels.items(): - print("\t{}: {}".format(label, description)) - - # Check failed. Return exit code 1. - sys.exit(1) - else: - print("Exactly one \"kind/*\" label found: {}".format(validKindLabels)) diff --git a/tekton/ci/jobs/tekton-kind-label.yaml b/tekton/ci/jobs/tekton-kind-label.yaml deleted file mode 100644 index 2017c7c83..000000000 --- a/tekton/ci/jobs/tekton-kind-label.yaml +++ /dev/null @@ -1,27 +0,0 @@ -apiVersion: tekton.dev/v1beta1 -kind: Pipeline -metadata: - name: tekton-kind-label - namespace: tektonci -spec: - params: - - name: checkName - description: The name of the GitHub check that this pipeline is used for - - name: gitHubCommand - description: The command that was used to trigger testing - - name: labels - description: The labels attached to the Pull Request - tasks: - - name: kind-label - conditions: - - conditionRef: "check-name-matches" - params: - - name: gitHubCommand - value: $(params.gitHubCommand) - - name: checkName - value: $(params.checkName) - taskRef: - name: kind-label - params: - - name: labels - value: $(params.labels) diff --git a/tekton/ci/templates/all-template.yaml b/tekton/ci/templates/all-template.yaml deleted file mode 100644 index 7ee98d003..000000000 --- a/tekton/ci/templates/all-template.yaml +++ /dev/null @@ -1,54 +0,0 @@ -apiVersion: triggers.tekton.dev/v1alpha1 -kind: TriggerTemplate -metadata: - name: tekton-all-ci-pipeline - namespace: tektonci - annotations: - triggers.tekton.dev/old-escape-quotes: "true" -spec: - params: - - name: buildUUID - description: UUID used to track a CI Pipeline Run in logs - - name: package - description: org/repo - - name: pullRequestNumber - description: The pullRequestNumber - - name: pullRequestUrl - description: The HTML URL for the pull request - - name: gitRepository - description: The git repository that hosts context and Dockerfile - - name: gitRevision - description: The Git revision to be used. - - name: gitCloneDepth - description: Number of commits in the change + 1 - - name: gitHubCommand - description: | - The GitHub command that was used a trigger. This is only available when - this template is triggered by a comment. The default value is for the - case of a pull_request event. - default: "" - - name: labels - description: List of labels currently on the Pull Request - resourcetemplates: - - apiVersion: tekton.dev/v1beta1 - kind: PipelineRun - metadata: - generateName: check-pr-labels- - labels: - prow.k8s.io/build-id: $(tt.params.buildUUID) - tekton.dev/check-name: check-pr-has-kind-label - tekton.dev/kind: ci - tekton.dev/pr-number: $(tt.params.pullRequestNumber) - annotations: - tekton.dev/gitRevision: "$(tt.params.gitRevision)" - tekton.dev/gitURL: "$(tt.params.gitRepository)" - spec: - pipelineRef: - name: tekton-kind-label - params: - - name: labels - value: $(tt.params.labels) - - name: checkName - value: check-pr-has-kind-label - - name: gitHubCommand - value: $(tt.params.gitHubCommand) \ No newline at end of file diff --git a/tekton/ci/templates/kustomization.yaml b/tekton/ci/templates/kustomization.yaml index f9c559aae..202bdc99f 100644 --- a/tekton/ci/templates/kustomization.yaml +++ b/tekton/ci/templates/kustomization.yaml @@ -1,4 +1,3 @@ resources: - plumbing-template.yaml -- all-template.yaml - bindings.yaml \ No newline at end of file