|
| 1 | +name: Create Cherrypick PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - closed |
| 7 | + branches: |
| 8 | + # TODO: Extract this to an env variable? |
| 9 | + - 'master' |
| 10 | + |
| 11 | +env: |
| 12 | + # TODO: Add a way to handle multiple potential cherrypick targets. |
| 13 | + TARGET_BRANCH: '4.3' |
| 14 | + USERNAME: 'Godot Organization' |
| 15 | + |
| 16 | + |
| 17 | +jobs: |
| 18 | + Create-cherrypick-PR: |
| 19 | + # The cherrypick label is hardcoded because `contains()` doesn't seem to be able to use an environment variable as a second argument. |
| 20 | + if: ${{ github.event.pull_request.merged == true && contains( github.event.pull_request.labels.*.name, 'cherrypick:4.3' ) }} |
| 21 | + runs-on: ubuntu-24.04 |
| 22 | + timeout-minutes: 10 |
| 23 | + env: |
| 24 | + # "Ternary" hack featured in the official docs. |
| 25 | + # When using "Squash and merge", the commit hash is the last merge commit of the pull request merge branch. |
| 26 | + # When using "Merge", the commit hash is the last commit to the head branch of the pull request. |
| 27 | + # This is mildly error-prone, since in theory we could merge multiple commits without squashing. |
| 28 | + # We are relying on human review of the generated PRs to catch that. |
| 29 | + COMMIT_HASH: ${{ github.event.pull_request.commits > 1 && github.sha || github.event.pull_request.head.sha }} |
| 30 | + PR_NUMBER: ${{ github.event.number }} |
| 31 | + |
| 32 | + permissions: |
| 33 | + contents: write |
| 34 | + pull-requests: write |
| 35 | + |
| 36 | + steps: |
| 37 | + - name: Checkout |
| 38 | + uses: actions/checkout@v4 |
| 39 | + with: |
| 40 | + ref: ${{ env.TARGET_BRANCH }} |
| 41 | + |
| 42 | + - name: Cherrypick Commit |
| 43 | + id: cherrypick_commit |
| 44 | + continue-on-error: true |
| 45 | + # TODO: Maybe only fetch some branches? |
| 46 | + run: | |
| 47 | + git config user.name "${{ env.USERNAME }}" |
| 48 | + git config user.email "${{ env.EMAIL }}" |
| 49 | + git fetch |
| 50 | + git cherry-pick -m 1 ${{ env.COMMIT_HASH }} |
| 51 | +
|
| 52 | + - name: Create Pull Request |
| 53 | + if: steps.cherrypick_commit.outcome == 'success' |
| 54 | + uses: peter-evans/create-pull-request@v7 |
| 55 | + with: |
| 56 | + commit-message: 'Cherrypick to ${{ env.TARGET_BRANCH }}' |
| 57 | + branch: 'cherrypick-${{ env.PR_NUMBER }}-${{ env.TARGET_BRANCH }}' |
| 58 | + delete-branch: true |
| 59 | + |
| 60 | + # Configure the commit author. |
| 61 | + author: '${{ env.USERNAME }} <${{ env.EMAIL }}>' |
| 62 | + committer: '${{ env.USERNAME }} <${{ env.EMAIL }}>' |
| 63 | + |
| 64 | + # Configure the pull request. |
| 65 | + title: 'Cherrypick ${{ env.PR_NUMBER }} to ${{ env.TARGET_BRANCH }}' |
| 66 | + body: 'Cherrypick #${{ env.PR_NUMBER }} to ${{ env.TARGET_BRANCH }}.' |
| 67 | + # TODO: Only add the bug or enhancement label, depending on which the original PR uses. |
| 68 | + labels: 'bug,enhancement' |
| 69 | + |
| 70 | + - name: Handle failure |
| 71 | + if: steps.cherrypick_commit.outcome == 'failure' |
| 72 | + run: | |
| 73 | + echo "Can't automatically cherrypick. Potential causes:" |
| 74 | + echo "- PR has multiple commits. Did you squash and merge?" |
| 75 | + echo "- Cherrypick did not apply cleanly and can't be auto-merged." |
0 commit comments