Fix ListBranches to handle empty case (#21921) (#22025)

Fix #21910
Backport #21921

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>

Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
This commit is contained in:
Lunny Xiao 2022-12-05 05:57:33 +08:00 committed by GitHub
parent 82d50af721
commit 8188cdfcd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -252,42 +252,50 @@ func ListBranches(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/BranchList" // "$ref": "#/responses/BranchList"
listOptions := utils.GetListOptions(ctx) var totalNumOfBranches int
skip, _ := listOptions.GetStartEnd() var apiBranches []*api.Branch
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)) listOptions := utils.GetListOptions(ctx)
for i := range branches {
c, err := branches[i].GetCommit() if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
skip, _ := listOptions.GetStartEnd()
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) { return
totalNumOfBranches-- }
continue
apiBranches = make([]*api.Branch, 0, len(branches))
for i := range branches {
c, err := branches[i].GetCommit()
if err != nil {
// Skip if this branch doesn't exist anymore.
if git.IsErrNotExist(err) {
total--
continue
}
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
} }
ctx.Error(http.StatusInternalServerError, "GetCommit", err) branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
return 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)
} }
branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
if err != nil { totalNumOfBranches = total
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", 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)
} }
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