Skip to content

Commit 1ee6fdc

Browse files
emily8rownmeta-codesync[bot]
authored andcommitted
Add branch targeting check to analyze-pr workflow (#55383)
Summary: Pull Request resolved: #55383 Adds a branch targeting validation that replaces the equivalent Danger.js check. The check validates that PRs target either `main` or a `-stable` branch, and automatically adds the "Pick Request" label when targeting stable branches. - Created `checkBranchTarget.js` - pure function returning validation message - Added check step to `annotate-pr.yml` that calls the JS and adds labels - Added `issues: write` permission for label functionality Changelog: [Internal] Reviewed By: huntie Differential Revision: D91685081 fbshipit-source-id: 87e7124f7d825b51cb791dc94720c487ff95d414
1 parent 5e41043 commit 1ee6fdc

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
13+
* Validates that the PR targets an appropriate base branch.
14+
*
15+
* @param {string} baseRef - The base branch ref (e.g., 'main', '0.76-stable')
16+
* @returns {{message: string, status: 'PASS'|'FAIL', shouldAddPickLabel: boolean}}
17+
*/
18+
function checkBranchTarget(baseRef) {
19+
const isMain = baseRef === 'main';
20+
const isStable = baseRef.endsWith('-stable');
21+
22+
let message = '';
23+
let status = 'PASS';
24+
if (!isMain && !isStable) {
25+
status = 'FAIL';
26+
message = `> [!CAUTION]
27+
> **Invalid Base Branch**
28+
>
29+
> The base branch for this PR is \`${baseRef}\`, which is not \`main\` or a \`-stable\` branch.
30+
> [Are you sure you want to target this branch?](https://reactnative.dev/docs/contributing#pull-requests)`;
31+
}
32+
33+
return {
34+
message,
35+
status,
36+
shouldAddPickLabel: isStable,
37+
};
38+
}
39+
40+
module.exports = checkBranchTarget;

.github/workflows/analyze-pr.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66

77
permissions:
88
pull-requests: write
9+
issues: write
910

1011
jobs:
1112
analyze-pr:
@@ -25,13 +26,33 @@ jobs:
2526
script: |
2627
const validatePRBody = require('./.github/workflow-scripts/validatePRBody.js');
2728
const {message, status} = validatePRBody(context.payload.pull_request.body);
29+
core.setOutput('message', message);
30+
core.setOutput('status', status);
31+
- name: Check branch target
32+
id: check-branch-target
33+
uses: actions/github-script@v8
34+
with:
35+
script: |
36+
const checkBranchTarget = require('./.github/workflow-scripts/checkBranchTarget.js');
37+
const baseRef = context.payload.pull_request.base.ref;
38+
const {message, status, shouldAddPickLabel} = checkBranchTarget(baseRef);
39+
40+
if (shouldAddPickLabel) {
41+
await github.rest.issues.addLabels({
42+
owner: context.repo.owner,
43+
repo: context.repo.repo,
44+
issue_number: context.issue.number,
45+
labels: ['Pick Request'],
46+
});
47+
}
48+
2849
core.setOutput('message', message);
2950
core.setOutput('status', status);
3051
- name: Post PR comment
3152
uses: ./.github/actions/post-pr-comment
3253
with:
3354
marker: '<!-- analyze-pr -->'
34-
sections: '[${{ toJSON(steps.check-pr-body.outputs.message) }}]'
55+
sections: '[${{ toJSON(steps.check-pr-body.outputs.message) }}, ${{ toJSON(steps.check-branch-target.outputs.message) }}]'
3556
- name: Fail if validation errors
36-
if: steps.check-pr-body.outputs.status == 'FAIL'
57+
if: steps.check-pr-body.outputs.status == 'FAIL' || steps.check-branch-target.outputs.status == 'FAIL'
3758
run: exit 1

0 commit comments

Comments
 (0)