From 250f87db597c8c3c4829af5c15ede7fd0c420b0c Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 6 Jul 2024 10:43:34 +0200 Subject: [PATCH] feat(api): An `order_by` param for user.ListMyRepos Add an optional `order_by` parameter to the `user.ListMyRepos` handler (which handles the `/api/v1/user/repos` route), allowing a user to sort repos by name (the default), id, or size. The latter will be useful later for figuring out which repos use most space, which repos eat most into a user's quota. Signed-off-by: Gergely Nagy --- routers/api/v1/user/repo.go | 19 +++++++++++++++++++ templates/swagger/v1_json.tmpl | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/routers/api/v1/user/repo.go b/routers/api/v1/user/repo.go index 9b6701b067..86716ff44f 100644 --- a/routers/api/v1/user/repo.go +++ b/routers/api/v1/user/repo.go @@ -99,9 +99,15 @@ func ListMyRepos(ctx *context.APIContext) { // in: query // description: page size of results // type: integer + // - name: order_by + // in: query + // description: order the repositories by name (default), id, or size + // type: string // responses: // "200": // "$ref": "#/responses/RepositoryList" + // "422": + // "$ref": "#/responses/validationError" opts := &repo_model.SearchRepoOptions{ ListOptions: utils.GetListOptions(ctx), @@ -110,6 +116,19 @@ func ListMyRepos(ctx *context.APIContext) { Private: ctx.IsSigned, IncludeDescription: true, } + orderBy := ctx.FormTrim("order_by") + switch orderBy { + case "name": + opts.OrderBy = "name ASC" + case "size": + opts.OrderBy = "size DESC" + case "id": + opts.OrderBy = "id ASC" + case "": + default: + ctx.Error(http.StatusUnprocessableEntity, "", "invalid order_by") + return + } var err error repos, count, err := repo_model.SearchRepository(ctx, opts) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 156c48d951..47b40b6d8a 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -17529,11 +17529,20 @@ "description": "page size of results", "name": "limit", "in": "query" + }, + { + "type": "string", + "description": "order the repositories by name (default), id, or size", + "name": "order_by", + "in": "query" } ], "responses": { "200": { "$ref": "#/responses/RepositoryList" + }, + "422": { + "$ref": "#/responses/validationError" } } },