Skip to content

Commit 6407b5e

Browse files
authored
Skip Meta import triage for exported Differential revisions (#60330)
1 parent ce56c6c commit 6407b5e

3 files changed

Lines changed: 163 additions & 5 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
const isMetaExportedPullRequest = require('../isMetaExportedPullRequest');
13+
14+
const exported = {
15+
author_association: 'MEMBER',
16+
body: 'Summary: Remove old flag\n\nDifferential Revision: D134244907\n',
17+
head: {ref: 'export-D134244907'},
18+
labels: [],
19+
};
20+
21+
describe('isMetaExportedPullRequest', () => {
22+
test('recognizes an exported PR before the bot label arrives', () => {
23+
expect(isMetaExportedPullRequest(exported)).toBe(true);
24+
});
25+
26+
test('recognizes the bot-applied label even without the revision line', () => {
27+
expect(
28+
isMetaExportedPullRequest({
29+
...exported,
30+
body: '',
31+
head: {ref: 'other-branch'},
32+
author_association: 'NONE',
33+
labels: [{name: 'meta-exported'}],
34+
}),
35+
).toBe(true);
36+
});
37+
38+
test('does not trust a non-member spoofing the body and branch', () => {
39+
for (const author_association of ['NONE', 'CONTRIBUTOR', 'COLLABORATOR']) {
40+
expect(isMetaExportedPullRequest({...exported, author_association})).toBe(
41+
false,
42+
);
43+
}
44+
});
45+
46+
test('requires the revision to match the export branch exactly', () => {
47+
expect(
48+
isMetaExportedPullRequest({
49+
...exported,
50+
head: {ref: 'export-D134244908'},
51+
}),
52+
).toBe(false);
53+
expect(
54+
isMetaExportedPullRequest({
55+
...exported,
56+
body: 'Differential Revision: D134244907\nDifferential Revision: D2',
57+
}),
58+
).toBe(false);
59+
});
60+
61+
test('does not exempt ordinary PRs from members', () => {
62+
expect(
63+
isMetaExportedPullRequest({
64+
...exported,
65+
head: {ref: 'fix-android'},
66+
body: 'Update Markdown documentation',
67+
}),
68+
).toBe(false);
69+
});
70+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
function isMetaExportedPullRequest(pr) {
13+
// meta-codesync applies this label, sometimes after the PR is opened.
14+
if (pr.labels?.some(label => label.name === 'meta-exported')) {
15+
return true;
16+
}
17+
18+
// The PR body and branch are author-controlled. Require a repository member
19+
// as well as a matching Differential revision before skipping the first run.
20+
if (!['OWNER', 'MEMBER'].includes(pr.author_association)) {
21+
return false;
22+
}
23+
const match = /^export-(D[1-9]\d*)$/.exec(pr.head?.ref ?? '');
24+
if (!match) {
25+
return false;
26+
}
27+
const revisions = [
28+
...(pr.body ?? '').matchAll(
29+
/^[ \t]*Differential Revision:[ \t]*(D[1-9]\d*)[ \t]*$/gm,
30+
),
31+
];
32+
return revisions.length === 1 && revisions[0][1] === match[1];
33+
}
34+
35+
module.exports = isMetaExportedPullRequest;

‎.github/workflows/meta-import-risk.yml‎

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,44 @@ name: Meta import risk
22

33
on:
44
pull_request_target:
5-
types: [opened, reopened, synchronize, labeled]
5+
types: [opened, reopened, synchronize, edited, labeled]
66

77
concurrency:
88
group: meta-import-risk-${{ github.event.pull_request.number }}
99
cancel-in-progress: true
1010

1111
jobs:
12-
classify:
12+
check_export:
1313
if: github.repository == 'react/react-native'
1414
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
pull-requests: read
18+
outputs:
19+
exported: ${{ steps.check.outputs.exported }}
20+
steps:
21+
- name: Checkout trusted base
22+
uses: actions/checkout@v6
23+
with:
24+
ref: ${{ github.event.pull_request.base.sha }}
25+
persist-credentials: false
26+
# Do this before the model job, including on the PR-opened event: the
27+
# meta-codesync label may not have arrived yet.
28+
- name: Identify Meta-exported pull requests
29+
id: check
30+
uses: actions/github-script@v8
31+
with:
32+
script: |
33+
const isMetaExportedPullRequest = require('./.github/workflow-scripts/isMetaExportedPullRequest');
34+
const {data: pr} = await github.rest.pulls.get({
35+
...context.repo, pull_number: context.issue.number,
36+
});
37+
core.setOutput('exported', isMetaExportedPullRequest(pr));
38+
39+
classify:
40+
needs: check_export
41+
if: needs.check_export.outputs.exported == 'false'
42+
runs-on: ubuntu-latest
1543
timeout-minutes: 10
1644
permissions:
1745
contents: read
@@ -45,31 +73,56 @@ jobs:
4573
}
4674
4775
label:
48-
needs: classify
76+
needs: [check_export, classify]
4977
# A newer PR event cancels the old run. Do not treat its missing output as
5078
# an unknown risk; only an explicit unknown from the classifier is fail-safe.
5179
if: >-
5280
always() && !cancelled() && github.repository == 'react/react-native' &&
53-
contains(fromJSON('["low","high","unknown"]'), needs.classify.outputs.risk)
81+
(needs.check_export.outputs.exported == 'true' ||
82+
(needs.check_export.outputs.exported == 'false' &&
83+
contains(fromJSON('["low","high","unknown"]'), needs.classify.outputs.risk)))
5484
runs-on: ubuntu-latest
5585
permissions:
86+
contents: read
5687
# Applying a label to a PR requires PR write access, even though the
5788
# REST endpoint is under issues. Keep this out of the classifier job.
5889
pull-requests: write
5990
issues: write
6091
steps:
61-
# This job has write access but no model secret and no PR checkout.
92+
# Only trusted base code is checked out; this job has no model secret.
93+
- name: Checkout trusted base
94+
uses: actions/checkout@v6
95+
with:
96+
ref: ${{ github.event.pull_request.base.sha }}
97+
persist-credentials: false
6298
- name: Apply Meta Review Required when needed
6399
uses: actions/github-script@v8
64100
env:
101+
EXPORTED: ${{ needs.check_export.outputs.exported }}
65102
RISK: ${{ needs.classify.outputs.risk }}
66103
with:
67104
script: |
105+
const isMetaExportedPullRequest = require('./.github/workflow-scripts/isMetaExportedPullRequest');
68106
const owner = context.repo.owner;
69107
const repo = context.repo.repo;
70108
const number = context.issue.number;
71109
const {data: pr} = await github.rest.pulls.get({owner, repo, pull_number: number});
72110
const labels = pr.labels.map(label => label.name);
111+
// Re-check current metadata in case meta-codesync labeled the PR
112+
// after the first check. An exported PR needs neither classification
113+
// nor a lingering Meta Review Required label.
114+
if (isMetaExportedPullRequest(pr)) {
115+
if (labels.includes('Meta Review Required')) {
116+
await github.rest.issues.removeLabel({
117+
owner, repo, issue_number: number, name: 'Meta Review Required',
118+
});
119+
}
120+
return;
121+
}
122+
if (process.env.EXPORTED === 'true') {
123+
core.setFailed('Export status changed after preflight; wait for a new PR event to classify it.');
124+
return;
125+
}
73126
const stale = pr.head.sha !== context.payload.pull_request.head.sha;
74127
if (process.env.RISK === 'low' && !stale && !labels.includes('high-risk')) {
75128
return;

0 commit comments

Comments
 (0)