Repository navigation
test: released cMCP/cA2A remote peer and TRACE software evidence for #199 #814
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Require maintainer approval | |
| on: | |
| pull_request_target: | |
| types: [opened, synchronize, reopened] | |
| branches: [main] | |
| # Re-evaluate the gate when a review is submitted or dismissed so an | |
| # approval clears the failing status without waiting for a new push. | |
| # Note: pull_request_review does not support a branches filter, so the | |
| # base-branch check lives in the job-level `if` below. | |
| pull_request_review: | |
| types: [submitted, dismissed] | |
| permissions: | |
| contents: read | |
| pull-requests: read | |
| statuses: write | |
| jobs: | |
| gate: | |
| runs-on: ubuntu-latest | |
| if: github.event.pull_request.base.ref == 'main' | |
| steps: | |
| - name: Check for maintainer approval | |
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | |
| with: | |
| script: | | |
| const { data: pr } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number, | |
| }); | |
| // Read only reviewed base-branch data. Never execute or read policy from a PR head. | |
| const readPolicy = async ref => (await github.rest.repos.getContent({ | |
| owner: context.repo.owner, repo: context.repo.repo, | |
| path: '.github/maintainers.json', ref, | |
| })).data; | |
| let file; | |
| try { | |
| file = await readPolicy(pr.base.sha); | |
| } catch (error) { | |
| if (error.status !== 404) throw error; | |
| // A pull request opened before the policy file landed keeps that older base.sha, | |
| // so read the policy at the tip of its base branch, which is reviewed data too. | |
| // Qualified, so a tag with the branch's name cannot stand in for it. | |
| try { | |
| file = await readPolicy('refs/heads/' + pr.base.ref); | |
| } catch (error) { | |
| // One-commit bootstrap: the initial policy PR must be reviewable before its | |
| // policy file exists on main. Missing policy on any later base fails closed. | |
| if (error.status !== 404 || pr.base.sha !== "06c1dd113d86ae94844bcc8840b5c8c78bc62e83") throw error; | |
| file = { encoding: 'base64', content: Buffer.from(JSON.stringify({"schema":1,"repository":"integrations","source":"agentrust-io/.github:maintainers/roster.json","maintainers":["imran-siddique","carloshvp","rajnisht7"],"security_paths":[],"security_approvals":1})).toString('base64') }; | |
| } | |
| } | |
| if (Array.isArray(file) || file.encoding !== 'base64') { | |
| throw new Error('Missing base-branch maintainer policy'); | |
| } | |
| const policy = JSON.parse(Buffer.from(file.content, 'base64').toString('utf8')); | |
| if (policy.schema !== 1 || policy.repository !== context.repo.repo || | |
| !Array.isArray(policy.maintainers) || policy.maintainers.length < 2 || | |
| !policy.maintainers.every(x => typeof x === 'string' && /^[A-Za-z0-9-]+$/.test(x)) || | |
| new Set(policy.maintainers.map(x => x.toLowerCase())).size !== policy.maintainers.length || | |
| !Array.isArray(policy.security_paths) || | |
| !policy.security_paths.every(x => typeof x === 'string' && x.endsWith('/') && !x.startsWith('/') && !x.includes('..')) || | |
| policy.security_approvals !== (policy.security_paths.length ? 2 : 1)) { | |
| throw new Error('Invalid base-branch maintainer policy'); | |
| } | |
| const maintainers = new Set(policy.maintainers.map(x => x.toLowerCase())); | |
| let securityChange = false; | |
| if (policy.security_paths.length) { | |
| const files = await github.paginate(github.rest.pulls.listFiles, { | |
| owner: context.repo.owner, repo: context.repo.repo, pull_number: pr.number, | |
| }); | |
| if (files.length !== pr.changed_files) throw new Error('Incomplete changed-file inventory'); | |
| // Renaming a security file out of its directory must still require two reviews. | |
| securityChange = files.some(f => [f.filename, f.previous_filename] | |
| .some(p => typeof p === 'string' && policy.security_paths.some(prefix => p.startsWith(prefix)))); | |
| } | |
| const author = pr.user.login.toLowerCase(); | |
| if (!securityChange && maintainers.has(author)) { | |
| core.info('Maintainer-authored routine change: branch rules still require independent review.'); | |
| return; | |
| } | |
| const reviews = await github.paginate(github.rest.pulls.listReviews, { | |
| owner: context.repo.owner, repo: context.repo.repo, pull_number: pr.number, | |
| }); | |
| const latest = new Map(); | |
| for (const review of reviews.slice().sort((a, b) => | |
| Date.parse(a.submitted_at || 0) - Date.parse(b.submitted_at || 0) || a.id - b.id)) { | |
| const login = review.user?.login?.toLowerCase(); | |
| if (review.user?.type === 'User' && maintainers.has(login) && | |
| login !== author && review.state !== 'COMMENTED') { | |
| latest.set(login, review); | |
| } | |
| } | |
| const approvals = [...latest.values()].filter(r => | |
| r.state === 'APPROVED' && r.commit_id === pr.head.sha); | |
| const required = securityChange ? policy.security_approvals : 1; | |
| if (approvals.length < required) { | |
| core.setFailed('Awaiting ' + required + ' independent maintainer approval(s) of current head ' + | |
| pr.head.sha + '; found ' + approvals.length + '. Branch rules and specification governance also apply.'); | |
| } else { | |
| core.info(approvals.length + ' maintainer(s) approved current head ' + pr.head.sha); | |
| } |