|
7 | 7 | - reopened |
8 | 8 | - synchronize |
9 | 9 | - ready_for_review |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + pr_number: |
| 13 | + description: "PR number to label." |
| 14 | + required: true |
| 15 | + type: number |
10 | 16 |
|
11 | 17 | permissions: |
12 | 18 | contents: read |
| 19 | + issues: write |
13 | 20 | pull-requests: write |
14 | 21 |
|
15 | 22 | jobs: |
16 | 23 | label: |
17 | 24 | runs-on: ubuntu-latest |
18 | 25 | steps: |
| 26 | + - name: Ensure main workflow |
| 27 | + if: ${{ github.event_name == 'workflow_dispatch' && github.ref != 'refs/heads/main' }} |
| 28 | + run: | |
| 29 | + echo "This workflow must be dispatched from main." |
| 30 | + exit 1 |
| 31 | +
|
| 32 | + - name: Resolve PR context |
| 33 | + id: pr |
| 34 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd |
| 35 | + env: |
| 36 | + MANUAL_PR_NUMBER: ${{ inputs.pr_number || '' }} |
| 37 | + with: |
| 38 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 39 | + script: | |
| 40 | + const isManual = context.eventName === 'workflow_dispatch'; |
| 41 | + let pr; |
| 42 | + if (isManual) { |
| 43 | + const prNumber = Number(process.env.MANUAL_PR_NUMBER); |
| 44 | + if (!prNumber) { |
| 45 | + core.setFailed('workflow_dispatch requires pr_number input.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + const { data } = await github.rest.pulls.get({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + pull_number: prNumber, |
| 52 | + }); |
| 53 | + pr = data; |
| 54 | + } else { |
| 55 | + pr = context.payload.pull_request; |
| 56 | + } |
| 57 | + if (!pr) { |
| 58 | + core.setFailed('Missing pull request context.'); |
| 59 | + return; |
| 60 | + } |
| 61 | + const headRepo = pr.head.repo.full_name; |
| 62 | + const repoFullName = `${context.repo.owner}/${context.repo.repo}`; |
| 63 | + core.setOutput('pr_number', pr.number); |
| 64 | + core.setOutput('base_sha', pr.base.sha); |
| 65 | + core.setOutput('head_sha', pr.head.sha); |
| 66 | + core.setOutput('head_repo', headRepo); |
| 67 | + core.setOutput('is_fork', headRepo !== repoFullName); |
| 68 | +
|
19 | 69 | - name: Checkout base |
20 | | - uses: actions/checkout@v6 |
| 70 | + uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 |
21 | 71 | with: |
22 | 72 | fetch-depth: 0 |
23 | | - ref: ${{ github.event.pull_request.base.sha }} |
| 73 | + ref: ${{ steps.pr.outputs.base_sha }} |
24 | 74 | - name: Fetch PR head |
25 | 75 | env: |
26 | | - PR_NUMBER: ${{ github.event.pull_request.number }} |
27 | | - PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} |
28 | | - PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 76 | + PR_HEAD_REPO: ${{ steps.pr.outputs.head_repo }} |
| 77 | + PR_HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
29 | 78 | run: | |
30 | 79 | set -euo pipefail |
31 | | - git fetch origin "refs/pull/${PR_NUMBER}/head:pr-head" |
32 | | - git cat-file -e "${PR_BASE_SHA}^{commit}" |
33 | | - git cat-file -e "${PR_HEAD_SHA}^{commit}" || git cat-file -e "pr-head^{commit}" |
| 80 | + git fetch --no-tags --prune --recurse-submodules=no \ |
| 81 | + "https://github.com/${PR_HEAD_REPO}.git" \ |
| 82 | + "${PR_HEAD_SHA}" |
34 | 83 | - name: Collect PR diff |
35 | 84 | env: |
36 | | - PR_BASE_SHA: ${{ github.event.pull_request.base.sha }} |
37 | | - PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 85 | + PR_BASE_SHA: ${{ steps.pr.outputs.base_sha }} |
| 86 | + PR_HEAD_SHA: ${{ steps.pr.outputs.head_sha }} |
38 | 87 | run: | |
39 | 88 | set -euo pipefail |
40 | 89 | mkdir -p .tmp/pr-labels |
|
49 | 98 | mkdir -p "$output_dir" |
50 | 99 | echo "output_file=${output_file}" >> "$GITHUB_OUTPUT" |
51 | 100 | - name: Run Codex labeling |
52 | | - uses: openai/codex-action@v1 |
| 101 | + if: ${{ github.event_name == 'workflow_dispatch' || steps.pr.outputs.is_fork != 'true' }} |
| 102 | + uses: openai/codex-action@f5c0ca71642badb34c1e66321d8d85685a0fa3dc |
53 | 103 | with: |
54 | 104 | openai-api-key: ${{ secrets.PROD_OPENAI_API_KEY }} |
55 | 105 | prompt-file: .github/codex/prompts/pr-labels.md |
|
59 | 109 | - name: Apply labels |
60 | 110 | env: |
61 | 111 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
62 | | - PR_NUMBER: ${{ github.event.pull_request.number }} |
| 112 | + PR_NUMBER: ${{ steps.pr.outputs.pr_number }} |
63 | 113 | CODEX_OUTPUT_PATH: ${{ steps.codex-output.outputs.output_file }} |
64 | 114 | run: | |
65 | 115 | python - <<'PY' |
@@ -143,3 +193,60 @@ jobs: |
143 | 193 |
|
144 | 194 | subprocess.check_call(cmd) |
145 | 195 | PY |
| 196 | +
|
| 197 | + - name: Comment on manual run failure |
| 198 | + if: ${{ github.event_name == 'workflow_dispatch' && always() }} |
| 199 | + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd |
| 200 | + env: |
| 201 | + PR_NUMBER: ${{ steps.pr.outputs.pr_number }} |
| 202 | + JOB_STATUS: ${{ job.status }} |
| 203 | + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} |
| 204 | + CODEX_CONCLUSION: ${{ steps.run_codex.conclusion }} |
| 205 | + with: |
| 206 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 207 | + script: | |
| 208 | + const marker = '<!-- pr-labels-manual-run -->'; |
| 209 | + const jobStatus = process.env.JOB_STATUS; |
| 210 | + if (jobStatus === 'success') { |
| 211 | + return; |
| 212 | + } |
| 213 | + const prNumber = Number(process.env.PR_NUMBER); |
| 214 | + if (!prNumber) { |
| 215 | + core.setFailed('Missing PR number for manual run comment.'); |
| 216 | + return; |
| 217 | + } |
| 218 | + const body = [ |
| 219 | + marker, |
| 220 | + 'Manual PR labeling failed.', |
| 221 | + `Job status: ${jobStatus}.`, |
| 222 | + `Run: ${process.env.RUN_URL}.`, |
| 223 | + `Codex labeling: ${process.env.CODEX_CONCLUSION}.`, |
| 224 | + ].join('\n'); |
| 225 | + const { data: comments } = await github.rest.issues.listComments({ |
| 226 | + owner: context.repo.owner, |
| 227 | + repo: context.repo.repo, |
| 228 | + issue_number: prNumber, |
| 229 | + per_page: 100, |
| 230 | + }); |
| 231 | + const existing = comments.find( |
| 232 | + (comment) => |
| 233 | + comment.user?.login === 'github-actions[bot]' && |
| 234 | + comment.body?.includes(marker), |
| 235 | + ); |
| 236 | + if (existing) { |
| 237 | + await github.rest.issues.updateComment({ |
| 238 | + owner: context.repo.owner, |
| 239 | + repo: context.repo.repo, |
| 240 | + comment_id: existing.id, |
| 241 | + body, |
| 242 | + }); |
| 243 | + core.info(`Updated existing comment ${existing.id}`); |
| 244 | + return; |
| 245 | + } |
| 246 | + const { data: created } = await github.rest.issues.createComment({ |
| 247 | + owner: context.repo.owner, |
| 248 | + repo: context.repo.repo, |
| 249 | + issue_number: prNumber, |
| 250 | + body, |
| 251 | + }); |
| 252 | + core.info(`Created comment ${created.id}`); |
0 commit comments