From 1ee6fdc57100907ef354be84645e01783245d455 Mon Sep 17 00:00:00 2001 From: Emily Brown Date: Fri, 6 Feb 2026 08:14:53 -0800 Subject: [PATCH] Add branch targeting check to analyze-pr workflow (#55383) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/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 --- .github/workflow-scripts/checkBranchTarget.js | 40 +++++++++++++++++++ .github/workflows/analyze-pr.yml | 25 +++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 .github/workflow-scripts/checkBranchTarget.js diff --git a/.github/workflow-scripts/checkBranchTarget.js b/.github/workflow-scripts/checkBranchTarget.js new file mode 100644 index 00000000000..78e95a5e814 --- /dev/null +++ b/.github/workflow-scripts/checkBranchTarget.js @@ -0,0 +1,40 @@ +/** + * 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'; + +/** + * Validates that the PR targets an appropriate base branch. + * + * @param {string} baseRef - The base branch ref (e.g., 'main', '0.76-stable') + * @returns {{message: string, status: 'PASS'|'FAIL', shouldAddPickLabel: boolean}} + */ +function checkBranchTarget(baseRef) { + const isMain = baseRef === 'main'; + const isStable = baseRef.endsWith('-stable'); + + let message = ''; + let status = 'PASS'; + if (!isMain && !isStable) { + status = 'FAIL'; + message = `> [!CAUTION] +> **Invalid Base Branch** +> +> The base branch for this PR is \`${baseRef}\`, which is not \`main\` or a \`-stable\` branch. +> [Are you sure you want to target this branch?](https://reactnative.dev/docs/contributing#pull-requests)`; + } + + return { + message, + status, + shouldAddPickLabel: isStable, + }; +} + +module.exports = checkBranchTarget; diff --git a/.github/workflows/analyze-pr.yml b/.github/workflows/analyze-pr.yml index 191d244ca3e..5bb2a4d5d28 100644 --- a/.github/workflows/analyze-pr.yml +++ b/.github/workflows/analyze-pr.yml @@ -6,6 +6,7 @@ on: permissions: pull-requests: write + issues: write jobs: analyze-pr: @@ -25,13 +26,33 @@ jobs: script: | const validatePRBody = require('./.github/workflow-scripts/validatePRBody.js'); const {message, status} = validatePRBody(context.payload.pull_request.body); + core.setOutput('message', message); + core.setOutput('status', status); + - name: Check branch target + id: check-branch-target + uses: actions/github-script@v8 + with: + script: | + const checkBranchTarget = require('./.github/workflow-scripts/checkBranchTarget.js'); + const baseRef = context.payload.pull_request.base.ref; + const {message, status, shouldAddPickLabel} = checkBranchTarget(baseRef); + + if (shouldAddPickLabel) { + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + labels: ['Pick Request'], + }); + } + core.setOutput('message', message); core.setOutput('status', status); - name: Post PR comment uses: ./.github/actions/post-pr-comment with: marker: '' - sections: '[${{ toJSON(steps.check-pr-body.outputs.message) }}]' + sections: '[${{ toJSON(steps.check-pr-body.outputs.message) }}, ${{ toJSON(steps.check-branch-target.outputs.message) }}]' - name: Fail if validation errors - if: steps.check-pr-body.outputs.status == 'FAIL' + if: steps.check-pr-body.outputs.status == 'FAIL' || steps.check-branch-target.outputs.status == 'FAIL' run: exit 1