diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index d7ec9b727..22ed2b216 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -307,21 +307,27 @@ func ListBranchesSlice(ctx *context.APIContext) { // description: name of the repo // type: string // required: true + // - name: page + // in: query + // description: page number of results to return (1-based) + // type: integer + // - name: limit + // in: query + // description: page size of results + // type: integer // responses: // "200": // "$ref": "#/responses/BranchList" - // listOptions := utils.GetListOptions(ctx) - // skip, _ := listOptions.GetStartEnd() - // branches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize) + listOptions := utils.GetListOptions(ctx) + skip, _ := listOptions.GetStartEnd() branches, totalNumOfBranches, err := repo_module.GetBranchesNoLimit(ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranches", err) return } - apiBranches := make([]*api.Branch, len(branches)) - apiBranchesList := []api.Branch{} + // apiBranchesSlice := []api.Branch{} for i := range branches { c, err := branches[i].GetCommit() if err != nil { @@ -338,19 +344,38 @@ func ListBranchesSlice(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) return } - apiBranchesList = append(apiBranchesList, *apiBranches[i]) - sort.Sort(api.SortBranch(apiBranchesList)) - } + sort.Sort(api.SortBranch(apiBranches)) + branchSlice := pageate(apiBranches, skip, listOptions.PageSize) + BranchesSlice := BranchesSliceByProtection(ctx, branchSlice) - // ctx.SetLinkHeader(int(totalNumOfBranches), listOptions.PageSize) + ctx.SetLinkHeader(int(totalNumOfBranches), listOptions.PageSize) ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", totalNumOfBranches)) ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") - // ctx.JSON(http.StatusOK, &apiBranches) - ctx.JSON(http.StatusOK, BranchesSliceByProtection(ctx, apiBranchesList)) + ctx.JSON(http.StatusOK, &BranchesSlice) + } -func BranchesSliceByProtection(ctx *context.APIContext, branchList []api.Branch) []api.BranchesSlice { +func pageate(branchSlice []*api.Branch, skip, pageSize int) []*api.Branch { + limit := func() int { + if skip+pageSize > len(branchSlice) { + return len(branchSlice) + } else { + return skip + pageSize + } + } + start := func() int { + if skip > len(branchSlice) { + return len(branchSlice) + } else { + return skip + } + } + return branchSlice[start():limit()] + +} + +func BranchesSliceByProtection(ctx *context.APIContext, branchList []*api.Branch) []api.BranchesSlice { // group by protection sort.Sort(api.SortBranch(branchList)) branchSlice := make([]api.BranchesSlice, 0)