Skip to content

disable automerge when adding existing PRs to stack#120

Open
skarim wants to merge 1 commit into
skarim/api-token-warningfrom
skarim/disable-automerge-on-adopt
Open

disable automerge when adding existing PRs to stack#120
skarim wants to merge 1 commit into
skarim/api-token-warningfrom
skarim/disable-automerge-on-adopt

Conversation

@skarim

@skarim skarim commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Disable auto-merge on existing PRs during submit

When gh stack submit discovers an existing PR for a branch, that PR may have auto-merge enabled. Auto-merge is incompatible with stacked PRs because the PR would merge independently, breaking the stack's base chain.

Previously, users could work around the link command's auto-merge guard by running init on a branch with an auto-merge-enabled PR, then submit to add it to a stack. The submit command had no auto-merge check, so the PR was adopted without any validation.

This change adds auto-merge detection to submit's ensurePR function. When an existing PR with auto-merge enabled is found, the
CLI automatically disables auto-merge via the disablePullRequestAutoMerge GraphQL mutation and warns the user. If the disable call fails, submit continues with a warning (non-fatal).

Changes

  • internal/github/github.go: add DisableAutoMerge() method using the disablePullRequestAutoMerge GraphQL mutation
  • internal/github/client_interface.go: add DisableAutoMerge to ClientOps interface
  • internal/github/mock_client.go: add DisableAutoMergeFn field and mock implementation
  • cmd/submit.go: in ensurePR, detect auto-merge-enabled PRs and disable auto-merge before adding to the stack
  • cmd/submit_test.go: 3 new tests — auto-merge disabled successfully, disable failure continues with warning, and no-op when auto-merge is not enabled

Stack created with GitHub Stacks CLIGive Feedback 💬

When a user runs `gh stack submit` and an existing PR is discovered for
a branch via `FindPRForBranch`, that PR may have auto-merge enabled.
Auto-merge is incompatible with stacked PRs because the PR would merge
on its own, breaking the stack's base chain.

Previously, the eligibility guard for auto-merge was only in the `link`
command (which blocks such PRs with an error). The `submit` command had
no such check, allowing users to add auto-merge-enabled PRs to a stack
by running `init` followed by `submit`.

This change adds auto-merge detection and automatic disabling in
`submit`'s `ensurePR` function. When an existing PR with auto-merge
enabled is discovered, the CLI disables auto-merge via the
`disablePullRequestAutoMerge` GraphQL mutation and warns the user.
If the disable call fails, submit continues with a warning (non-fatal).

The `link` command retains its stricter behavior of blocking auto-merge
PRs outright, since the user explicitly chose those PRs and can fix
them before retrying.

Changes:

  internal/github/github.go:
  - Add DisableAutoMerge() method using the
    disablePullRequestAutoMerge GraphQL mutation

  internal/github/client_interface.go:
  - Add DisableAutoMerge(prID string) error to ClientOps interface

  internal/github/mock_client.go:
  - Add DisableAutoMergeFn field and mock implementation

  cmd/submit.go:
  - In ensurePR, after discovering an existing PR with auto-merge
    enabled, call DisableAutoMerge before proceeding. Warns on
    success ("Disabled auto-merge for PR #N (incompatible with
    stacked PRs)") and on failure ("failed to disable auto-merge").

  cmd/submit_test.go:
  - Add TestSubmit_DisablesAutoMergeOnExistingPR: verifies auto-merge
    is disabled and warning is shown
  - Add TestSubmit_DisableAutoMergeFailure_ContinuesWithWarning:
    verifies submit continues even if the disable call fails
  - Add TestSubmit_NoAutoMerge_SkipsDisable: verifies DisableAutoMerge
    is not called for PRs without auto-merge
@skarim skarim marked this pull request as ready for review June 12, 2026 06:28
Copilot AI review requested due to automatic review settings June 12, 2026 06:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR closes a loophole where users could bypass the link command's auto-merge guard by running init on a branch with an auto-merge-enabled PR and then submit to adopt it into a stack. The fix adds auto-merge detection and automatic disabling during submit's PR reconciliation phase.

Changes:

  • Adds a DisableAutoMerge GraphQL mutation method following the same pattern as MarkPRReadyForReview
  • Integrates auto-merge detection in ensurePR as a non-fatal check that warns the user when auto-merge is disabled
  • Includes three test cases covering success, failure (continues with warning), and no-op scenarios
Show a summary per file
File Description
internal/github/github.go New DisableAutoMerge() method using disablePullRequestAutoMerge GraphQL mutation
internal/github/client_interface.go Adds DisableAutoMerge to the ClientOps interface
internal/github/mock_client.go Adds DisableAutoMergeFn field and mock implementation
cmd/submit.go Detects and disables auto-merge on existing PRs in ensurePR
cmd/submit_test.go Three new tests covering all auto-merge disable scenarios

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 5/5 changed files
  • Comments generated: 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants