Skip to content

Central driver implementation #16156

Central driver implementation

Central driver implementation #16156

# This workflow removes a stale 'ci-passed' label whenever a PR is updated
# or its eligibility labels change, then adds it back only after the
# 'CI Check' workflow completes successfully for a label-eligible PR.
name: Add CI Passed Label
on:
pull_request_target:
types:
- synchronize
- reopened
- labeled
- unlabeled
workflow_run:
workflows: ["CI Check"]
types:
- completed
permissions:
pull-requests: write
issues: write
jobs:
fetch_data:
name: Fetch PR eligibility
runs-on: ubuntu-latest
if: >
github.event_name == 'workflow_run' &&
github.event.workflow_run.conclusion == 'success'
outputs:
pr_number: ${{ steps.pr.outputs.pr_number }}
eligible: ${{ steps.pr.outputs.eligible }}
steps:
- name: Check PR label eligibility
id: pr
uses: actions/github-script@v8
with:
script: |
const workflowEvent = context.payload.workflow_run.event;
if (workflowEvent !== "pull_request" && workflowEvent !== "pull_request_target") {
core.info(`Skipping workflow_run event ${workflowEvent}`);
core.setOutput("eligible", "false");
return;
}
const headRepositoryOwner = context.payload.workflow_run.head_repository?.owner?.login;
const headBranch = context.payload.workflow_run.head_branch;
const runHeadSha = context.payload.workflow_run.head_sha;
if (!headRepositoryOwner || !headBranch || !runHeadSha) {
core.info("workflow_run is missing head repository, branch, or SHA metadata.");
core.setOutput("eligible", "false");
return;
}
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
head: `${headRepositoryOwner}:${headBranch}`,
});
const pullRequest = pullRequests.find((candidate) => candidate.head.sha === runHeadSha);
if (!pullRequest) {
core.info(
`No open pull request matched ${headRepositoryOwner}:${headBranch} at ${runHeadSha}.`
);
core.setOutput("eligible", "false");
return;
}
const labels = new Set(pullRequest.labels.map((label) => label.name));
const eligible = labels.has("ok-to-test") && !labels.has("needs-ok-to-test");
core.info(`PR #${pullRequest.number} labels: ${Array.from(labels).join(", ")}`);
core.setOutput("pr_number", String(pullRequest.number));
core.setOutput("eligible", eligible ? "true" : "false");
reset_ci_passed_label:
name: Reset stale 'ci-passed' label
runs-on: ubuntu-latest
# Only run on pull_request_target events that can actually affect ci-passed:
# - synchronize/reopened: new commits invalidate previous CI results
# - labeled with 'needs-ok-to-test': PR became ineligible
# - unlabeled with 'ok-to-test': PR became ineligible
# This avoids spinning up a runner for unrelated label changes.
if: >
github.event_name == 'pull_request_target' &&
(
github.event.action == 'synchronize' ||
github.event.action == 'reopened' ||
(github.event.action == 'labeled' && github.event.label.name == 'needs-ok-to-test') ||
(github.event.action == 'unlabeled' && github.event.label.name == 'ok-to-test')
)
steps:
- name: Remove existing 'ci-passed' label
run: |
pr_number=${{ github.event.pull_request.number }}
echo "Checking for stale 'ci-passed' label on PR #${pr_number} after ${{ github.event.action }}"
labels=$(gh pr view "${pr_number}" --repo "$GITHUB_REPOSITORY" --json labels --jq '.labels[].name')
if echo "${labels}" | grep -qx 'ci-passed'; then
echo "Removing stale 'ci-passed' label from PR #${pr_number}"
gh pr edit "${pr_number}" --remove-label "ci-passed" --repo "$GITHUB_REPOSITORY"
else
echo "Label 'ci-passed' not present on PR #${pr_number}; nothing to remove."
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
add_ci_passed_label:
name: Add 'ci-passed' label
runs-on: ubuntu-latest
needs: fetch_data
if: needs.fetch_data.outputs.eligible == 'true'
steps:
- name: Add 'ci-passed' label
run: |
echo "Adding 'ci-passed' label to PR #${{ needs.fetch_data.outputs.pr_number }}"
gh pr edit ${{ needs.fetch_data.outputs.pr_number }} --add-label "ci-passed" --repo "$GITHUB_REPOSITORY"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}