|
| 1 | +name: "Robot Test Unlabel" |
| 2 | +description: "On new commits, removes the test-robot-done label if present and posts a comment." |
| 3 | + |
| 4 | +inputs: |
| 5 | + label-test-robot-needed: |
| 6 | + description: 'Label indicating the PR needs robot testing' |
| 7 | + required: false |
| 8 | + default: 'test-robot-needed' |
| 9 | + label-test-robot-done: |
| 10 | + description: 'Label indicating the PR has been robot-tested' |
| 11 | + required: false |
| 12 | + default: 'test-robot-done' |
| 13 | + token: |
| 14 | + description: 'GitHub token or PAT used to remove labels and post comments (defaults to github.token)' |
| 15 | + required: false |
| 16 | + default: '' |
| 17 | + |
| 18 | +runs: |
| 19 | + using: "composite" |
| 20 | + steps: |
| 21 | + - name: Remove label and comment |
| 22 | + shell: python3 {0} |
| 23 | + env: |
| 24 | + GH_TOKEN: ${{ inputs.token || github.token }} |
| 25 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 26 | + REPO: ${{ github.repository }} |
| 27 | + COMMIT_SHA: ${{ github.event.pull_request.head.sha }} |
| 28 | + LABEL_DONE: ${{ inputs.label-test-robot-done }} |
| 29 | + LABEL_NEEDED: ${{ inputs.label-test-robot-needed }} |
| 30 | + run: | |
| 31 | + import os, json, sys, urllib.request, urllib.error, urllib.parse |
| 32 | +
|
| 33 | + token = os.environ['GH_TOKEN'] |
| 34 | + repo = os.environ['REPO'] |
| 35 | + pr = os.environ['PR_NUMBER'] |
| 36 | + sha = os.environ.get('COMMIT_SHA', '').strip() |
| 37 | + label_done = os.environ['LABEL_DONE'] |
| 38 | + label_needed = os.environ['LABEL_NEEDED'] |
| 39 | +
|
| 40 | + headers = { |
| 41 | + 'Authorization': f'Bearer {token}', |
| 42 | + 'Accept': 'application/vnd.github+json', |
| 43 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + } |
| 46 | +
|
| 47 | + def gh(method, path, body=None): |
| 48 | + req = urllib.request.Request( |
| 49 | + f'https://api.github.com{path}', |
| 50 | + data=json.dumps(body).encode() if body else None, |
| 51 | + headers=headers, |
| 52 | + method=method, |
| 53 | + ) |
| 54 | + try: |
| 55 | + with urllib.request.urlopen(req) as r: |
| 56 | + content = r.read() |
| 57 | + return json.loads(content) if content else None |
| 58 | + except urllib.error.HTTPError as e: |
| 59 | + print(f'GitHub API error {e.code}: {e.read().decode()}', file=sys.stderr) |
| 60 | + sys.exit(1) |
| 61 | +
|
| 62 | + labels = {l['name'] for l in gh('GET', f'/repos/{repo}/issues/{pr}/labels')} |
| 63 | +
|
| 64 | + if label_done not in labels: |
| 65 | + print(f'Label `{label_done}` not present — nothing to do.') |
| 66 | + sys.exit(0) |
| 67 | +
|
| 68 | + safe_label = urllib.parse.quote(label_done, safe='') |
| 69 | + gh('DELETE', f'/repos/{repo}/issues/{pr}/labels/{safe_label}') |
| 70 | +
|
| 71 | + sha_line = f'\n**Commit:** `{sha}`' if sha else '' |
| 72 | + warning = ( |
| 73 | + f'\n\n> [!WARNING]\n' |
| 74 | + f'> `{label_done}` was removed but `{label_needed}` was not present —' |
| 75 | + f' this PR may not be queued for robot testing.' |
| 76 | + ) if label_needed not in labels else '' |
| 77 | +
|
| 78 | + body = ( |
| 79 | + f'### 🔄 Robot Test Label Removed\n\n' |
| 80 | + f'`{label_done}` has been removed due to new commits pushed to this PR.{sha_line}' |
| 81 | + f'{warning}' |
| 82 | + ) |
| 83 | + gh('POST', f'/repos/{repo}/issues/{pr}/comments', {'body': body}) |
0 commit comments