|
| 1 | +name: Create automerge PR |
| 2 | + |
| 3 | +# Merges `head_branch` into `base_branch` and opens a PR to incorporate that merge commit into `base_branch`. |
| 4 | +# |
| 5 | +# The typical use case for this is in the first period after Swift has cut release branches. |
| 6 | +# Some repositories want to include most changes from `main` also in the release branch. When this job is set up, it can automatically create PRs to merge `main` into the release branch. |
| 7 | +# Maintainers of the package can then inspect the changes to ensure that they are not too risky for the release branch. |
| 8 | +# We will also run the normal PR testing on these changes, ensuring that these modifications don't break the build. |
| 9 | +# |
| 10 | +# Example usage in a repository: |
| 11 | +# |
| 12 | +# ``` |
| 13 | +# name: Create PR to merge main into release branch |
| 14 | +# |
| 15 | +# # In the first period after branching the release branch, we typically want to include all changes from `main` also in the release branch. This workflow automatically creates a PR every Monday to merge main into the release branch. |
| 16 | +# # Later in the release cycle we should stop this practice to avoid landing risky changes by disabling this workflow. To do so, disable the workflow as described in https://docs.github.com/en/actions/managing-workflow-runs-and-deployments/managing-workflow-runs/disabling-and-enabling-a-workflow |
| 17 | +# |
| 18 | +# on: |
| 19 | +# schedule: |
| 20 | +# - cron: '0 9 * * MON' |
| 21 | +# workflow_dispatch: |
| 22 | +# |
| 23 | +# jobs: |
| 24 | +# create_merge_pr: |
| 25 | +# name: Create PR to merge main into release branch |
| 26 | +# uses: swiftlang/github-workflows/.github/workflows/create_automerge_pr.yml@main |
| 27 | +# if: (github.event_name == 'schedule' && github.repository == 'swiftlang/swift-format') || (github.event_name != 'schedule') # Ensure that we don't run this on a schedule in a fork |
| 28 | +# permissions: |
| 29 | +# contents: write |
| 30 | +# pull-requests: write |
| 31 | +# with: |
| 32 | +# base_branch: release/6.2 |
| 33 | +# ``` |
| 34 | +# |
| 35 | +# PRs created by GitHub Actions don't kick off further actions (https://github.com/peter-evans/create-pull-request/blob/d57e551ebc1a16dee0b8c9ea6d24dba7627a6e35/docs/concepts-guidelines.md#triggering-further-workflow-runs). |
| 36 | +# As a workaround, we mark automerge PRs that are created by GitHub actions as draft and trigger the GitHub actions by marking the PR as ready for review. `ready_for_review` must be added to the PR types for this purpose, eg. |
| 37 | +# ``` |
| 38 | +# on: |
| 39 | +# pull_request: |
| 40 | +# types: [..., ready_for_review] |
| 41 | +# ``` |
| 42 | +# Unfortunately this will also re-trigger testing evenon a normal user's PR (which may have already been tested), but skipping them causes the checks to reset so this is the best we can do for now. |
| 43 | +on: |
| 44 | + workflow_call: |
| 45 | + inputs: |
| 46 | + base_branch: |
| 47 | + type: string |
| 48 | + description: The branch into which head_branch should be merged |
| 49 | + required: true |
| 50 | + head_branch: |
| 51 | + type: string |
| 52 | + description: The branch that should be merged into base_branch |
| 53 | + default: main |
| 54 | + pr_message: |
| 55 | + type: string |
| 56 | + description: The message that should be included in the PR created by this job |
| 57 | + default: This PR was automatically opened by a GitHub action. Review the changes included in this PR and determine if they should be included in the release branch. If yes, merge the PR. Otherwise revert changes that should not be included on this branch. |
| 58 | + |
| 59 | +jobs: |
| 60 | + create_merge_pr: |
| 61 | + name: Create PR to merge ${{ inputs.head_branch }} into ${{ inputs.base_branch }} branch |
| 62 | + runs-on: ubuntu-latest |
| 63 | + permissions: |
| 64 | + contents: write |
| 65 | + pull-requests: write |
| 66 | + steps: |
| 67 | + - name: Checkout repository |
| 68 | + uses: actions/checkout@v4 |
| 69 | + with: |
| 70 | + fetch-depth: 0 |
| 71 | + - name: Check if there are commits to merge |
| 72 | + id: create_merge_commit |
| 73 | + run: | |
| 74 | + # Without this, we can't perform git operations in GitHub actions. |
| 75 | + git config --global --add safe.directory "$(realpath .)" |
| 76 | + git config --local user.name 'swift-ci' |
| 77 | + git config --local user.email '[email protected]' |
| 78 | +
|
| 79 | + if [[ "$(git rev-list --left-only --count origin/${{ inputs.head_branch }}...origin/${{ inputs.base_branch }})" == 0 ]]; then |
| 80 | + echo "Nothing to merge" |
| 81 | + echo "has_commits_to_merge=false" >> "$GITHUB_OUTPUT" |
| 82 | + exit |
| 83 | + fi |
| 84 | +
|
| 85 | + echo "has_commits_to_merge=true" >> "$GITHUB_OUTPUT" |
| 86 | + - name: Push branch and create PR |
| 87 | + id: push_branch |
| 88 | + if: ${{ steps.create_merge_commit.outputs.has_commits_to_merge == 'true' }} |
| 89 | + env: |
| 90 | + GH_TOKEN: ${{ github.token }} |
| 91 | + run: | |
| 92 | + # Create a branch for the PR instead of opening a PR that merges head_branch directly so that we have a fixed |
| 93 | + # target in the PR and don't modify the PR as new commits are put on the head branch. |
| 94 | + PR_BRANCH="automerge/merge-main-$(date +%Y-%m-%d_%H-%M)" |
| 95 | + git checkout ${{ inputs.head_branch }} |
| 96 | + git checkout -b "$PR_BRANCH" |
| 97 | + git push --set-upstream origin "$PR_BRANCH" |
| 98 | +
|
| 99 | + gh pr create \ |
| 100 | + --base "${{ inputs.base_branch }}" \ |
| 101 | + --head "$PR_BRANCH" \ |
| 102 | + --title 'Merge `${{ inputs.head_branch }}` into `${{ inputs.base_branch }}`' \ |
| 103 | + --body '${{ inputs.pr_message }}' \ |
| 104 | + --draft |
0 commit comments