Skip to content

Commit

Permalink
Use reusable automerge workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
sergej-koscejev committed Jan 4, 2024
1 parent 4e2cad6 commit 56cdbc5
Showing 1 changed file with 1 addition and 60 deletions.
61 changes: 1 addition & 60 deletions .github/workflows/PR-into-next-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,4 @@ on:

jobs:
create-pr:
runs-on: ubuntu-latest
steps:
- shell: bash
env:
GH_TOKEN: ${{ github.token }}
owner_and_repo: ${{ github.repository }}
run: |
echo "Calculating PR branches:"
# Next branch matching refs/heads/maintenance/* pattern, or empty if we're on the last one
next_maintenance_branch=$(gh api "/repos/$owner_and_repo/git/matching-refs/heads/maintenance/mps" \
--jq 'map(.ref | ltrimstr("refs/heads/") | select(. > "${{ github.ref_name }}" and . < "maintenance/mps3")) | min')
echo " Next maintenance branch: ${next_maintenance_branch:-(none)}"
# Substitute the default branch if empty
default_branch=${{ github.event.repository.default_branch }}
base_branch=${next_maintenance_branch:-$default_branch}
echo " PR base branch: $base_branch"
# Calculate PR head branch name (the branch to merge) by substituting 'maintenance' for 'merge'
head_branch=${{ github.ref_name }}
merge_branch=${head_branch/#maintenance/merge}
echo " Merge branch (will be used as PR head): $merge_branch"
echo ""
echo "Pushing ${{ github.ref_name }} to $merge_branch"
# Create or update $merge_branch
if gh api repos/$owner_and_repo/git/ref/heads/$merge_branch --silent
then
# Branch exists, merge $head_branch into it
gh api repos/$owner_and_repo/merges --silent --method=POST -f base=$merge_branch -f head=$head_branch ||
(echo "Merging $head_branch to existing $merge_branch failed, aborting." ; exit 1)
else
# Branch does not exist, create it from $head_branch
gh api repos/$owner_and_repo/git/refs --silent --method=POST -f ref="refs/heads/$merge_branch" -f sha=${{ github.sha }} ||
(echo "Could not create merge branch $merge_branch" ; exit 1)
fi
# Merge $base_branch -> $merge_branch, in order for $merge_branch to be up to date, as required by the branch
# protection rules.
# The head and base arguments are intentionally reversed because this is a reverse merge.
gh api repos/$owner_and_repo/merges --silent --method=POST -f base=$merge_branch -f head=$base_branch ||
echo "Updating $merge_branch from $base_branch failed, please do it manually."
# Create PR if it does not exist yet
existing_pr_url=$(gh pr list --repo $owner_and_repo --head $merge_branch --base $base_branch --json url --jq 'map(.url[]).[]')
if [[ $existing_pr_url ]]
then
echo "Pull request $merge_branch -> $base_branch already exists at $existing_pr_url"
else
echo "Creating a new pull request $merge_branch -> $base_branch"
gh pr create --repo $owner_and_repo --head $merge_branch --base $base_branch \
--title "Merge ${{ github.ref_name }} into $base_branch" \
--body "An automatic PR to merge changes from \`${{ github.ref_name }}\` to \`$base_branch\`." \
--assignee '${{ github.actor }}' \
--reviewer '${{ github.actor }}'
fi
uses: specificlanguages/cascading-merge/.github/workflows/workflow.yml@v1

0 comments on commit 56cdbc5

Please sign in to comment.