Skip to content

Add branch listing, creation and deletion to the Bitbucket backend - #221

Merged
andrew merged 1 commit into
git-pkgs:mainfrom
NopeCharon:bitbucket-branches
Nov 21, 2026
Merged

andrew merged 1 commit into
git-pkgs:mainfrom
NopeCharon:bitbucket-branches

Conversation

@NopeCharon

@NopeCharon NopeCharon commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The Bitbucket Cloud backend returned ErrNotSupported for every BranchService method, so forge branch list, forge branch create and forge branch delete failed on bitbucket.org. This PR implements them with Bitbucket's /refs/branches endpoints.

How it works

  • List follows next links and supports Limit, PerPage and Page. Bitbucket rejects a pagelen above 100 with 400 Invalid pagelen, so the page size is capped at 100. The page size shrinks to Limit only when listing from the first page, because a start page is counted in pages of PerPage.
  • Create sends a commit hash as the target. Bitbucket's API reference documents the target as a commit hash (short prefixes allowed but may be ambiguous, full hashes preferred). It doesn't say a branch name is accepted, and the CLI passes main by default. So from is looked up as a branch, as the GitHub backend does, and a full commit SHA is used as is. A missing source branch returns ErrNotFound before anything is created.
  • Delete escapes the branch name into a single path segment. That is the only form in which the refs endpoints accept names containing a slash, such as feature/login.

Bitbucket's branch objects don't indicate whether a branch is the main branch or has restrictions (restrictions are a separate, admin-only API), so Default and Protected stay false, as on GitHub.

Testing

  • httptest coverage for pagination through next links, the limit stopping early, page sizes (including the 100 cap and a start page), not found, creation from a slashed branch and from a full SHA, a missing source branch, a rejected creation, deleting a slashed name, and read-only mode.
  • TestReplayListBranches replays a real api.bitbucket.org listing.
  • Ran forge branch list against bitbucket.org on a repository with 91 branches (all listed, and -L 5), and paged through the same repository with PerPage: 40 (3 pages, 91 branches; with Limit: 45 the result stops partway through the second page). Create and delete need an account with write access, so they're covered by the tests and the API reference.
  • go test ./..., go test -race ./bitbucket, the js/wasm and wasip1/wasm builds, and golangci-lint run (0 issues) all pass locally.

Summary by CodeRabbit

  • New Features
    • Bitbucket branch management is now supported: list branches, create branches from another branch or commit, and delete branches.
    • Branch listings support pagination and optional result limits, with page sizes capped at 100.
    • Branch names and commit hashes are shown in branch listings.
  • Bug Fixes
    • Branch operations now return relevant API errors, including not-found and read-only errors.

The Bitbucket Cloud backend returned ErrNotSupported for every
BranchService method, so `forge branch list`, `branch create` and
`branch delete` failed on bitbucket.org. They now use the
/refs/branches endpoints.

- List follows `next` links and honours Limit, PerPage and Page.
  Bitbucket rejects a pagelen above 100 with "Invalid pagelen", so the
  page size is capped there. It only shrinks to Limit when listing from
  the first page, since a start page is counted in pages of PerPage.
- Create needs a commit hash as the target: the API documents the
  target as a commit hash, preferably a full one. So `from` is looked
  up as a branch, as the GitHub backend does, and a full commit SHA is
  used as is. A missing source branch returns ErrNotFound before
  anything is created.
- Delete escapes the branch name into one path segment, the only form
  in which the refs endpoints accept names containing a slash such as
  "feature/login".

Branch objects don't say whether a branch is the main branch or has
restrictions, so Default and Protected stay false, as on GitHub.

Tests cover pagination, limits, page sizes, slashed names, creation
from a branch and from a commit, error responses and read-only mode,
plus a replay of a real api.bitbucket.org branch listing.
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Bitbucket forge now supports branch listing with pagination and limits, branch creation from a source branch or full commit SHA, and branch deletion. Tests cover request handling, response mapping, errors, read-only writes, and a recorded branch-list response.

Changes: Bitbucket branch operations

Layer / File(s) Summary
Branch service and listing
bitbucket/branches.go, bitbucket/branches_test.go, bitbucket/replay_test.go, bitbucket/testdata/replay/TestReplayListBranches.json
The branch service uses the forge’s token and HTTP client. Listing maps branch names and target hashes, follows pagination, applies page-size and result limits, and returns request errors. Tests cover query options, authorization, not-found responses, and a recorded list response.
Branch creation
bitbucket/branches.go, bitbucket/branches_test.go
Creation uses a full commit SHA directly or resolves a source branch to its target hash. It posts the new branch and maps the response. Tests cover escaped branch paths, rejected requests, and missing source branches.
Branch deletion
bitbucket/branches.go, bitbucket/branches_test.go
Deletion sends a request for the named branch. Tests cover escaped paths, missing branches, and read-only write errors.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ForgeClient
  participant BranchService
  participant BitbucketAPI
  ForgeClient->>BranchService: Create branch from a name or full commit SHA
  BranchService->>BitbucketAPI: GET source branch when from is not a full SHA
  BitbucketAPI-->>BranchService: Return source branch target hash
  BranchService->>BitbucketAPI: POST new branch name and target hash
  BitbucketAPI-->>BranchService: Return created branch
  BranchService-->>ForgeClient: Return forge.Branch
Loading

Possibly related PRs

  • git-pkgs/forge#206: Defines the read-only HTTP client behavior that the branch-write tests verify.

Suggested reviewers: andrew

Merge Risk: Minimal · up to ef9f0

This adds Bitbucket branch listing, creation and deletion in place of unsupported errors. Tests cover pagination, limits, error cases and read-only mode, and no merge-blocking issue was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title states the main change: the Bitbucket backend gains branch listing, creation, and deletion. The raw summary and objectives confirm that these three operations replace the earlier ErrNotSuppo…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the main change: the Bitbucket backend gains branch listing, creation, and deletion. The raw summary and objectives confirm that these three operations replace the earlier ErrNotSupported responses. The title is short, specific, and matches the changeset.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 9cb2564c-8809-4674-ba75-692c08d946c5
Commits

Reviewing files that changed from the base of the PR and between c046578 and ef9f0f1.

Files selected for processing: 4

  • bitbucket/branches.go
  • bitbucket/branches_test.go
  • bitbucket/replay_test.go
  • bitbucket/testdata/replay/TestReplayListBranches.json

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@andrew
andrew merged commit 06b1b28 into git-pkgs:main Nov 21, 2026
6 checks passed
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