Skip to content

Commit

Permalink
Fix the check labels job
Browse files Browse the repository at this point in the history
The check labels job uses the PR pipeline resource to add a comment
to the PR with instructions for developers in case of failure.
That doesn't work though because in case of failure the sync-up side
of the pipeline resource is not processed, so the commit is not
written.

Fix that by removing the pipeline resource and writing the instructions
to the step log instead. The step log is linked in the github status
details through the tekton dashboard.

In future we might extend the github update service to include
support for comments, but that would require support for results on
failure: tektoncd/pipeline#3749

Signed-off-by: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
  • Loading branch information
afrittoli committed Feb 5, 2021
1 parent ef5961b commit c71afba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 63 deletions.
62 changes: 21 additions & 41 deletions tekton/ci/jobs/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,6 @@ metadata:
description: |
Verifies that a PR has one valid kind label
spec:
resources:
inputs:
- name: pr
type: pullRequest
outputs:
- name: pr
type: pullRequest
params:
- name: labels
description: The labels attached to the Pull Request
Expand All @@ -163,12 +156,6 @@ spec:
configMap:
name: label-config-v2
steps:
- name: copy-pr-to-output
image: busybox
script: |
#!/bin/sh
mkdir -p $(outputs.resources.pr.path)
cp -r $(inputs.resources.pr.path)/* $(outputs.resources.pr.path)/
- name: install-pyyaml
image: python:3-alpine
script: |
Expand All @@ -187,38 +174,31 @@ spec:
prLabelsText = """$(params.labels)"""
prLabels = json.loads(prLabelsText)
labelNames = list(map(lambda e: e["name"], prLabels))
kindLabels = list(filter(lambda e: "kind" in e, labelNames))
availableLabels = None
with open("/etc/config/labels.yaml", "r") as stream:
with open("labels.yaml", "r") as stream:
availableLabels = yaml.safe_load(stream)["default"]["labels"]
availableKindLabels = list(filter(lambda e: "kind/" in e["name"], availableLabels))
availableKindNamesAndDescriptions = map(lambda e: "`" +str(e["name"])+ "`" + ": " + str(e["description"]), availableKindLabels)
comment_template=""
if (len(kindLabels) > 1 or len(kindLabels) == 0):
comment_template += """
**This PR cannot be merged:** expecting exactly one kind/ label
<details>
Available `kind/` labels are:
"""
for i in availableKindNamesAndDescriptions:
comment_template += i + "\n"
comment_template += """
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])
</details>
"""
new_comment_path = "$(outputs.resources.pr.path)/comments/new.json"
comment_body = dict(body=comment_template)
with open(new_comment_path, "w") as comment:
json.dump(comment_body, comment)
# 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)
sys.exit(1)
else:
print("Exactly one \"kind/*\" label found: {}".format(validKindLabels[0]))
10 changes: 0 additions & 10 deletions tekton/ci/jobs/tekton-kind-label.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ spec:
description: The command that was used to trigger testing
- name: labels
description: The labels attached to the Pull Request
resources:
- name: pr
type: pullRequest
tasks:
- name: check-kind-label
conditions:
Expand All @@ -28,10 +25,3 @@ spec:
params:
- name: labels
value: $(params.labels)
resources:
inputs:
- name: pr
resource: pr
outputs:
- name: pr
resource: pr
13 changes: 1 addition & 12 deletions tekton/ci/templates/all-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,4 @@ spec:
- name: checkName
value: check-pr-has-kind-label
- name: gitHubCommand
value: $(tt.params.gitHubCommand)
resources:
- name: pr
resourceSpec:
type: pullRequest
params:
- name: url
value: $(tt.params.pullRequestUrl)
secrets:
- fieldName: authToken
secretName: bot-token-github
secretKey: bot-token
value: $(tt.params.gitHubCommand)

0 comments on commit c71afba

Please sign in to comment.