|
| 1 | +name: clang-tidy-bazel-post |
| 2 | + |
| 3 | +# Runs in the base repository's context with a writable GITHUB_TOKEN, so it |
| 4 | +# can post reviewdog comments on PRs opened from forks (which the upstream |
| 5 | +# `clang-tidy-bazel` workflow cannot, since fork pull_request runs get a |
| 6 | +# read-only token by GitHub's design). |
| 7 | +# |
| 8 | +# Security: this workflow MUST NOT execute untrusted PR code. It only reads |
| 9 | +# the text artifact (clang-tidy.txt) produced by the upstream workflow and |
| 10 | +# the metadata file we wrote there ourselves. No checkout of the PR head, |
| 11 | +# no bazel build, no scripts from the fork. |
| 12 | + |
| 13 | +on: |
| 14 | + workflow_run: |
| 15 | + workflows: ["clang-tidy-bazel"] |
| 16 | + types: |
| 17 | + - completed |
| 18 | + |
| 19 | +permissions: |
| 20 | + contents: read |
| 21 | + pull-requests: write |
| 22 | + # Needed for actions/download-artifact@v4 to fetch from another workflow. |
| 23 | + actions: read |
| 24 | + |
| 25 | +jobs: |
| 26 | + Post-Reviewdog: |
| 27 | + # Run on success AND failure: Stage A intentionally exits non-zero when |
| 28 | + # clang-tidy findings exist on the PR diff (to fail the required PR |
| 29 | + # check), but the artifact is uploaded *before* that fail step, so the |
| 30 | + # findings are still posted as review comments. Only skip on cancelled. |
| 31 | + if: ${{ github.event.workflow_run.conclusion != 'cancelled' }} |
| 32 | + runs-on: ${{ vars.USE_SELF_HOSTED == 'true' && 'self-hosted' || 'ubuntu-latest' }} |
| 33 | + steps: |
| 34 | + # Reviewdog's github-pr-review reporter resolves the local git root |
| 35 | + # before flushing comments and silently no-ops if .git is missing. |
| 36 | + # Check out the base repo (default branch, shallow) so reviewdog has |
| 37 | + # a .git directory to operate against. No fork code involved — this |
| 38 | + # is the base repo at HEAD of its default branch. |
| 39 | + - name: Check out base repo for reviewdog .git requirement |
| 40 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 41 | + with: |
| 42 | + fetch-depth: 1 |
| 43 | + # Default ref in workflow_run context is the base repo's default |
| 44 | + # branch, which is the safe choice here. Reviewdog uses GitHub |
| 45 | + # API to fetch the actual PR diff, so the local SHA need not |
| 46 | + # match the PR head. |
| 47 | + |
| 48 | + - name: Download clang-tidy artifact |
| 49 | + uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 |
| 50 | + with: |
| 51 | + name: clang-tidy-bazel |
| 52 | + run-id: ${{ github.event.workflow_run.id }} |
| 53 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 54 | + |
| 55 | + - name: Load PR metadata |
| 56 | + id: meta |
| 57 | + run: | |
| 58 | + # pr-meta.txt is produced by the upstream workflow from the |
| 59 | + # pull_request event payload. It is text we wrote ourselves — |
| 60 | + # not arbitrary fork content — and is parsed with a strict |
| 61 | + # allowlist below before being exported. |
| 62 | + if [ ! -f pr-meta.txt ]; then |
| 63 | + echo "::error::pr-meta.txt missing from artifact" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + while IFS='=' read -r key value; do |
| 67 | + case "$key" in |
| 68 | + pr_number|head_sha|base_sha|head_repo|base_repo) ;; |
| 69 | + *) continue ;; |
| 70 | + esac |
| 71 | + # Validate values: numbers, hex SHAs, or owner/repo slugs only. |
| 72 | + case "$key" in |
| 73 | + pr_number) |
| 74 | + [[ "$value" =~ ^[0-9]+$ ]] || { echo "::error::bad pr_number"; exit 1; } ;; |
| 75 | + head_sha|base_sha) |
| 76 | + [[ "$value" =~ ^[0-9a-f]{40}$ ]] || { echo "::error::bad $key"; exit 1; } ;; |
| 77 | + head_repo|base_repo) |
| 78 | + [[ "$value" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]] || { echo "::error::bad $key"; exit 1; } ;; |
| 79 | + esac |
| 80 | + echo "$key=$value" >> "$GITHUB_OUTPUT" |
| 81 | + done < pr-meta.txt |
| 82 | +
|
| 83 | + - name: Synthesize pull_request event payload |
| 84 | + id: event |
| 85 | + env: |
| 86 | + PR_NUMBER: ${{ steps.meta.outputs.pr_number }} |
| 87 | + HEAD_SHA: ${{ steps.meta.outputs.head_sha }} |
| 88 | + BASE_SHA: ${{ steps.meta.outputs.base_sha }} |
| 89 | + HEAD_REPO: ${{ steps.meta.outputs.head_repo }} |
| 90 | + BASE_REPO: ${{ steps.meta.outputs.base_repo }} |
| 91 | + run: | |
| 92 | + # Reviewdog's `github-pr-review` reporter reads GITHUB_EVENT_PATH |
| 93 | + # expecting a pull_request payload. The real event here is |
| 94 | + # workflow_run, so we synthesize the minimum payload reviewdog |
| 95 | + # needs and point GITHUB_EVENT_PATH at it for the next step. |
| 96 | + EVENT_PATH="${RUNNER_TEMP}/pr-event.json" |
| 97 | + python3 - <<'PY' > "$EVENT_PATH" |
| 98 | + import json, os |
| 99 | + payload = { |
| 100 | + "action": "synchronize", |
| 101 | + "number": int(os.environ["PR_NUMBER"]), |
| 102 | + "pull_request": { |
| 103 | + "number": int(os.environ["PR_NUMBER"]), |
| 104 | + "head": { |
| 105 | + "sha": os.environ["HEAD_SHA"], |
| 106 | + "repo": {"full_name": os.environ["HEAD_REPO"]}, |
| 107 | + }, |
| 108 | + "base": { |
| 109 | + "sha": os.environ["BASE_SHA"], |
| 110 | + "repo": {"full_name": os.environ["BASE_REPO"]}, |
| 111 | + }, |
| 112 | + }, |
| 113 | + "repository": { |
| 114 | + "full_name": os.environ["BASE_REPO"], |
| 115 | + "owner": {"login": os.environ["BASE_REPO"].split("/")[0]}, |
| 116 | + "name": os.environ["BASE_REPO"].split("/")[1], |
| 117 | + }, |
| 118 | + } |
| 119 | + print(json.dumps(payload)) |
| 120 | + PY |
| 121 | + echo "event_path=${EVENT_PATH}" >> "$GITHUB_OUTPUT" |
| 122 | +
|
| 123 | + - name: Set up reviewdog |
| 124 | + uses: reviewdog/action-setup@d8a7baabd7f3e8544ee4dbde3ee41d0011c3a93f # v1.5.0 |
| 125 | + with: |
| 126 | + reviewdog_version: latest |
| 127 | + |
| 128 | + - name: Run reviewdog |
| 129 | + env: |
| 130 | + REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 131 | + GITHUB_EVENT_NAME: pull_request |
| 132 | + GITHUB_EVENT_PATH: ${{ steps.event.outputs.event_path }} |
| 133 | + GITHUB_SHA: ${{ steps.meta.outputs.head_sha }} |
| 134 | + GITHUB_REPOSITORY: ${{ steps.meta.outputs.base_repo }} |
| 135 | + run: | |
| 136 | + reviewdog \ |
| 137 | + -efm="%E%f:%l:%c: error: %m" \ |
| 138 | + -efm="%W%f:%l:%c: warning: %m" \ |
| 139 | + -name="clang-tidy" \ |
| 140 | + -reporter=github-pr-review \ |
| 141 | + -filter-mode=added \ |
| 142 | + -fail-level=any \ |
| 143 | + < clang-tidy.txt |
0 commit comments