Skip to content

Commit 472cb1f

Browse files
afrittolitekton-robot
authored andcommitted
Fix the check labels job
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>
1 parent 02da504 commit 472cb1f

File tree

3 files changed

+22
-63
lines changed

3 files changed

+22
-63
lines changed

tekton/ci/jobs/tasks.yaml

+21-41
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ metadata:
148148
description: |
149149
Verifies that a PR has one valid kind label
150150
spec:
151-
resources:
152-
inputs:
153-
- name: pr
154-
type: pullRequest
155-
outputs:
156-
- name: pr
157-
type: pullRequest
158151
params:
159152
- name: labels
160153
description: The labels attached to the Pull Request
@@ -163,12 +156,6 @@ spec:
163156
configMap:
164157
name: label-config-v2
165158
steps:
166-
- name: copy-pr-to-output
167-
image: busybox
168-
script: |
169-
#!/bin/sh
170-
mkdir -p $(outputs.resources.pr.path)
171-
cp -r $(inputs.resources.pr.path)/* $(outputs.resources.pr.path)/
172159
- name: install-pyyaml
173160
image: python:3-alpine
174161
script: |
@@ -187,38 +174,31 @@ spec:
187174
188175
prLabelsText = """$(params.labels)"""
189176
prLabels = json.loads(prLabelsText)
190-
labelNames = list(map(lambda e: e["name"], prLabels))
191-
kindLabels = list(filter(lambda e: "kind" in e, labelNames))
192177
193178
availableLabels = None
194-
with open("/etc/config/labels.yaml", "r") as stream:
179+
with open("labels.yaml", "r") as stream:
195180
availableLabels = yaml.safe_load(stream)["default"]["labels"]
196181
197-
availableKindLabels = list(filter(lambda e: "kind/" in e["name"], availableLabels))
198-
availableKindNamesAndDescriptions = map(lambda e: "`" +str(e["name"])+ "`" + ": " + str(e["description"]), availableKindLabels)
199-
200-
comment_template=""
201-
if (len(kindLabels) > 1 or len(kindLabels) == 0):
202-
comment_template += """
203-
**This PR cannot be merged:** expecting exactly one kind/ label
204-
205-
<details>
206-
207-
Available `kind/` labels are:
208-
209-
"""
210-
211-
for i in availableKindNamesAndDescriptions:
212-
comment_template += i + "\n"
213-
214-
comment_template += """
182+
availableKindLabels = {x.get("name"):x.get("description") for x in availableLabels if x.get("name").startswith("kind/")}
183+
foundKindLabels = set([x.get("name") for x in prLabels if x.get("name").startswith("kind/") and x.get("name")])
184+
validKindLabels = set([x for x in foundKindLabels if x in availableKindLabels])
215185
216-
</details>
217-
"""
218-
new_comment_path = "$(outputs.resources.pr.path)/comments/new.json"
219-
comment_body = dict(body=comment_template)
220-
with open(new_comment_path, "w") as comment:
221-
json.dump(comment_body, comment)
186+
# Check that we have one and only one kind label
187+
foundLabels = len(validKindLabels)
188+
if (foundLabels > 1 or foundLabels == 0):
189+
msg = "Error: {} valid \"kind/*\" labels found".format(foundLabels)
190+
if foundLabels > 1:
191+
msg += "({})".format(validKindLabels)
192+
msg += ", expecting exactly one."
193+
invalidKindLabels = foundKindLabels - validKindLabels
194+
if len(invalidKindLabels) > 0:
195+
msg += " Invalid labels found: {}".format(invalidKindLabels)
196+
print(msg)
197+
print("\nAvailable \"kind/*\" labels are:")
198+
for label, description in availableKindLabels.items():
199+
print("\t{}: {}".format(label, description))
222200
223201
# Check failed. Return exit code 1.
224-
sys.exit(1)
202+
sys.exit(1)
203+
else:
204+
print("Exactly one \"kind/*\" label found: {}".format(validKindLabels[0]))

tekton/ci/jobs/tekton-kind-label.yaml

-10
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ spec:
1111
description: The command that was used to trigger testing
1212
- name: labels
1313
description: The labels attached to the Pull Request
14-
resources:
15-
- name: pr
16-
type: pullRequest
1714
tasks:
1815
- name: check-kind-label
1916
conditions:
@@ -28,10 +25,3 @@ spec:
2825
params:
2926
- name: labels
3027
value: $(params.labels)
31-
resources:
32-
inputs:
33-
- name: pr
34-
resource: pr
35-
outputs:
36-
- name: pr
37-
resource: pr

tekton/ci/templates/all-template.yaml

+1-12
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,4 @@ spec:
5151
- name: checkName
5252
value: check-pr-has-kind-label
5353
- name: gitHubCommand
54-
value: $(tt.params.gitHubCommand)
55-
resources:
56-
- name: pr
57-
resourceSpec:
58-
type: pullRequest
59-
params:
60-
- name: url
61-
value: $(tt.params.pullRequestUrl)
62-
secrets:
63-
- fieldName: authToken
64-
secretName: bot-token-github
65-
secretKey: bot-token
54+
value: $(tt.params.gitHubCommand)

0 commit comments

Comments
 (0)