Skip to content

Commit 5930ab5

Browse files
authored
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
1 parent 9a0652f commit 5930ab5

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

routers/api/v1/repo/commits.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ func GetSingleCommit(ctx *context.APIContext) {
4242
// description: a git ref or commit sha
4343
// type: string
4444
// required: true
45+
// - name: stat
46+
// in: query
47+
// description: include diff stats for every commit (disable for speedup, default 'true')
48+
// type: boolean
49+
// - name: verification
50+
// in: query
51+
// description: include verification for every commit (disable for speedup, default 'true')
52+
// type: boolean
53+
// - name: files
54+
// in: query
55+
// description: include a list of affected files for every commit (disable for speedup, default 'true')
56+
// type: boolean
4557
// responses:
4658
// "200":
4759
// "$ref": "#/responses/Commit"
@@ -55,10 +67,11 @@ func GetSingleCommit(ctx *context.APIContext) {
5567
ctx.Error(http.StatusUnprocessableEntity, "no valid ref or sha", fmt.Sprintf("no valid ref or sha: %s", sha))
5668
return
5769
}
58-
getCommit(ctx, sha)
70+
71+
getCommit(ctx, sha, convert.ParseCommitOptions(ctx))
5972
}
6073

61-
func getCommit(ctx *context.APIContext, identifier string) {
74+
func getCommit(ctx *context.APIContext, identifier string, toCommitOpts convert.ToCommitOptions) {
6275
commit, err := ctx.Repo.GitRepo.GetCommit(identifier)
6376
if err != nil {
6477
if git.IsErrNotExist(err) {
@@ -69,7 +82,7 @@ func getCommit(ctx *context.APIContext, identifier string) {
6982
return
7083
}
7184

72-
json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, convert.ToCommitOptions{Stat: true})
85+
json, err := convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, nil, toCommitOpts)
7386
if err != nil {
7487
ctx.Error(http.StatusInternalServerError, "toCommit", err)
7588
return
@@ -240,24 +253,12 @@ func GetAllCommits(ctx *context.APIContext) {
240253
}
241254

242255
pageCount := int(math.Ceil(float64(commitsCountTotal) / float64(listOptions.PageSize)))
243-
244256
userCache := make(map[string]*user_model.User)
245-
246257
apiCommits := make([]*api.Commit, len(commits))
247258

248-
stat := ctx.FormString("stat") == "" || ctx.FormBool("stat")
249-
verification := ctx.FormString("verification") == "" || ctx.FormBool("verification")
250-
files := ctx.FormString("files") == "" || ctx.FormBool("files")
251-
252259
for i, commit := range commits {
253260
// Create json struct
254-
apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache,
255-
convert.ToCommitOptions{
256-
Stat: stat,
257-
Verification: verification,
258-
Files: files,
259-
})
260-
261+
apiCommits[i], err = convert.ToCommit(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, commit, userCache, convert.ParseCommitOptions(ctx))
261262
if err != nil {
262263
ctx.Error(http.StatusInternalServerError, "toCommit", err)
263264
return

services/convert/git_commit.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
repo_model "code.gitea.io/gitea/models/repo"
1212
user_model "code.gitea.io/gitea/models/user"
13+
ctx "code.gitea.io/gitea/modules/context"
1314
"code.gitea.io/gitea/modules/git"
1415
"code.gitea.io/gitea/modules/log"
1516
api "code.gitea.io/gitea/modules/structs"
@@ -78,6 +79,14 @@ type ToCommitOptions struct {
7879
Files bool
7980
}
8081

82+
func ParseCommitOptions(ctx *ctx.APIContext) ToCommitOptions {
83+
return ToCommitOptions{
84+
Stat: ctx.FormString("stat") == "" || ctx.FormBool("stat"),
85+
Files: ctx.FormString("files") == "" || ctx.FormBool("files"),
86+
Verification: ctx.FormString("verification") == "" || ctx.FormBool("verification"),
87+
}
88+
}
89+
8190
// ToCommit convert a git.Commit to api.Commit
8291
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) {
8392
var apiAuthor, apiCommitter *api.User

templates/swagger/v1_json.tmpl

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)