From 5930ab5fdf7a970fcca3cd50b44cf1cacb615a54 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Tue, 9 May 2023 18:34:07 -0700 Subject: [PATCH] Filter get single commit (#24613) Pretty much the same thing as #24568 but for getting a single commit instead of getting a list of commits --- routers/api/v1/repo/commits.go | 33 +++++++++++++++++---------------- services/convert/git_commit.go | 9 +++++++++ templates/swagger/v1_json.tmpl | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 9b7de91a72..20d4405d6d 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -42,6 +42,18 @@ func GetSingleCommit(ctx *context.APIContext) { // description: a git ref or commit sha // type: string // required: true + // - name: stat + // in: query + // description: include diff stats for every commit (disable for speedup, default 'true') + // type: boolean + // - name: verification + // in: query + // description: include verification for every commit (disable for speedup, default 'true') + // type: boolean + // - name: files + // in: query + // description: include a list of affected files for every commit (disable for speedup, default 'true') + // type: boolean // responses: // "200": // "$ref": "#/responses/Commit" @@ -55,10 +67,11 @@ func GetSingleCommit(ctx *context.APIContext) { ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha)) return } - getCommit(ctx, sha) + + getCommit(ctx, sha, convert.ParseCommitOptions(ctx)) } -func getCommit(ctx *context.APIContext, identifier string) { +func getCommit(ctx *context.APIContext, identifier string, toCommitOpts convert.ToCommitOptions) { commit, err := ctx.Repo.GitRepo.GetCommit(identifier) if err != nil { if git.IsErrNotExist(err) { @@ -69,7 +82,7 @@ func getCommit(ctx *context.APIContext, identifier string) { return } - json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, convert.ToCommitOptions{Stat: true}) + json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, toCommitOpts) if err != nil { ctx.Error(http.StatusInternalServerError, "toCommit", err) return @@ -240,24 +253,12 @@ func GetAllCommits(ctx *context.APIContext) { } pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize))) - userCache := make(map[string]*user_model.User) - apiCommits := make([]*api.Commit, len(commits)) - stat := ctx.FormString("stat") == "" || ctx.FormBool("stat") - verification := ctx.FormString("verification") == "" || ctx.FormBool("verification") - files := ctx.FormString("files") == "" || ctx.FormBool("files") - for i, commit := range commits { // Create json struct - apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, - convert.ToCommitOptions{ - Stat: stat, - Verification: verification, - Files: files, - }) - + apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, convert.ParseCommitOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "toCommit", err) return diff --git a/services/convert/git_commit.go b/services/convert/git_commit.go index 119237e0ca..e726011e02 100644 --- a/services/convert/git_commit.go +++ b/services/convert/git_commit.go @@ -10,6 +10,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + ctx "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" @@ -78,6 +79,14 @@ type ToCommitOptions struct { Files bool } +func ParseCommitOptions(ctx *ctx.APIContext) ToCommitOptions { + return ToCommitOptions{ + Stat: ctx.FormString("stat") == "" || ctx.FormBool("stat"), + Files: ctx.FormString("files") == "" || ctx.FormBool("files"), + Verification: ctx.FormString("verification") == "" || ctx.FormBool("verification"), + } +} + // ToCommit convert a git.Commit to api.Commit func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, commit *git.Commit, userCache map[string]*user_model.User, opts ToCommitOptions) (*api.Commit, error) { var apiAuthor, apiCommitter *api.User diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index e11fdeff80..35cbc71c8b 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -4498,6 +4498,24 @@ "name": "sha", "in": "path", "required": true + }, + { + "type": "boolean", + "description": "include diff stats for every commit (disable for speedup, default 'true')", + "name": "stat", + "in": "query" + }, + { + "type": "boolean", + "description": "include verification for every commit (disable for speedup, default 'true')", + "name": "verification", + "in": "query" + }, + { + "type": "boolean", + "description": "include a list of affected files for every commit (disable for speedup, default 'true')", + "name": "files", + "in": "query" } ], "responses": {