|
| 1 | +--- |
| 2 | +apiVersion: tekton.dev/v1 |
| 3 | +kind: PipelineRun |
| 4 | +metadata: |
| 5 | + name: prow-commands |
| 6 | + annotations: |
| 7 | + pipelinesascode.tekton.dev/on-comment: "^/(help|(assign|unassign|label|unlabel)[ ].*)" |
| 8 | + pipelinesascode.tekton.dev/max-keep-runs: "5" |
| 9 | +spec: |
| 10 | + pipelineSpec: |
| 11 | + tasks: |
| 12 | + - name: manage-pr |
| 13 | + displayName: Manage PR Assignments & Labels |
| 14 | + taskSpec: |
| 15 | + steps: |
| 16 | + - name: manage-pr |
| 17 | + image: registry.access.redhat.com/ubi9/ubi |
| 18 | + env: |
| 19 | + - name: GITHUB_TOKEN |
| 20 | + valueFrom: |
| 21 | + secretKeyRef: |
| 22 | + name: "{{ git_auth_secret }}" |
| 23 | + key: "git-provider-token" |
| 24 | + script: | |
| 25 | + #!/usr/bin/env python3 |
| 26 | + import os |
| 27 | + import re |
| 28 | + import requests |
| 29 | + import sys |
| 30 | +
|
| 31 | + GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") |
| 32 | + if not GITHUB_TOKEN: |
| 33 | + print("❌ GITHUB_TOKEN environment variable is missing", file=sys.stderr) |
| 34 | + sys.exit(1) |
| 35 | +
|
| 36 | + API_BASE = "https://api.github.com/repos/{{ repo_owner }}/{{ repo_name }}/issues/{{ pull_request_number }}" |
| 37 | + HEADERS = { |
| 38 | + "Authorization": f"Bearer {GITHUB_TOKEN}", |
| 39 | + "Accept": "application/vnd.github.v3+json", |
| 40 | + "X-GitHub-Api-Version": "2022-11-28" |
| 41 | + } |
| 42 | +
|
| 43 | + COMMENT = """{{ trigger_comment }}""" |
| 44 | + match = re.match(r"^/(assign|unassign|label|unlabel|help)\s*(.*)", COMMENT) |
| 45 | +
|
| 46 | + if not match: |
| 47 | + print(f"⚠️ No valid command found in comment: {COMMENT}", file=sys.stderr) |
| 48 | + sys.exit(1) |
| 49 | +
|
| 50 | + command, values = match.groups() |
| 51 | + values = values.split() |
| 52 | +
|
| 53 | + def make_request(method, url, data=None): |
| 54 | + try: |
| 55 | + if method == "POST": |
| 56 | + response = requests.post(url, json=data, headers=HEADERS) |
| 57 | + elif method == "DELETE": |
| 58 | + response = requests.delete(url, json=data, headers=HEADERS) |
| 59 | + else: |
| 60 | + return None |
| 61 | + response.raise_for_status() |
| 62 | + return response |
| 63 | + except requests.exceptions.RequestException as e: |
| 64 | + print(f"❌ API request failed: {e}", file=sys.stderr) |
| 65 | + sys.exit(1) |
| 66 | +
|
| 67 | + if command == "assign": |
| 68 | + API_URL = f"{API_BASE}/assignees" |
| 69 | + data = {"assignees": values} |
| 70 | + response = make_request("POST", API_URL, data) |
| 71 | + elif command == "unassign": |
| 72 | + API_URL = f"{API_BASE}/assignees" |
| 73 | + data = {"assignees": values} |
| 74 | + response = make_request("DELETE", API_URL, data) |
| 75 | + elif command == "label": |
| 76 | + API_URL = f"{API_BASE}/labels" |
| 77 | + data = {"labels": values} |
| 78 | + response = make_request("POST", API_URL, data) |
| 79 | + elif command == "unlabel": |
| 80 | + API_URL = f"{API_BASE}/labels/{'/'.join(values)}" |
| 81 | + for label in values: |
| 82 | + response = make_request("DELETE", f"{API_BASE}/labels/{label}") |
| 83 | + elif command == "help": |
| 84 | + API_URL = f"{API_BASE}/comments" |
| 85 | + help_text = """### 🤖 Available Commands |
| 86 | + | Command | Description | |
| 87 | + |---------|-------------| |
| 88 | + | `/assign user1 user2` | Assigns users to the PR | |
| 89 | + | `/unassign user1 user2` | Removes assigned users | |
| 90 | + | `/label bug feature` | Adds labels to the PR | |
| 91 | + | `/unlabel bug feature` | Removes labels from the PR | |
| 92 | + | `/help` | Shows this help message | |
| 93 | + """ |
| 94 | + response = make_request("POST", API_URL, {"body": help_text}) |
| 95 | + print(f"✅ Posted help message") |
| 96 | + sys.exit(0) |
| 97 | +
|
| 98 | + if response and response.status_code in [200, 201, 204]: |
| 99 | + print(f"✅ Successfully processed {command}: {', '.join(values) if values else ''}") |
| 100 | + else: |
| 101 | + print(f"❌ Failed to process {command}: {response.status_code if response else 'N/A'} - {response.text if response else 'N/A'}", file=sys.stderr) |
| 102 | + sys.exit(1) |
0 commit comments