Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an auto-cherry-pick GitHub Action #19021

Merged
merged 6 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
33 changes: 33 additions & 0 deletions .github/workflows/auto-cherry-picker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Auto Cherry-Picker

on:
pull_request:
types:
- closed
branches:
- main
thejcannon marked this conversation as resolved.
Show resolved Hide resolved
workflow_dispatch:
inputs:
PR_number:
description: The PR number to cherry-pick
type: string
required: true

jobs:
picker:
if: (github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'needs-cherrypick') ) || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v2
with:
# Always checkout the latest commit of main
ref: "main"

- name: Cherry-Pick
run: |
git config --local user.email "pantsbuild+github-automation@gmail.com"
git config --local user.name "Worker Pants (Pantsbuild GitHub Automation Bot)"
Comment on lines +29 to +30
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build-support/bin/cherry_pick.sh ${{ github.event.pull_request.number || inputs.PR_number }} origin
env:
GH_TOKEN: ${{ secrets.WORKER_PANTS_PAT }}
20 changes: 12 additions & 8 deletions build-support/bin/cherry_pick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ set -e

# (Optional) CLI Args:
# $1 - PR Number (E.g. "12345")
# $2 - Milestone (E.g. "2.11.x")
# this is grabbed off the PR. Useful if you also want to cherry-pick later.
# $2 - remote for pushing (E.g. "origin")
# The remote to push the cherry-pick branch to.
# Defaults to prompting.
# (Please avoid pushing to pantsbuild/pants, and push to your fork instead)

function fail {
printf '%s\n' "$1" >&2
Expand All @@ -25,13 +27,11 @@ if [[ -z $PR_NUM ]]; then
read -r PR_NUM
fi

TARGET_MILESTONE=$2
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The milestone changes should've been made on the "every milestone" PR, sorry y'all

REMOTE=$2

TARGET_MILESTONE=$(gh pr view "$PR_NUM" --json milestone --jq '.milestone.title')
if [[ -z $TARGET_MILESTONE ]]; then
TARGET_MILESTONE=$(gh pr view "$PR_NUM" --json milestone --jq '.milestone.title')
if [[ -z $TARGET_MILESTONE ]]; then
echo "No milestone on PR. What's the milestone? (E.g. 2.10.x)"
read -r TARGET_MILESTONE
fi
fail "No milestone on PR. Please add one or check the PR number."
fi

# NB: Find all milestones >= $TARGET_MILESTONE by having GH list them, then uses awk to trim the
Expand All @@ -51,6 +51,7 @@ COMMIT=$(gh pr view "$PR_NUM" --json mergeCommit --jq '.mergeCommit.oid')
if [[ -z $COMMIT ]]; then
fail "Wasn't able to retrieve merge commit for $PR_NUM."
fi
git fetch https://github.com/pantsbuild/pants "$COMMIT"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means the person running the script doesn't need an up-to-date main, we'll fetch the ref.


TITLE=$(gh pr view "$PR_NUM" --json title --jq '.title')
CATEGORY_LABEL=$(gh pr view "$PR_NUM" --json labels --jq '.labels.[] | select(.name|test("category:.")).name')
Expand All @@ -73,6 +74,9 @@ for MILESTONE in $MILESTONES; do
BRANCH_NAME="cherry-pick-$PR_NUM-to-$MILESTONE"
git checkout -b "$BRANCH_NAME" FETCH_HEAD
if git cherry-pick "$COMMIT"; then
if [[ -n $REMOTE ]]; then
git push -u "$REMOTE" "$BRANCH_NAME"
fi
Comment on lines +78 to +80
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the GitHub Action doesn't get to participate in prompting, we need to push the branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the GitHub cli won't even try to prompt in this case, it'll detect the branch already exists on a remote?

"${PR_CREATE_CMD[@]}"
else
readarray -t -d '' ESCAPED_PR_CREATE_CMD < <(printf "%q\0" "${PR_CREATE_CMD[@]}")
Expand Down
Loading