Skip to content

Commit 5d91fee

Browse files
committed
Accept stacked PR base branches in analyze-pr
1 parent 210b1fb commit 5d91fee

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 checkBranchTarget = require('../checkBranchTarget');
13+
14+
describe('checkBranchTarget', () => {
15+
test('passes for main', () => {
16+
expect(checkBranchTarget('main')).toEqual({
17+
message: '',
18+
status: 'PASS',
19+
shouldAddPickLabel: false,
20+
});
21+
});
22+
23+
test('passes for a stable branch and adds the pick label', () => {
24+
expect(checkBranchTarget('0.76-stable')).toEqual({
25+
message: '',
26+
status: 'PASS',
27+
shouldAddPickLabel: true,
28+
});
29+
});
30+
31+
test('passes for the head branch of another open PR', () => {
32+
expect(checkBranchTarget('pr58950', true)).toEqual({
33+
message: '',
34+
status: 'PASS',
35+
shouldAddPickLabel: false,
36+
});
37+
});
38+
39+
test('fails for any other branch', () => {
40+
const {message, status, shouldAddPickLabel} =
41+
checkBranchTarget('feature-branch');
42+
43+
expect(status).toBe('FAIL');
44+
expect(message).toContain('`feature-branch`');
45+
expect(shouldAddPickLabel).toBe(false);
46+
});
47+
});

‎.github/workflow-scripts/checkBranchTarget.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
* Validates that the PR targets an appropriate base branch.
1414
*
1515
* @param {string} baseRef - The base branch ref (e.g., 'main', '0.76-stable')
16+
* @param {boolean} [isStacked] - Whether the base branch is the head of another
17+
* open PR, as in a stack of PRs
1618
* @returns {{message: string, status: 'PASS'|'FAIL', shouldAddPickLabel: boolean}}
1719
*/
18-
function checkBranchTarget(baseRef) {
20+
function checkBranchTarget(baseRef, isStacked = false) {
1921
const isMain = baseRef === 'main';
2022
const isStable = baseRef.endsWith('-stable');
2123

2224
let message = '';
2325
let status = 'PASS';
24-
if (!isMain && !isStable) {
26+
if (!isMain && !isStable && !isStacked) {
2527
status = 'FAIL';
2628
message = `> [!CAUTION]
2729
> **Invalid Base Branch**

‎.github/workflows/analyze-pr.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,15 @@ jobs:
4444
repo: context.repo.repo,
4545
pull_number: context.issue.number,
4646
});
47+
const {data: basePullRequests} = await github.rest.pulls.list({
48+
owner: context.repo.owner,
49+
repo: context.repo.repo,
50+
state: 'open',
51+
head: `${context.repo.owner}:${pullRequest.base.ref}`,
52+
});
4753
const {message, status, shouldAddPickLabel} = checkBranchTarget(
4854
pullRequest.base.ref,
55+
basePullRequests.length > 0,
4956
);
5057
5158
if (shouldAddPickLabel) {

0 commit comments

Comments
 (0)