Fix ListBranches to handle empty case (#21921)

Fix #21910

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
pull/21787/head^2
Lunny Xiao 2 years ago committed by GitHub
parent 9eb9cf5153
commit 36cbaec54c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      routers/api/v1/repo/branch.go

@ -251,42 +251,50 @@ func ListBranches(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/BranchList" // "$ref": "#/responses/BranchList"
var totalNumOfBranches int
var apiBranches []*api.Branch
listOptions := utils.GetListOptions(ctx) listOptions := utils.GetListOptions(ctx)
skip, _ := listOptions.GetStartEnd()
branches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
return
}
apiBranches := make([]*api.Branch, 0, len(branches)) if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
for i := range branches { skip, _ := listOptions.GetStartEnd()
c, err := branches[i].GetCommit() branches, total, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
if err != nil { if err != nil {
// Skip if this branch doesn't exist anymore. ctx.Error(http.StatusInternalServerError, "GetBranches", err)
if git.IsErrNotExist(err) {
totalNumOfBranches--
continue
}
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return return
} }
branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
if err != nil { apiBranches = make([]*api.Branch, 0, len(branches))
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) for i := range branches {
return c, err := branches[i].GetCommit()
} if err != nil {
apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) // Skip if this branch doesn't exist anymore.
if err != nil { if git.IsErrNotExist(err) {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) total--
return continue
}
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
}
branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
return
}
apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
if err != nil {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
return
}
apiBranches = append(apiBranches, apiBranch)
} }
apiBranches = append(apiBranches, apiBranch)
totalNumOfBranches = total
} }
ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize) ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize)
ctx.SetTotalCountHeader(int64(totalNumOfBranches)) ctx.SetTotalCountHeader(int64(totalNumOfBranches))
ctx.JSON(http.StatusOK, &apiBranches) ctx.JSON(http.StatusOK, apiBranches)
} }
// GetBranchProtection gets a branch protection // GetBranchProtection gets a branch protection

Loading…
Cancel
Save