Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
});
});
35 changes: 35 additions & 0 deletions .github/workflow-scripts/isMetaExportedPullRequest.js
Original file line number Diff line number Diff line change
@@ -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;
63 changes: 58 additions & 5 deletions .github/workflows/meta-import-risk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
Loading