From 7d2d8b2f38e186ad065f091b1f86c85788149699 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 24 Nov 2022 15:24:43 +0800 Subject: [PATCH 1/3] Fix get branches --- routers/api/v1/repo/branch.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index d10a308df3a79..766528045c45c 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -252,6 +252,11 @@ func ListBranches(ctx *context.APIContext) { // "200": // "$ref": "#/responses/BranchList" + if ctx.Repo.Repository.IsEmpty || ctx.Repo.GitRepo == nil { + ctx.JSON(http.StatusOK, &[]*api.Branch{}) + return + } + listOptions := utils.GetListOptions(ctx) skip, _ := listOptions.GetStartEnd() branches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize) From d142221b5f0f9751a9e9d84a6082e70cb2635991 Mon Sep 17 00:00:00 2001 From: KN4CK3R Date: Thu, 24 Nov 2022 12:23:35 +0100 Subject: [PATCH 2/3] Update branch.go --- routers/api/v1/repo/branch.go | 63 ++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 766528045c45c..e4e30004aac3b 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -252,47 +252,50 @@ func ListBranches(ctx *context.APIContext) { // "200": // "$ref": "#/responses/BranchList" - if ctx.Repo.Repository.IsEmpty || ctx.Repo.GitRepo == nil { - ctx.JSON(http.StatusOK, &[]*api.Branch{}) - return - } + var totalNumOfBranches int64 + var apiBranches []*api.Branch 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)) - for i := range branches { - c, err := branches[i].GetCommit() - if err != nil { - // Skip if this branch doesn't exist anymore. - if git.IsErrNotExist(err) { - totalNumOfBranches-- - continue - } - ctx.Error(http.StatusInternalServerError, "GetCommit", err) - return - } - branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name) + 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 { - ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) + ctx.Error(http.StatusInternalServerError, "GetBranches", 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 = 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 + } + 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.SetTotalCountHeader(int64(totalNumOfBranches)) - ctx.JSON(http.StatusOK, &apiBranches) + ctx.JSON(http.StatusOK, apiBranches) } // GetBranchProtection gets a branch protection From 8187c016ce1a192a31746a85725e95e7924c2294 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 24 Nov 2022 22:04:13 +0800 Subject: [PATCH 3/3] Fix lint --- routers/api/v1/repo/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index e4e30004aac3b..34721fb70de91 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -252,7 +252,7 @@ func ListBranches(ctx *context.APIContext) { // "200": // "$ref": "#/responses/BranchList" - var totalNumOfBranches int64 + var totalNumOfBranches int var apiBranches []*api.Branch listOptions := utils.GetListOptions(ctx)