diff --git a/.github/workflow-scripts/__tests__/isMetaExportedPullRequest-test.js b/.github/workflow-scripts/__tests__/isMetaExportedPullRequest-test.js new file mode 100644 index 000000000000..5ee443fa5a19 --- /dev/null +++ b/.github/workflow-scripts/__tests__/isMetaExportedPullRequest-test.js @@ -0,0 +1,70 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +'use strict'; + +const isMetaExportedPullRequest = require('../isMetaExportedPullRequest'); + +const exported = { + author_association: 'MEMBER', + body: 'Summary: Remove old flag\n\nDifferential Revision: D134244907\n', + head: {ref: 'export-D134244907'}, + labels: [], +}; + +describe('isMetaExportedPullRequest', () => { + test('recognizes an exported PR before the bot label arrives', () => { + expect(isMetaExportedPullRequest(exported)).toBe(true); + }); + + test('recognizes the bot-applied label even without the revision line', () => { + expect( + isMetaExportedPullRequest({ + ...exported, + body: '', + head: {ref: 'other-branch'}, + author_association: 'NONE', + labels: [{name: 'meta-exported'}], + }), + ).toBe(true); + }); + + test('does not trust a non-member spoofing the body and branch', () => { + for (const author_association of ['NONE', 'CONTRIBUTOR', 'COLLABORATOR']) { + expect(isMetaExportedPullRequest({...exported, author_association})).toBe( + false, + ); + } + }); + + test('requires the revision to match the export branch exactly', () => { + expect( + isMetaExportedPullRequest({ + ...exported, + head: {ref: 'export-D134244908'}, + }), + ).toBe(false); + expect( + isMetaExportedPullRequest({ + ...exported, + body: 'Differential Revision: D134244907\nDifferential Revision: D2', + }), + ).toBe(false); + }); + + test('does not exempt ordinary PRs from members', () => { + expect( + isMetaExportedPullRequest({ + ...exported, + head: {ref: 'fix-android'}, + body: 'Update Markdown documentation', + }), + ).toBe(false); + }); +}); diff --git a/.github/workflow-scripts/isMetaExportedPullRequest.js b/.github/workflow-scripts/isMetaExportedPullRequest.js new file mode 100644 index 000000000000..9cbb5b26e77d --- /dev/null +++ b/.github/workflow-scripts/isMetaExportedPullRequest.js @@ -0,0 +1,35 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +'use strict'; + +function isMetaExportedPullRequest(pr) { + // meta-codesync applies this label, sometimes after the PR is opened. + if (pr.labels?.some(label => label.name === 'meta-exported')) { + return true; + } + + // The PR body and branch are author-controlled. Require a repository member + // as well as a matching Differential revision before skipping the first run. + if (!['OWNER', 'MEMBER'].includes(pr.author_association)) { + return false; + } + const match = /^export-(D[1-9]\d*)$/.exec(pr.head?.ref ?? ''); + if (!match) { + return false; + } + const revisions = [ + ...(pr.body ?? '').matchAll( + /^[ \t]*Differential Revision:[ \t]*(D[1-9]\d*)[ \t]*$/gm, + ), + ]; + return revisions.length === 1 && revisions[0][1] === match[1]; +} + +module.exports = isMetaExportedPullRequest; diff --git a/.github/workflows/meta-import-risk.yml b/.github/workflows/meta-import-risk.yml index fa50bb04204c..50cf00806526 100644 --- a/.github/workflows/meta-import-risk.yml +++ b/.github/workflows/meta-import-risk.yml @@ -2,16 +2,44 @@ name: Meta import risk on: pull_request_target: - types: [opened, reopened, synchronize, labeled] + types: [opened, reopened, synchronize, edited, labeled] concurrency: group: meta-import-risk-${{ github.event.pull_request.number }} cancel-in-progress: true jobs: - classify: + check_export: if: github.repository == 'react/react-native' runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + outputs: + exported: ${{ steps.check.outputs.exported }} + steps: + - name: Checkout trusted base + uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.base.sha }} + persist-credentials: false + # Do this before the model job, including on the PR-opened event: the + # meta-codesync label may not have arrived yet. + - name: Identify Meta-exported pull requests + id: check + uses: actions/github-script@v8 + with: + script: | + const isMetaExportedPullRequest = require('./.github/workflow-scripts/isMetaExportedPullRequest'); + const {data: pr} = await github.rest.pulls.get({ + ...context.repo, pull_number: context.issue.number, + }); + core.setOutput('exported', isMetaExportedPullRequest(pr)); + + classify: + needs: check_export + if: needs.check_export.outputs.exported == 'false' + runs-on: ubuntu-latest timeout-minutes: 10 permissions: contents: read @@ -45,31 +73,56 @@ jobs: } label: - needs: classify + needs: [check_export, classify] # A newer PR event cancels the old run. Do not treat its missing output as # an unknown risk; only an explicit unknown from the classifier is fail-safe. if: >- always() && !cancelled() && github.repository == 'react/react-native' && - contains(fromJSON('["low","high","unknown"]'), needs.classify.outputs.risk) + (needs.check_export.outputs.exported == 'true' || + (needs.check_export.outputs.exported == 'false' && + contains(fromJSON('["low","high","unknown"]'), needs.classify.outputs.risk))) runs-on: ubuntu-latest permissions: + contents: read # Applying a label to a PR requires PR write access, even though the # REST endpoint is under issues. Keep this out of the classifier job. pull-requests: write issues: write steps: - # This job has write access but no model secret and no PR checkout. + # Only trusted base code is checked out; this job has no model secret. + - name: Checkout trusted base + uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.base.sha }} + persist-credentials: false - name: Apply Meta Review Required when needed uses: actions/github-script@v8 env: + EXPORTED: ${{ needs.check_export.outputs.exported }} RISK: ${{ needs.classify.outputs.risk }} with: script: | + const isMetaExportedPullRequest = require('./.github/workflow-scripts/isMetaExportedPullRequest'); const owner = context.repo.owner; const repo = context.repo.repo; const number = context.issue.number; const {data: pr} = await github.rest.pulls.get({owner, repo, pull_number: number}); const labels = pr.labels.map(label => label.name); + // Re-check current metadata in case meta-codesync labeled the PR + // after the first check. An exported PR needs neither classification + // nor a lingering Meta Review Required label. + if (isMetaExportedPullRequest(pr)) { + if (labels.includes('Meta Review Required')) { + await github.rest.issues.removeLabel({ + owner, repo, issue_number: number, name: 'Meta Review Required', + }); + } + return; + } + if (process.env.EXPORTED === 'true') { + core.setFailed('Export status changed after preflight; wait for a new PR event to classify it.'); + return; + } const stale = pr.head.sha !== context.payload.pull_request.head.sha; if (process.env.RISK === 'low' && !stale && !labels.includes('high-risk')) { return;