|
| 1 | +name: CLA Gate |
| 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. |
| 8 | + |
| 9 | +on: |
| 10 | + status: |
| 11 | + merge_group: |
| 12 | + |
| 13 | +permissions: |
| 14 | + contents: read |
| 15 | + statuses: write |
| 16 | + |
| 17 | +jobs: |
| 18 | + publish-cla-gate-status: |
| 19 | + name: CLA status |
| 20 | + runs-on: ubuntu-latest |
| 21 | + if: github.event_name != 'status' || github.event.context == 'license/cla' |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Check out trusted base code |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + ref: ${{ github.event.repository.default_branch }} |
| 28 | + |
| 29 | + - uses: dsherret/rust-toolchain-file@v1 |
| 30 | + |
| 31 | + - name: Publish CLA Gate status |
| 32 | + uses: actions/github-script@v7 |
| 33 | + env: |
| 34 | + GITHUB_TOKEN: ${{ github.token }} |
| 35 | + with: |
| 36 | + script: | |
| 37 | + const { execFileSync } = require("child_process"); |
| 38 | +
|
| 39 | + function claStatus(args) { |
| 40 | + const command = ["ci", "cla-assistant", "status", ...args]; |
| 41 | + core.info(`Running cargo ${command.join(" ")}`); |
| 42 | + const output = execFileSync("cargo", command, { |
| 43 | + encoding: "utf8", |
| 44 | + env: process.env, |
| 45 | + }); |
| 46 | + core.info(`cargo ci output: ${output.trim()}`); |
| 47 | + const status = JSON.parse(output); |
| 48 | + core.info( |
| 49 | + `Resolved license/cla for ${status.sha}: state=${status.state || "missing"} ` + |
| 50 | + `description=${status.description || "<none>"} ` + |
| 51 | + `target_url=${status.target_url || "<none>"}` |
| 52 | + ); |
| 53 | + return status; |
| 54 | + } |
| 55 | +
|
| 56 | + async function postStatus({ sha, state, description, targetUrl }) { |
| 57 | + const statusContext = "CLA Gate"; |
| 58 | + core.info( |
| 59 | + `Publishing ${statusContext}=${state} for ${sha}: ${description}` |
| 60 | + ); |
| 61 | +
|
| 62 | + await github.rest.repos.createCommitStatus({ |
| 63 | + owner: context.repo.owner, |
| 64 | + repo: context.repo.repo, |
| 65 | + sha, |
| 66 | + state, |
| 67 | + description, |
| 68 | + context: statusContext, |
| 69 | + target_url: targetUrl, |
| 70 | + }); |
| 71 | + } |
| 72 | +
|
| 73 | + let targetSha; |
| 74 | + let claStatusArgs; |
| 75 | +
|
| 76 | + core.info( |
| 77 | + `Handling ${context.eventName} event for workflow SHA ${process.env.GITHUB_SHA}` |
| 78 | + ); |
| 79 | +
|
| 80 | + if (context.eventName === "merge_group") { |
| 81 | + core.info(`Merge group SHA is ${process.env.GITHUB_SHA}`); |
| 82 | + await postStatus({ |
| 83 | + sha: process.env.GITHUB_SHA, |
| 84 | + state: "success", |
| 85 | + description: "Merge group entry; CLA already checked before queue", |
| 86 | + }); |
| 87 | + return; |
| 88 | + } |
| 89 | +
|
| 90 | + if (context.eventName === "status") { |
| 91 | + targetSha = context.payload.sha; |
| 92 | + claStatusArgs = ["--sha", targetSha]; |
| 93 | + core.info( |
| 94 | + `status event context=${context.payload.context} state=${context.payload.state} sha=${targetSha}` |
| 95 | + ); |
| 96 | + } else { |
| 97 | + core.setFailed(`Unsupported event type: ${context.eventName}`); |
| 98 | + return; |
| 99 | + } |
| 100 | +
|
| 101 | + core.info(`Checking license/cla with args: ${claStatusArgs.join(" ")}`); |
| 102 | + const status = claStatus(claStatusArgs); |
| 103 | +
|
| 104 | + if (!status.state) { |
| 105 | + core.info(`No CLA Gate status to publish for ${targetSha}`); |
| 106 | + return; |
| 107 | + } |
| 108 | +
|
| 109 | + await postStatus({ |
| 110 | + sha: targetSha, |
| 111 | + state: status.state, |
| 112 | + description: status.description || undefined, |
| 113 | + targetUrl: status.target_url || undefined, |
| 114 | + }); |
0 commit comments