feat(api): add q parameter to list branches API for server-side filtering (#37982)

The GET /repos/{owner}/{repo}/branches endpoint currently has no way to
filter branches by name server-side, forcing API consumers to paginate
through all branches and filter client-side.

The UI already supports branch search (added in
[#27055](https://github.com/go-gitea/gitea/pull/27055)). The underlying
DB layer has a Keyword field on FindBranchOptions in
models/git/branch_list.go that does a LIKE %keyword% SQL filter, it just
wasn't wired up to the API handler.

This PR exposes a ?q= query parameter on the endpoint that maps to
FindBranchOptions.Keyword.

Example:

```GET /repos/owner/repo/branches?q=feature ```
Closes #37981

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Harsh Mahajan
2026-06-04 04:51:48 +05:30
committed by GitHub
parent b2748d7654
commit 792fa5eeba
5 changed files with 41 additions and 0 deletions

View File

@@ -306,6 +306,10 @@ func ListBranches(ctx *context.APIContext) {
// in: query
// description: page size of results
// type: integer
// - name: q
// in: query
// description: branch name substring to filter by
// type: string
// responses:
// "200":
// "$ref": "#/responses/BranchList"
@@ -314,6 +318,7 @@ func ListBranches(ctx *context.APIContext) {
var apiBranches []*api.Branch
listOptions := utils.GetListOptions(ctx)
keyword := ctx.FormString("q")
if !ctx.Repo.Repository.IsEmpty {
if ctx.Repo.GitRepo == nil {
@@ -325,6 +330,7 @@ func ListBranches(ctx *context.APIContext) {
ListOptions: listOptions,
RepoID: ctx.Repo.Repository.ID,
IsDeletedBranch: optional.Some(false),
Keyword: keyword,
}
var err error
totalNumOfBranches, err = db.Count[git_model.Branch](ctx, branchOpts)