|
| 1 | +name: CLA Gate |
| 2 | + |
| 3 | +# This workflow makes CLA checks work with merge queue entries. Merge groups get |
| 4 | +# a repository-owned `CLA Gate` commit status on the synthetic queue SHA, while |
| 5 | +# pull request/status events use this Actions job result based on CLA Assistant's |
| 6 | +# raw `license/cla` status. |
| 7 | +# |
| 8 | +# SECURITY: Pull request runs must still check out trusted base-branch code, not |
| 9 | +# PR code, before running repository scripts. |
| 10 | + |
| 11 | +on: |
| 12 | + pull_request: |
| 13 | + types: [opened, reopened] |
| 14 | + status: |
| 15 | + merge_group: |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | + pull-requests: read |
| 20 | + statuses: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + publish-cla-gate-status: |
| 24 | + name: CLA status |
| 25 | + runs-on: ubuntu-latest |
| 26 | + if: github.event_name != 'status' || github.event.context == 'license/cla' |
| 27 | + |
| 28 | + steps: |
| 29 | + - name: Check out trusted base code |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + ref: ${{ github.event.repository.default_branch }} |
| 33 | + |
| 34 | + - uses: dsherret/rust-toolchain-file@v1 |
| 35 | + |
| 36 | + - name: Publish CLA Gate status |
| 37 | + uses: actions/github-script@v7 |
| 38 | + env: |
| 39 | + GITHUB_TOKEN: ${{ github.token }} |
| 40 | + with: |
| 41 | + script: | |
| 42 | + const { execFileSync } = require("child_process"); |
| 43 | +
|
| 44 | + function claStatus(args) { |
| 45 | + const output = execFileSync("cargo", ["ci", "cla-assistant", "status", ...args], { |
| 46 | + encoding: "utf8", |
| 47 | + env: process.env, |
| 48 | + }); |
| 49 | + return JSON.parse(output); |
| 50 | + } |
| 51 | +
|
| 52 | + async function postStatus({ sha, state, description, targetUrl }) { |
| 53 | + const statusContext = "CLA Gate"; |
| 54 | + core.info( |
| 55 | + `Publishing ${statusContext}=${state} for ${sha}: ${description}` |
| 56 | + ); |
| 57 | +
|
| 58 | + await github.rest.repos.createCommitStatus({ |
| 59 | + owner: context.repo.owner, |
| 60 | + repo: context.repo.repo, |
| 61 | + sha, |
| 62 | + state, |
| 63 | + description, |
| 64 | + context: statusContext, |
| 65 | + target_url: targetUrl, |
| 66 | + }); |
| 67 | + } |
| 68 | +
|
| 69 | + let targetSha; |
| 70 | + let claStatusArgs; |
| 71 | +
|
| 72 | + if (context.eventName === "merge_group") { |
| 73 | + await postStatus({ |
| 74 | + sha: process.env.GITHUB_SHA, |
| 75 | + state: "success", |
| 76 | + description: "Merge group entry; CLA already checked before queue", |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | +
|
| 81 | + if (context.eventName === "status") { |
| 82 | + targetSha = context.payload.sha; |
| 83 | + claStatusArgs = ["--sha", targetSha]; |
| 84 | + } else if (context.eventName === "pull_request") { |
| 85 | + const pr = context.payload.pull_request; |
| 86 | + targetSha = pr.head.sha; |
| 87 | + claStatusArgs = ["--pr", String(pr.number)]; |
| 88 | + } else { |
| 89 | + core.setFailed(`Unsupported event type: ${context.eventName}`); |
| 90 | + return; |
| 91 | + } |
| 92 | +
|
| 93 | + const status = claStatus(claStatusArgs); |
| 94 | +
|
| 95 | + if (!status.state) { |
| 96 | + core.info(`No CLA Gate status to publish for ${targetSha}`); |
| 97 | + return; |
| 98 | + } |
| 99 | +
|
| 100 | + if (status.state === "success") { |
| 101 | + core.info(`license/cla is success for ${targetSha}`); |
| 102 | + return; |
| 103 | + } |
| 104 | +
|
| 105 | + const state = status.state || "missing"; |
| 106 | + const description = status.description || "license/cla status is missing"; |
| 107 | + const targetUrl = status.target_url ? ` (${status.target_url})` : ""; |
| 108 | + core.setFailed(`license/cla is ${state}: ${description}${targetUrl}`); |
0 commit comments