From adc28a0b60c49d017f4ad3fad88db37cf3b5e065 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Wed, 21 Oct 2026 07:43:13 -0700 Subject: [PATCH] Accept stacked PR base branches in analyze-pr (#59109) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: The `analyze_pr` check fails every PR in a stack above the first (example: https://github.com/react/react-native/pull/58867), since each targets the branch of the PR below it rather than `main` or a `-stable` branch. - Pass the branch target check when the base branch is the head of another open PR in the repo. - Other base branches still fail as before, and the parent PR's own check still guards its target. NOTE: The workflow runs on `pull_request_target` from `main`, so this takes effect only after it lands. Changelog: [Internal] Pull Request resolved: https://github.com/react/react-native/pull/59109 Test Plan: ``` yarn jest .github/workflow-scripts/__tests__/checkBranchTarget-test.js ``` Checked the lookup the workflow makes against GitHub: ``` gh api 'repos/react/react-native/pulls?state=open&head=react:pr58950' ``` ✅ Returns https://github.com/react/react-native/issues/58950, so https://github.com/react/react-native/issues/58951 (based on `pr58950`) will pass. ℹ️ A non-PR branch returns no results and still fails. Reviewed By: christophpurrer Differential Revision: D125867260 Pulled By: Abbondanzo fbshipit-source-id: e30b4768525f5c4938e782231b9b9fc435953f42 --- .../__tests__/checkBranchTarget-test.js | 47 +++++++++++++++++++ .github/workflow-scripts/checkBranchTarget.js | 6 ++- .github/workflows/analyze-pr.yml | 7 +++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 .github/workflow-scripts/__tests__/checkBranchTarget-test.js diff --git a/.github/workflow-scripts/__tests__/checkBranchTarget-test.js b/.github/workflow-scripts/__tests__/checkBranchTarget-test.js new file mode 100644 index 00000000000..3524fd39f6c --- /dev/null +++ b/.github/workflow-scripts/__tests__/checkBranchTarget-test.js @@ -0,0 +1,47 @@ +/** + * 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 checkBranchTarget = require('../checkBranchTarget'); + +describe('checkBranchTarget', () => { + test('passes for main', () => { + expect(checkBranchTarget('main')).toEqual({ + message: '', + status: 'PASS', + shouldAddPickLabel: false, + }); + }); + + test('passes for a stable branch and adds the pick label', () => { + expect(checkBranchTarget('0.76-stable')).toEqual({ + message: '', + status: 'PASS', + shouldAddPickLabel: true, + }); + }); + + test('passes for the head branch of another open PR', () => { + expect(checkBranchTarget('pr58950', true)).toEqual({ + message: '', + status: 'PASS', + shouldAddPickLabel: false, + }); + }); + + test('fails for any other branch', () => { + const {message, status, shouldAddPickLabel} = + checkBranchTarget('feature-branch'); + + expect(status).toBe('FAIL'); + expect(message).toContain('`feature-branch`'); + expect(shouldAddPickLabel).toBe(false); + }); +}); diff --git a/.github/workflow-scripts/checkBranchTarget.js b/.github/workflow-scripts/checkBranchTarget.js index 78e95a5e814..68009e83819 100644 --- a/.github/workflow-scripts/checkBranchTarget.js +++ b/.github/workflow-scripts/checkBranchTarget.js @@ -13,15 +13,17 @@ * Validates that the PR targets an appropriate base branch. * * @param {string} baseRef - The base branch ref (e.g., 'main', '0.76-stable') + * @param {boolean} [isStacked] - Whether the base branch is the head of another + * open PR, as in a stack of PRs * @returns {{message: string, status: 'PASS'|'FAIL', shouldAddPickLabel: boolean}} */ -function checkBranchTarget(baseRef) { +function checkBranchTarget(baseRef, isStacked = false) { const isMain = baseRef === 'main'; const isStable = baseRef.endsWith('-stable'); let message = ''; let status = 'PASS'; - if (!isMain && !isStable) { + if (!isMain && !isStable && !isStacked) { status = 'FAIL'; message = `> [!CAUTION] > **Invalid Base Branch** diff --git a/.github/workflows/analyze-pr.yml b/.github/workflows/analyze-pr.yml index 18c72a16e31..825fbd41598 100644 --- a/.github/workflows/analyze-pr.yml +++ b/.github/workflows/analyze-pr.yml @@ -44,8 +44,15 @@ jobs: repo: context.repo.repo, pull_number: context.issue.number, }); + const {data: basePullRequests} = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${context.repo.owner}:${pullRequest.base.ref}`, + }); const {message, status, shouldAddPickLabel} = checkBranchTarget( pullRequest.base.ref, + basePullRequests.length > 0, ); if (shouldAddPickLabel) {