Skip to content

feat(repositories): add delete_branch tool - #2790

Open
huzaifa678 wants to merge 2 commits into
github:mainfrom
huzaifa678:add-delete-branch-tool
Open

huzaifa678 wants to merge 2 commits into
github:mainfrom
huzaifa678:add-delete-branch-tool

Conversation

@huzaifa678

Copy link
Copy Markdown

Description

Adds a delete_branch tool to the repositories toolset, resolving #1476.

The repo already exposes create_branch and list_branches but had no way to delete a branch. This fills that gap, following the existing create_branch implementation pattern.

Guardrail

As requested in the issue, the tool will not delete a protected branch. It fetches the branch first and, if protected is true, returns a tool error rather than attempting the deletion — giving the model actionable feedback.

Behavior

Aspect Value
Tool name delete_branch
Required params owner, repo, branch
ReadOnlyHint false
Scope repo

Flow:

  1. GET /repos/{owner}/{repo}/branches/{branch} → check protection status
  2. If protected → return error, do not delete
  3. Otherwise DELETE /repos/{owner}/{repo}/git/refs/heads/{branch}

Testing

Test_DeleteBranch covers:

  • successful deletion of an unprotected branch
  • refusal to delete a protected branch
  • branch not found
  • deletion API failure

Verified locally:

  • go test ./... — pass
  • golangci-lint run — 0 issues
  • script/generate-docs — README updated
  • UPDATE_TOOLSNAPS=true go test ./... — snapshot committed

Checklist

  • Tests added
  • script/lint passes
  • Toolsnap committed (pkg/github/__toolsnaps__/delete_branch.snap)
  • README regenerated via script/generate-docs
  • Change is focused on a single tool

Closes #1476

The repositories toolset exposes create_branch and list_branches but
provides no way to delete a branch. This adds a delete_branch tool that
fills that gap, following the existing create_branch implementation.

As a guardrail, the tool fetches the branch first and refuses to delete
protected branches, returning a clear error to the model instead of
attempting the deletion. It requires owner, repo, and branch, is scoped
to `repo`, and is annotated as a non-read-only operation.

Tests cover successful deletion, protected-branch refusal, branch not
found, and deletion failure.

Closes github#1476
@huzaifa678
huzaifa678 requested a review from a team as a code owner June 28, 2026 23:43
@JordyDevrix

Copy link
Copy Markdown

lgtm

Resolve conflicts from upstream's typed-tool migration:
- helper_test.go: keep both GetReposBranchesByOwnerByRepoByBranch (used by
  delete_branch) and the new DeleteReposByOwnerByRepo endpoint constant.
- repositories.go: migrate delete_branch to the new scope/output API —
  use scopes.RequireAll(scopes.Repo) and a concrete DeleteBranchOutput so
  the tool satisfies TestToolDefinitionsUseConcreteOutputTypes.
- Regenerate delete_branch toolsnap and README docs.
Copilot AI adaptive review requested due to automatic review settings November 11, 2026 21:32

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.

🟡 Changes recommended

Branch deletion can panic on transport-level failures, and its scope filter incorrectly hides the tool from users with public_repo-only PATs.

2 open issues
What changed in this PR

Adds delete_branch to the repositories toolset, complementing branch creation and listing and addressing #1476.

Changes:

  • Checks branch protection before attempting deletion and returns structured success output.
  • Registers the tool with required owner, repo, and branch parameters.
  • Adds behavioral tests, a tool schema snapshot, and generated documentation.
File Description
README.md Documents the tool, parameters, and OAuth scope.
pkg/​github/​tools.go Registers delete_branch in the tool inventory.
pkg/​github/​repositories.go Implements branch lookup, protection checks, and deletion.
pkg/​github/​repositories_test.go Tests successful deletion, protected branches, and API errors.
pkg/​github/​helper_test.go Adds branch lookup and reference deletion mock routes.
pkg/​github/​__toolsnaps__/​delete_branch.snap Records the new tool’s schema and annotations.

Review effort: Balanced (auto)

Required: []string{"owner", "repo", "branch"},
},
},
scopes.RequireAll(scopes.Repo),
err,
), nil, nil
}
defer func() { _ = resp.Body.Close() }()
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.

Query on availability of Delete Branch tool

3 participants