Repository navigation
feat(auto-merge): pilot auto-merge for owner PRs that Copilot approved #79
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b8b33a6
feat(configure): request Copilot reviews where the auto-merge pilot runs
atdr 8bc46b3
ci(auto-merge): pilot auto-merge for Copilot approved owner PRs
atdr 3c9ee5c
ci(auto-merge): merge the approved commit directly, not via auto-merge
atdr cefd173
ci(auto-merge): start CI on main after each merge, drop manual runs
atdr b7bab0f
ci(auto-merge): report a merge overtaken before its CI dispatch
atdr 2d38d43
ci(auto-merge): check the tip after both CI dispatches
atdr 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
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,140 @@ | ||
| # Pilot, this repo only: an owner PR that Copilot approved merges once every | ||
| # gate the ruleset requires passes (STANDARDS.md, Copilot auto-merge). | ||
| # | ||
| # It merges only the exact commit Copilot's latest review approved, and only | ||
| # while that commit is still the PR's head. Never a fork, Dependabot | ||
| # (auto-merge.yml handles those) or release-please: the release PR stays a | ||
| # human checkpoint before any caller picks a change up. | ||
| # | ||
| # It merges directly rather than switching GitHub's auto-merge on. Auto-merge | ||
| # stays on across pushes, so a push Copilot never saw could merge if its checks | ||
| # passed before anything switched it off. `gh pr merge --match-head-commit` | ||
| # makes GitHub refuse the merge once the head is not the approved commit, and | ||
| # the ruleset refuses it until that commit's gates pass, so there is no window. | ||
| # | ||
| # Triggers. Not `pull_request_review`: GitHub holds a run started by Copilot's | ||
| # review for manual approval before any job is read (action_required with no | ||
| # jobs, later failure), reportedly starts none for an approval without inline | ||
| # comments, and runs the PR's own copy of this file with a write token, so a | ||
| # PR could rewrite its own guards. Instead a scheduled sweep runs the copy on | ||
| # the default branch, so a PR never runs its own version. No | ||
| # `workflow_dispatch` either: a manual run can pick a PR branch's copy. | ||
| # | ||
| # A merge made with GITHUB_TOKEN starts no `push` workflow, so after each merge | ||
| # the sweep starts ci and checks on the default branch itself: a dispatch is one | ||
| # of the events that token may start. | ||
| # | ||
| # Nothing from a PR is checked out or run: the job reads PR metadata through | ||
| # the API and calls `gh pr merge`, and nothing else. | ||
| name: copilot-auto-merge | ||
|
|
||
| on: | ||
| schedule: | ||
| # Every 10 minutes: GitHub's schedule is best effort, so a run can lag. | ||
| - cron: "*/10 * * * *" | ||
|
|
||
| permissions: {} | ||
|
|
||
| # One sweep at a time; a queued one does the same thing, so none is cancelled. | ||
| concurrency: | ||
| group: ${{ github.workflow }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| sweep: | ||
| name: merge owner PRs that Copilot approved | ||
| timeout-minutes: 10 | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| actions: write # starting ci and checks on the merge | ||
| contents: write # the squash merge | ||
| pull-requests: write # merging a pull request | ||
| steps: | ||
| - name: Merge what Copilot's latest review approved | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| REPO: ${{ github.repository }} | ||
| OWNER: ${{ github.repository_owner }} | ||
| run: | | ||
| copilot='copilot-pull-request-reviewer[bot]' | ||
| failed=0 | ||
| # Read from the API: a schedule event's payload has no repository. | ||
| default_branch=$(gh api "repos/$REPO" --jq .default_branch) | ||
| [ -n "$default_branch" ] || exit 1 | ||
| # Empty fields print as "-": a tab is whitespace to `read`, so an | ||
| # empty field would shift the ones after it. | ||
| # Into the default branch only: the ruleset that makes a merge wait | ||
| # for the gates covers that branch alone. | ||
| prs=$(gh api --paginate \ | ||
| "repos/$REPO/pulls?state=open&base=$default_branch&per_page=100" \ | ||
| --jq '.[] | [.number, .user.login, (.head.repo.full_name // "-"), | ||
| .head.ref, .head.sha, .draft, | ||
| (.auto_merge.enabled_by.login // "-")] | @tsv') | ||
| while IFS=$'\t' read -r number author head_repo head_ref head_sha draft enabled_by <&3; do | ||
| [ -n "$number" ] || continue | ||
| # The owner's PRs from a branch in this repo, never a fork. | ||
| if [ "$author" != "$OWNER" ] || [ "$head_repo" != "$REPO" ]; then | ||
| continue | ||
| fi | ||
| case "$author" in dependabot\[bot\] | github-actions\[bot\]) continue ;; esac | ||
| case "$head_ref" in dependabot/* | release-please--*) continue ;; esac | ||
| # A draft is how the owner holds a PR back from this pilot. | ||
| [ "$draft" = false ] || continue | ||
| # Auto-merge the owner switched on is the owner's call. | ||
| [ "$enabled_by" = - ] || continue | ||
|
|
||
| # Per PR, so one failed call never stops the rest of the sweep. | ||
| # Reviews come oldest first, so the last line is Copilot's latest. | ||
| if ! reviews=$(gh api --paginate "repos/$REPO/pulls/$number/reviews?per_page=100" \ | ||
| --jq '.[] | select(.user.login == "copilot-pull-request-reviewer[bot]") | ||
| | "\(.state) \(.commit_id)"'); then | ||
| echo "::warning::#$number: could not read its reviews" | ||
| failed=1 | ||
| continue | ||
| fi | ||
| latest=$(printf '%s\n' "$reviews" | tail -n 1) | ||
| [ "$latest" = "APPROVED $head_sha" ] || continue | ||
|
|
||
| # Merge only once GitHub reports the PR mergeable with its required | ||
| # checks passed; until then the next sweep looks again. | ||
| if ! state=$(gh pr view "$number" --repo "$REPO" --json mergeStateStatus \ | ||
| --jq .mergeStateStatus); then | ||
| echo "::warning::#$number: could not read its merge state" | ||
| failed=1 | ||
| continue | ||
| fi | ||
| case "$state" in | ||
| CLEAN | UNSTABLE | HAS_HOOKS) ;; | ||
| *) continue ;; | ||
| esac | ||
| # GitHub refuses unless the head is still the approved commit. | ||
| if gh pr merge --squash --match-head-commit "$head_sha" \ | ||
| --repo "$REPO" "$number"; then | ||
| echo "::notice::#$number: merged $head_sha, which $copilot approved" | ||
| for workflow in ci.yml checks.yml; do | ||
| if ! gh workflow run "$workflow" --ref "$default_branch" --repo "$REPO"; then | ||
|
atdr marked this conversation as resolved.
Resolved
atdr marked this conversation as resolved.
Resolved
|
||
| echo "::warning::#$number: merged, but could not start $workflow on $default_branch" | ||
| failed=1 | ||
| fi | ||
| done | ||
| # A dispatch runs on the branch tip at that moment, so a merge | ||
| # landing before either one leaves this commit without that run. | ||
| # Checked after both: the tip only moves forward, so a tip still | ||
| # at the merge commit means both ran on it. Said loudly rather | ||
| # than threading a revision through every reusable workflow; the | ||
| # tip's runs still cover the combined result. | ||
| merged=$(gh pr view "$number" --repo "$REPO" --json mergeCommit \ | ||
| --jq .mergeCommit.oid || true) | ||
| tip=$(gh api "repos/$REPO/commits/$default_branch" --jq .sha || true) | ||
| if [ -z "$merged" ] || [ "$merged" != "$tip" ]; then | ||
| echo "::warning::#$number: $default_branch moved past its merge commit ${merged:-(unknown)} before its ci and checks runs started, so they may test ${tip:-a later commit} instead" | ||
| failed=1 | ||
| fi | ||
| else | ||
| echo "::warning::#$number: could not merge $head_sha" | ||
| failed=1 | ||
| fi | ||
| done 3<<EOF | ||
| $prs | ||
| EOF | ||
| exit "$failed" | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.