Skip to content

Cherry-Pick PRs by Milestone #48

Cherry-Pick PRs by Milestone

Cherry-Pick PRs by Milestone #48

Workflow file for this run

name: Cherry-Pick PRs by Milestone
on:
workflow_dispatch:
inputs:
milestone:
description: "The milestone to filter PRs by"
required: true
default: "tst"
target_branch:
description: "The branch to base the new branch on (e.g., staging)"
required: true
default: "main"
jobs:
cherry-pick:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Git
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Get PRs with Milestone
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
id: fetch_prs
run: |
PR_LIST=$(gh pr list --state closed --json number,milestone,mergeCommit \
--jq '[.[] | select(.milestone.title == "${{ inputs.milestone }}") | .mergeCommit.oid]' | jq -c)
if [ -z "$PR_LIST" ]; then
echo "No PRs found with milestone ${{ inputs.milestone }}"
exit 1
fi
ENCODED_PR_LIST=$(printf '%s' "$PR_LIST" | jq -Rr @json)
echo "PRs to cherry-pick:"
echo "$ENCODED_PR_LIST"
echo "pr_list=$ENCODED_PR_LIST" >> "$GITHUB_OUTPUT"
- name: Create new branch
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
id: create_branch
run: |
NEW_BRANCH="cherry-pick-${{ inputs.milestone }}-$(date +'%Y%m%d%H%M%S')"
git checkout ${{ inputs.target_branch }}
git checkout -b $NEW_BRANCH
git push -u origin $NEW_BRANCH
echo "new_branch=$NEW_BRANCH" >> "$GITHUB_OUTPUT"
- name: Cherry-pick PRs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "Received PR_LIST: ${{ steps.fetch_prs.outputs.pr_list }}"
PR_LIST="${{ steps.fetch_prs.outputs.pr_list }}"
DECODED_PR_LIST=$(echo "$PR_LIST" | jq -r)
echo "exported list: $DECODED_PR_LIST"
echo "$DECODED_PR_LIST" | jq -r '.[]' | while read -r COMMIT; do
echo "Cherry-picking commit $COMMIT..."
git cherry-pick -m 1 $COMMIT ||
{
echo "Conflict occurred. Aborting.";
git cherry-pick --abort;
exit 1;
}
done
- name: Push new branch
run: |
echo ${{ steps.create_branch.outputs.new_branch }}
git push origin ${{ steps.create_branch.outputs.new_branch }}
- name: Create Pull Request
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr create --base ${{ inputs.target_branch }} --head ${{ steps.create_branch.outputs.new_branch }} -t "Cherry-pick PRs for Milestone: ${{ inputs.milestone }}" -b "This PR cherry-picks the following commits into the ${{ inputs.target_branch }} branch:\n\n${{ steps.fetch_prs.outputs.pr_list }}"