Skip to content

Commit

Permalink
[code-infra] Allow manual cherry pick inputs (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
JCQuintas authored Jan 22, 2025
1 parent ebb1123 commit 8e71e4a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/prs_create-cherry-pick-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ name: Cherry pick onto target branches

on:
workflow_call:
inputs:
target_branch:
description: 'A target branch passed from the caller workflow'
required: false
type: string
pr_number:
description: 'The PR number to cherry-pick from'
required: false
type: string

permissions: {}

Expand All @@ -12,7 +21,7 @@ jobs:
permissions:
pull-requests: write
contents: write
if: ${{ contains(github.event.pull_request.labels.*.name, 'needs cherry-pick') && github.event.pull_request.merged == true }}
if: ${{ (inputs.target_branch != '' && inputs.pr_number != '') || (contains(github.event.pull_request.labels.*.name, 'needs cherry-pick') && github.event.pull_request.merged == true)}}
outputs:
targetBranches: ${{ steps.detect.outputs.TARGET_BRANCHES }}
reviewers: ${{ steps.detect.outputs.REVIEWERS }}
Expand All @@ -28,6 +37,9 @@ jobs:
- name: Detect target
id: detect
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
env:
TARGET_BRANCH: ${{ inputs.target_branch }}
PR_NUMBER: ${{ inputs.pr_number }}
with:
script: |
const script = require('./.github/workflows/scripts/prs/detectTargetBranch.js');
Expand Down
37 changes: 20 additions & 17 deletions .github/workflows/scripts/prs/detectTargetBranch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// @ts-check
const vBranchRegex = /^v\d{1,3}\.x$/;
const targetBranches = [];

/**
* @param {Object} params
Expand All @@ -12,7 +11,7 @@ module.exports = async ({ core, context, github }) => {
try {
const owner = context.repo.owner;
const repo = context.repo.repo;
const pullNumber = context.issue.number;
const pullNumber = process.env.PR_NUMBER ?? context.issue.number;

const { data: pr } = await github.rest.pulls.get({
owner,
Expand All @@ -30,25 +29,29 @@ module.exports = async ({ core, context, github }) => {
(label) => label !== 'needs cherry-pick' && !vBranchRegex.test(label),
);

if (vBranchRegex.test(pr.head_ref) || pr.head_ref === 'next') {
// the branch this is coming from is a version branch, so one of the targets should be master
core.info('>>> Head Ref is a version branch. Adding `master` as target');
targetLabels.push('master');
}

if (targetLabels.length === 0) {
// there was no target branch present
core.info('>>> No target label found');

// the PR is not coming from a version branch
core.setOutput('TARGET_BRANCHES', '');
return;
if (process.env.TARGET_BRANCH) {
targetLabels.push(process.env.TARGET_BRANCH);
} else {
if (vBranchRegex.test(pr.head_ref) || pr.head_ref === 'next') {
// the branch this is coming from is a version branch, so one of the targets should be master
core.info('>>> Head Ref is a version branch. Adding `master` as target');
targetLabels.push('master');
}

if (targetLabels.length === 0) {
// there was no target branch present
core.info('>>> No target label found');

// the PR is not coming from a version branch
core.setOutput('TARGET_BRANCHES', '');
return;
}
}

core.info(`>>> Target labels found: ${targetLabels.join(', ')}`);

// get a list of the originally requested reviewers
const reuestedReviewers = pr.requested_reviewers.map((reviewer) => reviewer.login);
const requestedReviewers = pr.requested_reviewers.map((reviewer) => reviewer.login);

// get a list of the reviews done for the PR
const { data: reviews } = github.rest.pulls.listReviews({
Expand All @@ -63,7 +66,7 @@ module.exports = async ({ core, context, github }) => {
[];

// merge the 2 arrays into a single array of unique reviewers
const reviewers = [...new Set([...reuestedReviewers, ...approvingReviewers])];
const reviewers = [...new Set([...requestedReviewers, ...approvingReviewers])];

core.info(`>>> Reviewers from original PR: ${reviewers.join(', ')}`);

Expand Down

0 comments on commit 8e71e4a

Please sign in to comment.