Fix ListBranches to handle empty case (#21921)
Fix #21910 Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
This commit is contained in:
parent
9eb9cf5153
commit
36cbaec54c
1 changed files with 36 additions and 28 deletions
|
@ -251,21 +251,26 @@ 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)
|
||||||
|
|
||||||
|
if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
|
||||||
skip, _ := listOptions.GetStartEnd()
|
skip, _ := listOptions.GetStartEnd()
|
||||||
branches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
|
branches, total, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
|
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
apiBranches := make([]*api.Branch, 0, len(branches))
|
apiBranches = make([]*api.Branch, 0, len(branches))
|
||||||
for i := range branches {
|
for i := range branches {
|
||||||
c, err := branches[i].GetCommit()
|
c, err := branches[i].GetCommit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Skip if this branch doesn't exist anymore.
|
// Skip if this branch doesn't exist anymore.
|
||||||
if git.IsErrNotExist(err) {
|
if git.IsErrNotExist(err) {
|
||||||
totalNumOfBranches--
|
total--
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
|
||||||
|
@ -273,7 +278,7 @@ func ListBranches(ctx *context.APIContext) {
|
||||||
}
|
}
|
||||||
branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
|
branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
|
ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
|
||||||
|
@ -284,9 +289,12 @@ func ListBranches(ctx *context.APIContext) {
|
||||||
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…
Reference in a new issue