Skip to content

Commit 5c82f9f

Browse files
committed
feat: Add tekton pipeline to auto label PR with e2e
- Added a new Tekton PipelineRun to automatically label pull requests. - The pipeline will add the `e2e` label to pull requests if not present - Triggered on `pull_request` events targeting the `main` branch. - Uses a python script to interact with Github API using a token - The script checks if the PR already has the label before adding it Signed-off-by: Chmouel Boudjnah <[email protected]>
1 parent 2f4a1a3 commit 5c82f9f

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Diff for: .tekton/e2e-label.yaml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
apiVersion: tekton.dev/v1beta1
3+
kind: PipelineRun
4+
metadata:
5+
name: e2e-label.yaml
6+
annotations:
7+
pipelinesascode.tekton.dev/max-keep-runs: "2"
8+
pipelinesascode.tekton.dev/cancel-in-progress: "true"
9+
pipelinesascode.tekton.dev/on-event: "pull_request"
10+
pipelinesascode.tekton.dev/on-target-branch: "main"
11+
pipelinesascode.tekton.dev/on-path-change: "[***/*.go, .github/workflows/*l]"
12+
spec:
13+
pipelineSpec:
14+
tasks:
15+
- name: label-pr
16+
taskSpec:
17+
steps:
18+
- name: label-pr
19+
# it has curl and we already pulled it
20+
image: registry.access.redhat.com/ubi9/ubi
21+
env:
22+
- name: HUB_TOKEN
23+
valueFrom:
24+
secretKeyRef:
25+
name: "nightly-ci-github-hub-token"
26+
key: "hub-token"
27+
script: |
28+
#!/usr/bin/env python3
29+
30+
import os
31+
import sys
32+
import requests
33+
34+
PR_NUMBER = "{{ pull_request_number }}"
35+
REPO_OWNER = "{{ repo_owner }}"
36+
REPO_NAME = "{{ repo_name }}"
37+
LABEL = "e2e"
38+
HUB_TOKEN = os.getenv("HUB_TOKEN")
39+
40+
headers = {
41+
"Accept": "application/vnd.github.v3+json",
42+
"Authorization": f"token {HUB_TOKEN}",
43+
"User-Agent": "PAC"
44+
}
45+
46+
# Check if the PR already has the label
47+
response = requests.get(
48+
f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues/{PR_NUMBER}/labels",
49+
headers=headers
50+
)
51+
52+
if response.status_code != 200:
53+
print(f"Failed to get labels, response code: {response.status_code}")
54+
sys.exit(1)
55+
56+
labels = [label['name'] for label in response.json()]
57+
58+
if LABEL in labels:
59+
print(f"Pull request already has the label '{LABEL}'")
60+
sys.exit(0)
61+
62+
# Add the label to the PR
63+
response = requests.post(
64+
f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/issues/{PR_NUMBER}/labels",
65+
headers=headers,
66+
json={"labels": [LABEL]}
67+
)
68+
69+
if response.status_code != 200:
70+
print(f"Failed to add label, response code: {response.status_code}")
71+
sys.exit(1)
72+
73+
print(f"Label '{LABEL}' added to pull request #{PR_NUMBER} successfully")

0 commit comments

Comments
 (0)