Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflow-scripts/__tests__/checkBranchTarget-test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 4 additions & 2 deletions .github/workflow-scripts/checkBranchTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/analyze-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading