|
| 1 | +name: Cherry-Pick PRs by Milestone |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + inputs: |
| 5 | + milestone: |
| 6 | + description: "The milestone to filter PRs by" |
| 7 | + required: true |
| 8 | + default: "v1.0" |
| 9 | + target_branch: |
| 10 | + description: "The branch to base the new branch on (e.g., staging)" |
| 11 | + required: true |
| 12 | + default: "staging" |
| 13 | + |
| 14 | +jobs: |
| 15 | + cherry-pick: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + - name: Set up Git |
| 23 | + run: | |
| 24 | + git config --global user.name "github-actions[bot]" |
| 25 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 26 | + - name: Get PRs with Milestone |
| 27 | + id: fetch_prs |
| 28 | + run: | |
| 29 | + PR_LIST=$(gh pr list --state closed --json number,milestone,mergeCommitSha \ i |
| 30 | + --jq ".[] | select(.milestone.title == '${{ inputs.milestone }}') | .mergeCommitSha") |
| 31 | + if [ -z "$PR_LIST" ]; then |
| 32 | + echo "No PRs found with milestone ${{ inputs.milestone }}" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + echo "PRs to cherry-pick:" |
| 36 | + echo "$PR_LIST" |
| 37 | + echo "::set-output name=pr_list::$PR_LIST" |
| 38 | + - name: Create new branch |
| 39 | + id: create_branch |
| 40 | + run: | |
| 41 | + NEW_BRANCH="cherry-pick-${{ inputs.milestone }}-$(date +'%Y%m%d%H%M%S')" |
| 42 | + git checkout ${{ inputs.target_branch }} |
| 43 | + git checkout -b $NEW_BRANCH |
| 44 | + echo "::set-output name=new_branch::$NEW_BRANCH" |
| 45 | + - name: Cherry-pick PRs |
| 46 | + run: | |
| 47 | + for COMMIT in ${{ steps.fetch_prs.outputs.pr_list }}; do |
| 48 | + echo "Cherry-picking commit $COMMIT..." git cherry-pick $COMMIT || { |
| 49 | + echo "Conflict occurred. Aborting."; |
| 50 | + git cherry-pick --abort; |
| 51 | + exit 1; } |
| 52 | + done |
| 53 | + - name: Push new branch |
| 54 | + run: | |
| 55 | + git push origin ${{ steps.create_branch.outputs.new_branch }} |
| 56 | + - name: Create Pull Request |
| 57 | + uses: peter-evans/create-pull-request@v5 |
| 58 | + with: |
| 59 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + base: ${{ inputs.target_branch }} |
| 61 | + head: ${{ steps.create_branch.outputs.new_branch }} |
| 62 | + title: "Cherry-pick PRs for Milestone: ${{ inputs.milestone }}" |
| 63 | + body: "This PR cherry-picks the following commits into the ${{ inputs.target_branch }} branch:\n\n${{ steps.fetch_prs.outputs.pr_list }}" |
0 commit comments