对分支进行分页 #51

Merged
wonderful merged 1 commits from wonderful/gitea-1156:develop into develop 2022-04-25 11:37:48 +08:00
1 changed files with 37 additions and 12 deletions

View File

@ -307,21 +307,27 @@ func ListBranchesSlice(ctx *context.APIContext) {
// description: name of the repo // description: name of the repo
// type: string // type: string
// required: true // 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: // responses:
// "200": // "200":
// "$ref": "#/responses/BranchList" // "$ref": "#/responses/BranchList"
// listOptions := utils.GetListOptions(ctx) listOptions := utils.GetListOptions(ctx)
// skip, _ := listOptions.GetStartEnd() skip, _ := listOptions.GetStartEnd()
// branches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize)
branches, totalNumOfBranches, err := repo_module.GetBranchesNoLimit(ctx.Repo.Repository) branches, totalNumOfBranches, err := repo_module.GetBranchesNoLimit(ctx.Repo.Repository)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "GetBranches", err) ctx.Error(http.StatusInternalServerError, "GetBranches", err)
return return
} }
apiBranches := make([]*api.Branch, len(branches)) apiBranches := make([]*api.Branch, len(branches))
apiBranchesList := []api.Branch{} // apiBranchesSlice := []api.Branch{}
for i := range branches { for i := range branches {
c, err := branches[i].GetCommit() c, err := branches[i].GetCommit()
if err != nil { if err != nil {
@ -338,19 +344,38 @@ func ListBranchesSlice(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
return 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("X-Total-Count", fmt.Sprintf("%d", totalNumOfBranches))
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link") ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
// ctx.JSON(http.StatusOK, &apiBranches) ctx.JSON(http.StatusOK, &BranchesSlice)
ctx.JSON(http.StatusOK, BranchesSliceByProtection(ctx, apiBranchesList))
} }
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 // group by protection
sort.Sort(api.SortBranch(branchList)) sort.Sort(api.SortBranch(branchList))
branchSlice := make([]api.BranchesSlice, 0) branchSlice := make([]api.BranchesSlice, 0)