Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter get single commit #24613

Merged
merged 6 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions routers/api/v1/repo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions services/convert/git_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.