|
| 1 | +name: Validate Integration Comment |
| 2 | + |
| 3 | +# Posts the validation-results comment to the PR. |
| 4 | +# |
| 5 | +# Why a separate workflow? |
| 6 | +# ------------------------ |
| 7 | +# When a pull request is opened from a fork, GitHub deliberately downgrades |
| 8 | +# the GITHUB_TOKEN provided to `pull_request`-triggered workflows to |
| 9 | +# read-only — even if the workflow declares `permissions: pull-requests: |
| 10 | +# write`. That means the validation workflow can't post a sticky comment |
| 11 | +# directly on fork PRs. |
| 12 | +# |
| 13 | +# This companion workflow runs on `workflow_run` (which executes in the |
| 14 | +# base repository's context with normal token permissions) and posts the |
| 15 | +# comment using the artifact uploaded by validate-integration.yml. |
| 16 | +# |
| 17 | +# See: https://docs.github.com/actions/security-guides/automatic-token-authentication |
| 18 | + |
| 19 | +on: |
| 20 | + workflow_run: |
| 21 | + workflows: ['Validate Integration (Tooling)'] |
| 22 | + types: [completed] |
| 23 | + |
| 24 | +jobs: |
| 25 | + comment: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + if: github.event.workflow_run.event == 'pull_request' |
| 28 | + permissions: |
| 29 | + pull-requests: write |
| 30 | + actions: read |
| 31 | + steps: |
| 32 | + - name: Resolve PR number |
| 33 | + id: pr |
| 34 | + shell: bash |
| 35 | + env: |
| 36 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + REPO: ${{ github.repository }} |
| 38 | + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 39 | + # workflow_run.pull_requests is populated for same-repo PRs but |
| 40 | + # is empty for fork PRs (the head SHA is in a different repo). |
| 41 | + INLINE_PR: ${{ github.event.workflow_run.pull_requests[0].number }} |
| 42 | + # head_repository / head_branch are populated for both same-repo |
| 43 | + # and fork PRs and come from the workflow_run event payload, so |
| 44 | + # they're trustworthy (not influenceable by runner code). |
| 45 | + HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }} |
| 46 | + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 47 | + run: | |
| 48 | + set -euo pipefail |
| 49 | + # All inputs below come from the workflow_run event payload — |
| 50 | + # they cannot be influenced by code that ran on the original |
| 51 | + # runner. Never read the PR number from the artifact: the |
| 52 | + # validation job executes fork-controlled code (pytest, |
| 53 | + # imported integration modules), so any file it produces must |
| 54 | + # be treated as untrusted. |
| 55 | + PR="$INLINE_PR" |
| 56 | + if [ -z "$PR" ] || [ "$PR" = "null" ]; then |
| 57 | + # Fork PR: pull_requests array is empty (the head SHA lives |
| 58 | + # in a different repo, and /commits/{sha}/pulls doesn't |
| 59 | + # index fork commits). Look up the open PR by its head |
| 60 | + # (user:branch) — this pair uniquely identifies an open PR |
| 61 | + # in this repo. |
| 62 | + HEAD_USER="${HEAD_REPO%%/*}" |
| 63 | + PR=$(gh api "repos/$REPO/pulls?state=open&head=$HEAD_USER:$HEAD_BRANCH&per_page=10" \ |
| 64 | + --jq ".[] | select(.head.repo.full_name == \"$HEAD_REPO\" and .head.ref == \"$HEAD_BRANCH\") | .number" \ |
| 65 | + | head -1) |
| 66 | + fi |
| 67 | + if [ -z "$PR" ]; then |
| 68 | + echo "::error::Could not resolve PR for $HEAD_REPO:$HEAD_BRANCH (head SHA $HEAD_SHA)" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | + echo "number=$PR" >> "$GITHUB_OUTPUT" |
| 72 | +
|
| 73 | + - name: Download comment artifact |
| 74 | + uses: actions/download-artifact@v4 |
| 75 | + with: |
| 76 | + name: validation-comment |
| 77 | + run-id: ${{ github.event.workflow_run.id }} |
| 78 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + path: validation-artifact |
| 80 | + |
| 81 | + - name: Determine action |
| 82 | + id: meta |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + set -euo pipefail |
| 86 | + if [ -f validation-artifact/delete.marker ]; then |
| 87 | + echo "action=delete" >> "$GITHUB_OUTPUT" |
| 88 | + elif [ -f validation-artifact/comment.md ]; then |
| 89 | + echo "action=post" >> "$GITHUB_OUTPUT" |
| 90 | + else |
| 91 | + echo "::error::Artifact missing both comment.md and delete.marker" |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | +
|
| 95 | + - name: Delete stale comment |
| 96 | + if: steps.meta.outputs.action == 'delete' |
| 97 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 98 | + with: |
| 99 | + number: ${{ steps.pr.outputs.number }} |
| 100 | + header: validation-results |
| 101 | + delete: true |
| 102 | + |
| 103 | + - name: Post sticky comment |
| 104 | + if: steps.meta.outputs.action == 'post' |
| 105 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 106 | + with: |
| 107 | + number: ${{ steps.pr.outputs.number }} |
| 108 | + header: validation-results |
| 109 | + path: validation-artifact/comment.md |
0 commit comments