-
Notifications
You must be signed in to change notification settings - Fork 1
Add Dependabot auto-merge #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+79
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| name: Auto Merge Dependabot PR | ||
|
|
||
| "on": | ||
| workflow_run: | ||
| workflows: ["CI"] | ||
| types: [completed] | ||
|
|
||
| jobs: | ||
| auto-merge: | ||
| if: github.event.workflow_run.conclusion == 'success' && startsWith(github.event.workflow_run.head_branch, 'dependabot/') | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
|
|
||
| steps: | ||
| - name: Resolve Dependabot PR | ||
| id: pr | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| BRANCH_NAME="${{ github.event.workflow_run.head_branch }}" | ||
| PR_NUMBER=$(gh pr list --state open --head "${BRANCH_NAME}" --json number --jq '.[0].number // empty') | ||
| if [ -z "${PR_NUMBER}" ]; then | ||
| echo "No open Dependabot PR found for ${BRANCH_NAME}." >> "$GITHUB_STEP_SUMMARY" | ||
| exit 0 | ||
| fi | ||
| echo "pr_number=${PR_NUMBER}" >> "$GITHUB_OUTPUT" | ||
|
|
||
| - name: Evaluate merge eligibility | ||
| id: merge_guard | ||
| if: steps.pr.outputs.pr_number != '' | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: | | ||
| gh pr view "${{ steps.pr.outputs.pr_number }}" --json number,isDraft,author,url,labels > pr.json | ||
| python3 - <<'PY' | ||
| import json | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| pr = json.loads(Path("pr.json").read_text(encoding="utf-8")) | ||
| author = (pr.get("author") or {}).get("login") | ||
| labels = {item.get("name", "") for item in pr.get("labels", [])} | ||
| should_merge = author == "dependabot[bot]" and not pr.get("isDraft") | ||
| reason = "ready" if should_merge else "not_dependabot_or_draft" | ||
|
|
||
| summary_lines = [ | ||
| "## Auto-Merge Gate", | ||
| f"- PR: {pr['url']}", | ||
| f"- Author: `{author or '<unknown>'}`", | ||
| f"- Draft: `{ 'yes' if pr.get('isDraft') else 'no' }`", | ||
| f"- Dependabot label: `{ 'yes' if 'dependencies' in labels else 'no' }`", | ||
| f"- Final merge decision: `{ 'merge' if should_merge else 'skip' }`", | ||
| f"- Reason: `{reason}`", | ||
| ] | ||
| Path("pr-summary.md").write_text("\n".join(summary_lines).strip() + "\n", encoding="utf-8") | ||
| with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as output: | ||
| print(f"should_merge={'true' if should_merge else 'false'}", file=output) | ||
| print(f"reason={reason}", file=output) | ||
| PY | ||
|
|
||
| - name: Append merge summary | ||
| if: steps.pr.outputs.pr_number != '' | ||
| run: cat pr-summary.md >> "$GITHUB_STEP_SUMMARY" | ||
|
|
||
| - name: Merge Dependabot PR | ||
| if: steps.merge_guard.outputs.should_merge == 'true' | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| run: gh pr merge "${{ steps.pr.outputs.pr_number }}" --rebase --delete-branch | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This workflow invokes
gh pr list/view/mergewithout--repo(orGH_REPO) and without anactions/checkoutstep, so onworkflow_runjobs the CLI may not have a local git repository context to resolve the target repo; in that case the firstgh pr listcall fails and the auto-merge path never runs. Theghdocs describeGH_REPOas required for commands that otherwise operate on a local repository, so this should explicitly passGH_REPO: ${{ github.repository }}or--repo ${{ github.repository }}for each command.Useful? React with 👍 / 👎.