The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Replace CODEOWNERS with a CI approval check for sensitive paths (#28497)
Félix Malfaitb6de383a32d4
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERSdeleted file mode 100644index 41c1f09d343..00000000000--- a/.github/CODEOWNERS+++ /dev/null@@ -1,9 +0,0 @@-/.github/ @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.github/CODEOWNERS @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.github/workflows/ @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.github/actions/ @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.github/dependabot.yml @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.yarnrc.yml @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.claude/ @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/.mcp.json @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrp-/CLAUDE.md @charlesBochet @Weiko @prastoin @bosiraphael @etiennejouan @ijreilly @martmull @thomtrpdiff --git a/.github/workflows/ci-sensitive-paths-approval.yaml b/.github/workflows/ci-sensitive-paths-approval.yamlnew file mode 100644index 00000000000..72d31901ba0--- /dev/null+++ b/.github/workflows/ci-sensitive-paths-approval.yaml@@ -0,0 +1,167 @@+name: Sensitive Paths Approval++# Requires an approval from a maintainer when a PR touches CI, supply-chain or+# agent configuration, without requesting anyone's review. Reports the+# "Sensitive paths approval" commit status, which the main ruleset requires.+on:+ pull_request_target:+ types: [opened, synchronize, reopened, ready_for_review]+ workflow_run:+ workflows: ['PR Review Relay']+ types: [completed]+ merge_group:+ # Backfills the status on every open PR, e.g. right after the ruleset starts requiring it.+ workflow_dispatch:++permissions:+ contents: read+ pull-requests: read+ statuses: write++concurrency:+ group: sensitive-paths-approval-${{ github.event.pull_request.head.sha || github.event.workflow_run.head_sha || github.event.merge_group.head_sha || github.run_id }}++jobs:+ evaluate:+ runs-on: ubuntu-latest+ timeout-minutes: 10+ steps:+ - uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7.1.0+ with:+ script: |+ const STATUS_CONTEXT = 'Sensitive paths approval';+ const SENSITIVE_PATH_PREFIXES = ['.github/', '.claude/'];+ const SENSITIVE_FILES = ['.yarnrc.yml', '.mcp.json', 'CLAUDE.md'];+ const APPROVERS = [+ 'charlesBochet',+ 'Weiko',+ 'prastoin',+ 'bosiraphael',+ 'etiennejouan',+ 'ijreilly',+ 'martmull',+ 'thomtrp',+ ].map((login) => login.toLowerCase());++ const { owner, repo } = context.repo;++ const setStatus = (sha, state, description) =>+ github.rest.repos.createCommitStatus({+ owner,+ repo,+ sha,+ state,+ context: STATUS_CONTEXT,+ description,+ target_url: `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}`,+ });++ const isSensitive = (path) =>+ SENSITIVE_FILES.includes(path) ||+ SENSITIVE_PATH_PREFIXES.some((prefix) => path.startsWith(prefix));++ const sensitivePathsIn = (files) =>+ files+ .flatMap((file) => [file.filename, file.previous_filename])+ .filter((path) => path && isSensitive(path));++ // Ignores files only brought in by merging the base branch, which are not in the PR diff.+ const approvalCoversHead = async (approvedSha, headSha, sensitivePaths) => {+ if (approvedSha === headSha) return true;+ try {+ const files = await github.paginate(+ github.rest.repos.compareCommitsWithBasehead,+ { owner, repo, basehead: `${approvedSha}...${headSha}`, per_page: 100 },+ (response) => response.data.files ?? [],+ );+ return !sensitivePathsIn(files).some((path) => sensitivePaths.includes(path));+ } catch (error) {+ core.info(`Could not compare ${approvedSha} with ${headSha}: ${error.message}`);+ return false;+ }+ };++ const evaluate = async (pullNumber) => {+ const { data: pull } = await github.rest.pulls.get({ owner, repo, pull_number: pullNumber });+ const headSha = pull.head.sha;++ const files = await github.paginate(github.rest.pulls.listFiles, {+ owner,+ repo,+ pull_number: pullNumber,+ per_page: 100,+ });+ const sensitivePaths = sensitivePathsIn(files);++ if (sensitivePaths.length === 0) {+ await setStatus(headSha, 'success', 'No sensitive paths changed');+ return;+ }++ core.info(`#${pullNumber} sensitive paths changed:\n${[...new Set(sensitivePaths)].join('\n')}`);++ const reviews = await github.paginate(github.rest.pulls.listReviews, {+ owner,+ repo,+ pull_number: pullNumber,+ per_page: 100,+ });++ const latestReviewByApprover = new Map();+ for (const review of reviews) {+ const login = review.user?.login;+ if (!login || !APPROVERS.includes(login.toLowerCase())) continue;+ if (review.state === 'COMMENTED' || review.state === 'PENDING') continue;+ latestReviewByApprover.set(login, review);+ }++ for (const [login, review] of latestReviewByApprover) {+ if (review.state !== 'APPROVED') continue;+ if (await approvalCoversHead(review.commit_id, headSha, sensitivePaths)) {+ await setStatus(headSha, 'success', `Approved by ${login}`);+ return;+ }+ }++ await setStatus(headSha, 'failure', 'Sensitive paths changed: needs a maintainer approval');+ };++ // A PR only enters the queue once its own status is green.+ if (context.eventName === 'merge_group') {+ await setStatus(context.payload.merge_group.head_sha, 'success', 'Checked on the pull request');+ return;+ }++ if (context.eventName === 'workflow_dispatch') {+ const pulls = await github.paginate(github.rest.pulls.list, { owner, repo, state: 'open', per_page: 100 });+ for (const pull of pulls) {+ await evaluate(pull.number);+ }+ return;+ }++ let pullNumber = context.payload.pull_request?.number;++ if (context.eventName === 'workflow_run') {+ const run = context.payload.workflow_run;+ pullNumber = run.pull_requests?.[0]?.number;++ // pull_requests is empty for fork PRs+ if (!pullNumber) {+ const { data: pulls } = await github.rest.pulls.list({+ owner,+ repo,+ state: 'open',+ head: `${run.head_repository.owner.login}:${run.head_branch}`,+ per_page: 1,+ });+ pullNumber = pulls[0]?.number;+ }+ }++ if (!pullNumber) {+ core.info('No open pull request found for this event');+ return;+ }++ await evaluate(pullNumber);diff --git a/.github/workflows/pr-review-relay.yaml b/.github/workflows/pr-review-relay.yamlnew file mode 100644index 00000000000..556c6a348da--- /dev/null+++ b/.github/workflows/pr-review-relay.yaml@@ -0,0 +1,18 @@+name: PR Review Relay++# pull_request_review runs the PR's own copy of a workflow, so the sensitive+# paths gate listens to this run via workflow_run and evaluates from main.+on:+ pull_request_review:+ types: [submitted, dismissed]++permissions: {}++jobs:+ relay:+ runs-on: ubuntu-latest+ timeout-minutes: 2+ steps:+ - run: 'echo "Review event on PR $PR_NUMBER"'+ env:+ PR_NUMBER: ${{ github.event.pull_request.number }}