proposal-system/.github/workflows/auto-merge.yaml

111 lines
3.6 KiB
YAML

name: Enable auto-merge
on:
pull_request:
types: [opened, ready_for_review, reopened, synchronize]
branches: [main]
pull_request_review:
types: [submitted]
permissions:
contents: read
jobs:
enable:
if: >-
github.event.pull_request.draft == false
&& github.event.pull_request.base.ref == 'main'
&& github.event.pull_request.head.repo.full_name == github.repository
&& github.event.pull_request.user.login != 'dependabot[bot]'
&& github.event.pull_request.user.login != 'renovate[bot]'
&& (github.event_name != 'pull_request_review' || github.event.review.state == 'approved')
runs-on: ubuntu-latest
concurrency:
group: enable-auto-merge-${{ github.event.pull_request.number }}
cancel-in-progress: true
steps:
- name: Mint seahaven-bot token
id: app-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
with:
app-id: ${{ secrets.SEAHAVEN_BOT_APP_ID }}
private-key: ${{ secrets.SEAHAVEN_BOT_PRIVATE_KEY }}
owner: Sea-Haven-Industries
repositories: ${{ github.event.repository.name }}
- name: Enqueue or arm auto-merge
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
PR: ${{ github.event.pull_request.number }}
REPO: ${{ github.repository }}
PR_NODE_ID: ${{ github.event.pull_request.node_id }}
run: |
set -euo pipefail
BOT_LOGIN='seahaven-bot[bot]'
GQL_STATE="query(\$id:ID!) { node(id:\$id) { ... on PullRequest { isInMergeQueue autoMergeRequest { mergeMethod enabledBy { login } } } } }"
pr_json() {
gh api graphql -f query="$GQL_STATE" -f id="$PR_NODE_ID" "$@"
}
in_queue() {
pr_json --jq '.data.node.isInMergeQueue'
}
enabled_by() {
pr_json --jq -r '.data.node.autoMergeRequest.enabledBy.login // ""'
}
armed_as_bot() {
by="$(enabled_by)"
[ "$by" = "$BOT_LOGIN" ] || [ "$by" = "seahaven-bot" ]
}
if [ "$(in_queue)" = "true" ]; then
echo "already in merge queue"
exit 0
fi
set +e
out="$(gh api graphql -f query="mutation(\$id:ID!) { enqueuePullRequest(input:{pullRequestId:\$id}) { mergeQueueEntry { state } } }" -f id="$PR_NODE_ID" 2>&1)"
status=$?
set -e
if [ "$status" -eq 0 ]; then
printf '%s\n' "$out"
exit 0
fi
echo "$out"
# Re-arm as seahaven-bot. A leftover GITHUB_TOKEN auto-merge would
# enqueue without starting merge_group. Do not treat an unknown
# autoMergeRequest as success.
by="$(enabled_by)"
if [ -n "$by" ]; then
set +e
disable_out="$(gh pr merge "$PR" --repo "$REPO" --disable-auto 2>&1)"
set -e
echo "$disable_out"
by="$(enabled_by)"
if [ -n "$by" ]; then
echo "failed to disable auto-merge (enabledBy=$by)"
exit 1
fi
fi
set +e
arm_out="$(gh pr merge "$PR" --repo "$REPO" --auto 2>&1)"
arm_status=$?
set -e
printf '%s\n' "$arm_out"
if [ "$(in_queue)" = "true" ]; then
echo "in merge queue"
exit 0
fi
if armed_as_bot; then
echo "auto-merge armed as seahaven-bot"
exit 0
fi
by="$(enabled_by)"
echo "auto-merge not armed as seahaven-bot (enabledBy=${by:-none} arm_status=$arm_status)"
exit 1