|
| 1 | +name: Precise Auto PR |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - 'release/*' |
| 7 | + |
| 8 | +jobs: |
| 9 | + auto-pr: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@v4 |
| 13 | + with: |
| 14 | + fetch-depth: 0 |
| 15 | + |
| 16 | + - name: set git config |
| 17 | + run: | |
| 18 | + git config --global user.email "${GITHUB_ACTOR_ID}+${GITHUB_ACTOR}@users.noreply.github.com" |
| 19 | + git config --global user.name "${GITHUB_ACTOR}" |
| 20 | + git config --global advice.mergeConflict false |
| 21 | + git config --global --add safe.directory "${{ github.workspace }}" |
| 22 | + git config -l |
| 23 | +
|
| 24 | + - name: check branch names |
| 25 | + id: branch_info |
| 26 | + run: | |
| 27 | + RELEASE_VERSIONS=$(git branch -r | grep 'release/' | cut -d '/' -f 3 | sort -V) |
| 28 | + CURRENT_VERSION=$(echo $GITHUB_REF | rev | cut -d '/' -f 1 | rev) |
| 29 | + CURRENT_BRANCH="release/$CURRENT_VERSION" |
| 30 | + ME_AND_MY_NEXT=$(echo "$RELEASE_VERSIONS" | grep -w $CURRENT_VERSION -A 1) |
| 31 | + NUM=$(echo "$ME_AND_MY_NEXT" | wc -l) |
| 32 | + if (( NUM > 1 )); then |
| 33 | + NEXT_VERSION=$(echo "$ME_AND_MY_NEXT" | tail -n 1) |
| 34 | + NEXT_BRANCH="release/$NEXT_VERSION" |
| 35 | + set -x |
| 36 | + else |
| 37 | + echo "No more higher release versions, will merge to main" |
| 38 | + NEXT_VERSION="main" |
| 39 | + NEXT_BRANCH="main" |
| 40 | + set -x |
| 41 | + fi |
| 42 | + echo "NEXT_VERSION=$NEXT_VERSION" >> $GITHUB_OUTPUT |
| 43 | + echo "NEXT_BRANCH=$NEXT_BRANCH" >> $GITHUB_OUTPUT |
| 44 | + echo "CURRENT_VERSION=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 45 | + echo "CURRENT_BRANCH=$CURRENT_BRANCH" >> $GITHUB_OUTPUT |
| 46 | +
|
| 47 | + - name: create pr branch |
| 48 | + id: create_branch |
| 49 | + run: | |
| 50 | + PRBRANCH=auto-pr-${{github.event.after}}-${{steps.branch_info.outputs.NEXT_VERSION}} |
| 51 | + echo "PRBRANCH=$PRBRANCH" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - name: Merge release branch into ${{steps.branch_info.outputs.NEXT_BRANCH}} |
| 54 | + id: merge-changes |
| 55 | + run: | |
| 56 | + set -x |
| 57 | + git checkout ${{steps.branch_info.outputs.NEXT_BRANCH}} |
| 58 | + git checkout -b ${{steps.create_branch.outputs.PRBRANCH}} |
| 59 | + echo "MESSAGE<<__EOF" >> $GITHUB_OUTPUT |
| 60 | + git log --format=%B ${{ github.event.commits[0].id }}~..${{ github.event.after }} >> $GITHUB_OUTPUT |
| 61 | + echo "__EOF" >> $GITHUB_OUTPUT |
| 62 | + REVS=$(git rev-list --reverse ${{ github.event.commits[0].id }}~..${{ github.event.after }} ) |
| 63 | + for rev in "$REVS"; do |
| 64 | + if ! git cherry-pick -X theirs ${rev} ; then |
| 65 | + echo "merge=false" >> $GITHUB_OUTPUT |
| 66 | + exit 0; |
| 67 | + fi |
| 68 | + done |
| 69 | + git push -d origin ${{steps.create_branch.outputs.PRBRANCH}} || true |
| 70 | + git push -u origin ${{steps.create_branch.outputs.PRBRANCH}} |
| 71 | + echo "merge=true" >> $GITHUB_OUTPUT |
| 72 | +
|
| 73 | + - uses: actions/github-script@v7 |
| 74 | + name: Open pick PR to ${{steps.branch_info.outputs.NEXT_BRANCH}} |
| 75 | + if: steps.merge-changes.outputs.merge == 'true' |
| 76 | + with: |
| 77 | + github-token: ${{ github.token }} |
| 78 | + script: | |
| 79 | + await github.rest.pulls.create({ |
| 80 | + ...context.repo, |
| 81 | + title: `Pick ${{steps.branch_info.outputs.CURRENT_VERSION}} to ${{steps.branch_info.outputs.NEXT_VERSION}}`, |
| 82 | + head: `${{steps.create_branch.outputs.PRBRANCH}}`, |
| 83 | + base: `${{steps.branch_info.outputs.NEXT_BRANCH}}`, |
| 84 | + body: `${{steps.merge-changes.outputs.MESSAGE}}\nGenerated by Auto PR, by cherry-pick related commits`, |
| 85 | + }); |
| 86 | +
|
| 87 | + - uses: actions/github-script@v7 |
| 88 | + name: Open merge PR to ${{steps.branch_info.outputs.NEXT_BRANCH}} |
| 89 | + if: steps.merge-changes.outputs.merge == 'false' |
| 90 | + with: |
| 91 | + github-token: ${{ github.token }} |
| 92 | + script: | |
| 93 | + await github.rest.pulls.create({ |
| 94 | + ...context.repo, |
| 95 | + title: `Merge ${{steps.branch_info.outputs.CURRENT_VERSION}} to ${{steps.branch_info.outputs.NEXT_VERSION}}`, |
| 96 | + head: `${context.ref}`, |
| 97 | + base: `${{steps.branch_info.outputs.NEXT_BRANCH}}`, |
| 98 | + body: `${{steps.merge-changes.outputs.MESSAGE}}\nGenerated by Auto PR, using merge since cherry-pick failed`, |
| 99 | + }); |
0 commit comments