Repository navigation
192 lines (187 loc) · 8.43 KB
/
Copy pathmeta-import-risk.yml
File metadata and controls
192 lines (187 loc) · 8.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
name: Meta import risk
on:
pull_request_target:
types: [opened, reopened, synchronize, edited, labeled]
branches: [main]
concurrency:
group: meta-import-risk-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
check_exemptions:
if: github.repository == 'react/react-native'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
exported: ${{ steps.check.outputs.exported }}
build_tooling: ${{ steps.check.outputs.build_tooling }}
steps:
- name: Checkout trusted base
uses: actions/checkout@v6
with:
# The PR payload's base.sha can predate the workflow and its helper scripts.
ref: ${{ github.event.pull_request.base.ref }}
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 PRs exempt from Meta import risk triage
id: check
uses: actions/github-script@v8
with:
script: |
const isMetaExportedPullRequest = require('./.github/workflow-scripts/isMetaExportedPullRequest');
const buildToolingOnly = require('./.github/workflow-scripts/buildToolingOnly');
const {data: pr} = await github.rest.pulls.get({
...context.repo, pull_number: context.issue.number,
});
const exported = isMetaExportedPullRequest(pr);
core.setOutput('exported', exported);
if (exported) {
core.setOutput('build_tooling', 'none');
return;
}
// On a transient file-list error, let the classifier retry and
// conservatively report an unknown risk if it cannot inspect the PR.
let category = null;
try {
const files = await github.paginate(github.rest.pulls.listFiles, {
...context.repo, pull_number: context.issue.number, per_page: 100,
});
category = buildToolingOnly(files, pr.labels);
} catch {
core.warning('Could not inspect changed paths for build-tooling exemption.');
}
core.setOutput('build_tooling', category ?? 'none');
classify:
needs: check_exemptions
if: >-
needs.check_exemptions.outputs.exported == 'false' &&
needs.check_exemptions.outputs.build_tooling == 'none'
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
pull-requests: read
outputs:
risk: ${{ steps.assess.outputs.risk }}
steps:
# pull_request_target has access to the model secret even for fork PRs.
# Only the current trusted base branch is checked out; PR code is read as data
# through the GitHub API and is never executed on this runner.
- name: Checkout trusted base
uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.base.ref }}
persist-credentials: false
- name: Assess changed files and patch with Muse
id: assess
uses: actions/github-script@v8
env:
META_API_KEY: ${{ secrets.EXPO_CODE_REVIEW_API_KEY }}
with:
script: |
const {assessPullRequest} = require('./.github/workflow-scripts/metaImportRisk');
const result = await assessPullRequest({
github, context, apiKey: process.env.META_API_KEY,
});
core.setOutput('risk', result.status);
core.info(`Meta import risk: ${result.status}`);
// Log only the validated, bounded decision and failure category.
// Never log the raw patch, model response, or API credential.
core.info(`Meta import Muse trace: ${JSON.stringify(result.trace)}`);
if (!result.aiComplete) {
core.setFailed('Muse did not assess the complete PR diff; the labeling job will conservatively request Meta review.');
}
label:
needs: [check_exemptions, 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' &&
(needs.check_exemptions.outputs.exported == 'true' ||
contains(fromJSON('["gradle","cocoapods"]'), needs.check_exemptions.outputs.build_tooling) ||
(needs.check_exemptions.outputs.exported == 'false' &&
needs.check_exemptions.outputs.build_tooling == 'none' &&
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:
# 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.ref }}
persist-credentials: false
- name: Apply Meta Review Required when needed
uses: actions/github-script@v8
env:
EXPORTED: ${{ needs.check_exemptions.outputs.exported }}
BUILD_TOOLING: ${{ needs.check_exemptions.outputs.build_tooling }}
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;
}
if (process.env.BUILD_TOOLING !== 'none' && !labels.includes('high-risk')) {
if (pr.head.sha !== context.payload.pull_request.head.sha) {
core.setFailed('PR head changed after build-tooling preflight; wait for a new PR event.');
return;
}
if (labels.includes('Meta Review Required')) {
await github.rest.issues.removeLabel({
owner, repo, issue_number: number, name: 'Meta Review Required',
});
}
return;
}
const stale = pr.head.sha !== context.payload.pull_request.head.sha;
if (process.env.RISK === 'low' && !stale && !labels.includes('high-risk')) {
return;
}
if (labels.includes('Meta Review Required')) {
return;
}
// The destination label does not yet exist in every repository.
// Create it once on first use, then apply it idempotently.
try {
await github.rest.issues.getLabel({owner, repo, name: 'Meta Review Required'});
} catch (error) {
if (error.status !== 404) throw error;
try {
await github.rest.issues.createLabel({
owner, repo, name: 'Meta Review Required', color: 'b60205',
description: 'This PR is considered risky and requires Meta to import it to do more testing before merging.',
});
} catch (createError) {
// A concurrent PR may have created the same label.
if (createError.status !== 422) throw createError;
}
}
await github.rest.issues.addLabels({
owner, repo, issue_number: number, labels: ['Meta Review Required'],
});