|
1 | 1 | name: CLA Gate |
2 | 2 |
|
3 | | -# This workflow publishes a repository-owned commit status named `CLA Gate`. |
4 | | -# Make `CLA Gate` required instead of requiring CLA Assistant's raw `license/cla` |
5 | | -# status directly. That lets merge queue entries pass without waiting for CLA |
6 | | -# Assistant to report on the synthetic merge-group SHA, while pull requests still |
7 | | -# mirror the real CLA Assistant result. |
| 3 | +# Make this Actions check required instead of requiring CLA Assistant's raw |
| 4 | +# `license/cla` status directly. Merge queue entries pass without waiting for |
| 5 | +# CLA Assistant on the synthetic merge-group SHA, while pull requests wait for |
| 6 | +# the real CLA Assistant result on the PR head SHA. |
8 | 7 | # |
9 | | -# SECURITY: This workflow uses pull_request_target so it can publish commit |
10 | | -# statuses on external PRs. It must never check out, build, or execute PR code. |
| 8 | +# SECURITY: This workflow uses pull_request_target so it can read statuses on |
| 9 | +# external PRs. It must never check out, build, or execute PR code. |
11 | 10 |
|
12 | 11 | on: |
13 | 12 | pull_request_target: |
14 | | - types: [opened, reopened] |
15 | | - status: |
| 13 | + types: [opened, reopened, synchronize, ready_for_review] |
16 | 14 | merge_group: |
17 | 15 |
|
18 | 16 | permissions: |
19 | 17 | contents: read |
20 | | - pull-requests: read |
21 | | - statuses: write |
| 18 | + statuses: read |
| 19 | + |
| 20 | +concurrency: |
| 21 | + group: cla-gate-${{ github.event.pull_request.number || github.sha }} |
| 22 | + cancel-in-progress: true |
22 | 23 |
|
23 | 24 | jobs: |
24 | | - publish-cla-gate-status: |
25 | | - name: CLA status |
| 25 | + cla-gate: |
| 26 | + name: CLA Gate |
26 | 27 | runs-on: ubuntu-latest |
27 | | - if: github.event_name != 'status' || github.event.context == 'license/cla' |
28 | 28 |
|
29 | 29 | steps: |
30 | | - - name: Check out trusted base code |
31 | | - uses: actions/checkout@v4 |
32 | | - with: |
33 | | - ref: ${{ github.event.repository.default_branch }} |
34 | | - |
35 | | - - uses: dsherret/rust-toolchain-file@v1 |
| 30 | + - name: Skip CLA check for merge queue |
| 31 | + if: ${{ github.event_name == 'merge_group' }} |
| 32 | + run: echo "Merge group entry; CLA was checked before entering the queue." |
36 | 33 |
|
37 | | - - name: Publish CLA Gate status |
38 | | - uses: actions/github-script@v7 |
| 34 | + - name: Wait for CLA Assistant |
| 35 | + if: ${{ github.event_name == 'pull_request_target' }} |
39 | 36 | env: |
40 | | - GITHUB_TOKEN: ${{ github.token }} |
41 | | - with: |
42 | | - script: | |
43 | | - const { execFileSync } = require("child_process"); |
44 | | -
|
45 | | - function claStatus(args) { |
46 | | - const output = execFileSync("cargo", ["ci", "cla-assistant", "status", ...args], { |
47 | | - encoding: "utf8", |
48 | | - env: process.env, |
49 | | - }); |
50 | | - return JSON.parse(output); |
51 | | - } |
52 | | -
|
53 | | - async function postStatus({ sha, state, description, targetUrl }) { |
54 | | - const statusContext = "CLA Gate"; |
55 | | - core.info( |
56 | | - `Publishing ${statusContext}=${state} for ${sha}: ${description}` |
57 | | - ); |
58 | | -
|
59 | | - await github.rest.repos.createCommitStatus({ |
60 | | - owner: context.repo.owner, |
61 | | - repo: context.repo.repo, |
62 | | - sha, |
63 | | - state, |
64 | | - description, |
65 | | - context: statusContext, |
66 | | - target_url: targetUrl, |
67 | | - }); |
68 | | - } |
69 | | -
|
70 | | - let targetSha; |
71 | | - let claStatusArgs; |
72 | | -
|
73 | | - if (context.eventName === "merge_group") { |
74 | | - await postStatus({ |
75 | | - sha: process.env.GITHUB_SHA, |
76 | | - state: "success", |
77 | | - description: "Merge group entry; CLA already checked before queue", |
78 | | - }); |
79 | | - return; |
80 | | - } |
81 | | -
|
82 | | - if (context.eventName === "status") { |
83 | | - targetSha = context.payload.sha; |
84 | | - claStatusArgs = ["--sha", targetSha]; |
85 | | - } else if (context.eventName === "pull_request_target") { |
86 | | - const pr = context.payload.pull_request; |
87 | | - targetSha = pr.head.sha; |
88 | | - claStatusArgs = ["--pr", String(pr.number)]; |
89 | | - } else { |
90 | | - core.setFailed(`Unsupported event type: ${context.eventName}`); |
91 | | - return; |
92 | | - } |
93 | | -
|
94 | | - const status = claStatus(claStatusArgs); |
95 | | -
|
96 | | - if (!status.state) { |
97 | | - core.info(`No CLA Gate status to publish for ${targetSha}`); |
98 | | - return; |
99 | | - } |
100 | | -
|
101 | | - await postStatus({ |
102 | | - sha: targetSha, |
103 | | - state: status.state, |
104 | | - description: status.description || undefined, |
105 | | - targetUrl: status.target_url || undefined, |
106 | | - }); |
| 37 | + GH_TOKEN: ${{ github.token }} |
| 38 | + PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }} |
| 39 | + run: | |
| 40 | + set -euo pipefail |
| 41 | +
|
| 42 | + for attempt in $(seq 1 30); do |
| 43 | + status_json="$(gh api "repos/${GITHUB_REPOSITORY}/commits/${PR_HEAD_SHA}/status")" |
| 44 | + cla_status="$(jq -c '.statuses | map(select(.context == "license/cla")) | first // empty' <<<"${status_json}")" |
| 45 | +
|
| 46 | + if [ -z "${cla_status}" ]; then |
| 47 | + echo "license/cla is not present on ${PR_HEAD_SHA} yet." |
| 48 | + else |
| 49 | + state="$(jq -r '.state' <<<"${cla_status}")" |
| 50 | + description="$(jq -r '.description // ""' <<<"${cla_status}")" |
| 51 | + target_url="$(jq -r '.target_url // ""' <<<"${cla_status}")" |
| 52 | +
|
| 53 | + echo "license/cla is ${state}: ${description}" |
| 54 | + case "${state}" in |
| 55 | + success) |
| 56 | + exit 0 |
| 57 | + ;; |
| 58 | + pending) |
| 59 | + ;; |
| 60 | + failure|error) |
| 61 | + if [ -n "${target_url}" ]; then |
| 62 | + echo "::error title=license/cla ${state}::${description} (${target_url})" |
| 63 | + else |
| 64 | + echo "::error title=license/cla ${state}::${description}" |
| 65 | + fi |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | + *) |
| 69 | + echo "::error::Unexpected license/cla state: ${state}" |
| 70 | + exit 1 |
| 71 | + ;; |
| 72 | + esac |
| 73 | + fi |
| 74 | +
|
| 75 | + if [ "${attempt}" -lt 30 ]; then |
| 76 | + sleep 10 |
| 77 | + fi |
| 78 | + done |
| 79 | +
|
| 80 | + echo "::error::Timed out waiting for license/cla to succeed on ${PR_HEAD_SHA}." |
| 81 | + exit 1 |
0 commit comments