From 05799c06d93305857b3410cdaba2686715485740 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 12:56:42 -0700 Subject: [PATCH 1/7] Add options for stat --- routers/api/v1/repo/commits.go | 12 ++++++++++-- routers/api/v1/repo/notes.go | 2 +- routers/api/v1/repo/pull.go | 2 +- services/convert/git_commit.go | 24 ++++++++++++++++++++---- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 401403c83dee3..9b72969dc0406 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -69,7 +69,7 @@ func getCommit(ctx *context.APIContext, identifier string) { return } - json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, true) + json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, convert.ToCommitOptions{Stat: true}) if err != nil { ctx.Error(http.StatusInternalServerError, "toCommit", err) return @@ -219,10 +219,18 @@ func GetAllCommits(ctx *context.APIContext) { 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, stat) + apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, + convert.ToCommitOptions{ + Stat: stat, + Verification: verification, + Files: files, + }) + if err != nil { ctx.Error(http.StatusInternalServerError, "toCommit", err) return diff --git a/routers/api/v1/repo/notes.go b/routers/api/v1/repo/notes.go index 74969f2cad7c8..1bd66101f0d68 100644 --- a/routers/api/v1/repo/notes.go +++ b/routers/api/v1/repo/notes.go @@ -78,7 +78,7 @@ func getNote(ctx *context.APIContext, identifier string) { return } - cmt, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, note.Commit, nil, true) + cmt, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, note.Commit, nil, convert.ToCommitOptions{Stat: true}) if err != nil { ctx.Error(http.StatusInternalServerError, "ToCommit", err) return diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index f4e2969d7d1c2..a507c1f44d171 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1318,7 +1318,7 @@ func GetPullRequestCommits(ctx *context.APIContext) { apiCommits := make([]*api.Commit, 0, end-start) for i := start; i < end; i++ { - apiCommit, err := convert.ToCommit(ctx, ctx.Repo.Repository, baseGitRepo, commits[i], userCache, true) + apiCommit, err := convert.ToCommit(ctx, ctx.Repo.Repository, baseGitRepo, commits[i], userCache, convert.ToCommitOptions{Stat: true}) if err != nil { ctx.ServerError("toCommit", err) return diff --git a/services/convert/git_commit.go b/services/convert/git_commit.go index 20fb8c2565398..87fefa0f27abb 100644 --- a/services/convert/git_commit.go +++ b/services/convert/git_commit.go @@ -72,8 +72,14 @@ func ToPayloadCommit(ctx context.Context, repo *repo_model.Repository, c *git.Co } } +type ToCommitOptions struct { + Stat bool + Verification bool + Files bool +} + // 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, stat bool) (*api.Commit, error) { +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 // Retrieve author and committer information @@ -162,19 +168,24 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep SHA: commit.ID.String(), Created: commit.Committer.When, }, - Verification: ToVerification(ctx, commit), }, Author: apiAuthor, Committer: apiCommitter, Parents: apiParents, } + // Retrieve verification for commit + if opts.Verification { + res.RepoCommit.Verification = ToVerification(ctx, commit) + } + // Retrieve files affected by the commit - if stat { + if opts.Files { fileStatus, err := git.GetCommitFileStatus(gitRepo.Ctx, repo.RepoPath(), commit.ID.String()) if err != nil { return nil, err } + affectedFileList := make([]*api.CommitAffectedFiles, 0, len(fileStatus.Added)+len(fileStatus.Removed)+len(fileStatus.Modified)) for _, files := range [][]string{fileStatus.Added, fileStatus.Removed, fileStatus.Modified} { for _, filename := range files { @@ -184,14 +195,19 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep } } + res.Files = affectedFileList + } + + // Get diff stats for commit + if opts.Stat { diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{ AfterCommitID: commit.ID.String(), }) + if err != nil { return nil, err } - res.Files = affectedFileList res.Stats = &api.CommitStats{ Total: diff.TotalAddition + diff.TotalDeletion, Additions: diff.TotalAddition, From 12b658cb7367d0c0082bbab5b2cbfefe45c5e9ca Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 12:58:25 -0700 Subject: [PATCH 2/7] Add comment on param --- routers/api/v1/repo/commits.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/routers/api/v1/repo/commits.go b/routers/api/v1/repo/commits.go index 9b72969dc0406..56b61d399b315 100644 --- a/routers/api/v1/repo/commits.go +++ b/routers/api/v1/repo/commits.go @@ -107,6 +107,14 @@ func GetAllCommits(ctx *context.APIContext) { // 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 // - name: page // in: query // description: page number of results to return (1-based) From fc9d5f55473c7370b9676e830b9b2a3ab776b0cd Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 12:58:53 -0700 Subject: [PATCH 3/7] Format --- services/convert/git_commit.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/convert/git_commit.go b/services/convert/git_commit.go index 87fefa0f27abb..119237e0ca030 100644 --- a/services/convert/git_commit.go +++ b/services/convert/git_commit.go @@ -203,7 +203,6 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep diff, err := gitdiff.GetDiff(gitRepo, &gitdiff.DiffOptions{ AfterCommitID: commit.ID.String(), }) - if err != nil { return nil, err } From bfbf27bffb717478fee78b66b6d9bf965f61e106 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 13:15:29 -0700 Subject: [PATCH 4/7] Add test --- .../integration/api_repo_git_commits_test.go | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/integration/api_repo_git_commits_test.go b/tests/integration/api_repo_git_commits_test.go index 17750a2799f6c..d22cada846a6e 100644 --- a/tests/integration/api_repo_git_commits_test.go +++ b/tests/integration/api_repo_git_commits_test.go @@ -110,6 +110,27 @@ func TestAPIReposGitCommitListDifferentBranch(t *testing.T) { compareCommitFiles(t, []string{"readme.md"}, apiData[0].Files) } +func TestAPIReposGitCommitListWithoutSelectFields(t *testing.T) { + defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session) + + // Test getting commits without files, verification, and stats + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign&stats=false&files=false&verification=false", user.Name) + resp := MakeRequest(t, req, http.StatusOK) + + var apiData []api.Commit + DecodeJSON(t, resp, &apiData) + + assert.Len(t, apiData, 1) + assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) + assert.Equal(t, nil, apiData[0].Stats) + assert.Equal(t, nil, apiData[0].RepoCommit.Verification) + assert.Equal(t, nil, apiData[0].Files) +} + func TestDownloadCommitDiffOrPatch(t *testing.T) { defer tests.PrepareTestEnv(t)() user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) From 89a03865fae4c6d245a8e97828ff33ac17d0d79c Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 13:21:58 -0700 Subject: [PATCH 5/7] Cast nil pointers --- tests/integration/api_repo_git_commits_test.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/integration/api_repo_git_commits_test.go b/tests/integration/api_repo_git_commits_test.go index d22cada846a6e..75aebb60a7d1b 100644 --- a/tests/integration/api_repo_git_commits_test.go +++ b/tests/integration/api_repo_git_commits_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/tests" @@ -118,7 +119,7 @@ func TestAPIReposGitCommitListWithoutSelectFields(t *testing.T) { token := getTokenForLoggedInUser(t, session) // Test getting commits without files, verification, and stats - req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign&stats=false&files=false&verification=false", user.Name) + req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo16/commits?token="+token+"&sha=good-sign&stat=false&files=false&verification=false", user.Name) resp := MakeRequest(t, req, http.StatusOK) var apiData []api.Commit @@ -126,9 +127,9 @@ func TestAPIReposGitCommitListWithoutSelectFields(t *testing.T) { assert.Len(t, apiData, 1) assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) - assert.Equal(t, nil, apiData[0].Stats) - assert.Equal(t, nil, apiData[0].RepoCommit.Verification) - assert.Equal(t, nil, apiData[0].Files) + assert.Equal(t, (*structs.CommitStats)(nil), apiData[0].Stats) + assert.Equal(t, (*api.PayloadCommitVerification)(nil), apiData[0].RepoCommit.Verification) + assert.Equal(t, ([]*api.CommitAffectedFiles)(nil), apiData[0].Files) } func TestDownloadCommitDiffOrPatch(t *testing.T) { From 914b8460cf3e19daddb689207dd69c2f926cb1e1 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 13:23:19 -0700 Subject: [PATCH 6/7] Update swagger --- templates/swagger/v1_json.tmpl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 7923e93ca4d23..99c49fec9cd31 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -3794,6 +3794,18 @@ "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" + }, { "type": "integer", "description": "page number of results to return (1-based)", From a83c907622cb8f833ab693ddffccd0146ab853e6 Mon Sep 17 00:00:00 2001 From: Matthew Walowski Date: Sat, 6 May 2023 13:33:07 -0700 Subject: [PATCH 7/7] Fix test imports --- tests/integration/api_repo_git_commits_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/api_repo_git_commits_test.go b/tests/integration/api_repo_git_commits_test.go index 75aebb60a7d1b..4722ea2d81ebb 100644 --- a/tests/integration/api_repo_git_commits_test.go +++ b/tests/integration/api_repo_git_commits_test.go @@ -9,7 +9,6 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/tests" @@ -127,7 +126,7 @@ func TestAPIReposGitCommitListWithoutSelectFields(t *testing.T) { assert.Len(t, apiData, 1) assert.Equal(t, "f27c2b2b03dcab38beaf89b0ab4ff61f6de63441", apiData[0].CommitMeta.SHA) - assert.Equal(t, (*structs.CommitStats)(nil), apiData[0].Stats) + assert.Equal(t, (*api.CommitStats)(nil), apiData[0].Stats) assert.Equal(t, (*api.PayloadCommitVerification)(nil), apiData[0].RepoCommit.Verification) assert.Equal(t, ([]*api.CommitAffectedFiles)(nil), apiData[0].Files) }