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

Fix GetCommitStatuses (#28787) #28802

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
54 changes: 19 additions & 35 deletions models/git/commit_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"code.gitea.io/gitea/modules/translation"

"xorm.io/builder"
"xorm.io/xorm"
)

// CommitStatus holds a single Status of a single Commit
Expand Down Expand Up @@ -220,57 +219,42 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
// CommitStatusOptions holds the options for query commit statuses
type CommitStatusOptions struct {
db.ListOptions
RepoID int64
SHA string
State string
SortType string
}

// GetCommitStatuses returns all statuses for a given commit.
func GetCommitStatuses(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
if opts.Page <= 0 {
opts.Page = 1
}
if opts.PageSize <= 0 {
opts.Page = setting.ItemsPerPage
}

countSession := listCommitStatusesStatement(ctx, repo, sha, opts)
countSession = db.SetSessionPagination(countSession, opts)
maxResults, err := countSession.Count(new(CommitStatus))
if err != nil {
log.Error("Count PRs: %v", err)
return nil, maxResults, err
func (opts *CommitStatusOptions) ToConds() builder.Cond {
var cond builder.Cond = builder.Eq{
"repo_id": opts.RepoID,
"sha": opts.SHA,
}

statuses := make([]*CommitStatus, 0, opts.PageSize)
findSession := listCommitStatusesStatement(ctx, repo, sha, opts)
findSession = db.SetSessionPagination(findSession, opts)
sortCommitStatusesSession(findSession, opts.SortType)
return statuses, maxResults, findSession.Find(&statuses)
}

func listCommitStatusesStatement(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) *xorm.Session {
sess := db.GetEngine(ctx).Where("repo_id = ?", repo.ID).And("sha = ?", sha)
switch opts.State {
case "pending", "success", "error", "failure", "warning":
sess.And("state = ?", opts.State)
cond = cond.And(builder.Eq{
"state": opts.State,
})
}
return sess

return cond
}

func sortCommitStatusesSession(sess *xorm.Session, sortType string) {
switch sortType {
func (opts *CommitStatusOptions) ToOrders() string {
switch opts.SortType {
case "oldest":
sess.Asc("created_unix")
return "created_unix ASC"
case "recentupdate":
sess.Desc("updated_unix")
return "updated_unix DESC"
case "leastupdate":
sess.Asc("updated_unix")
return "updated_unix ASC"
case "leastindex":
sess.Desc("index")
return "`index` DESC"
case "highestindex":
sess.Asc("index")
return "`index` ASC"
default:
sess.Desc("created_unix")
return "created_unix DESC"
}
}

Expand Down
15 changes: 14 additions & 1 deletion models/git/commit_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@

sha1 := "1234123412341234123412341234123412341234"

statuses, maxResults, err := git_model.GetCommitStatuses(db.DefaultContext, repo1, sha1, &git_model.CommitStatusOptions{ListOptions: db.ListOptions{Page: 1, PageSize: 50}})
statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](db.DefaultContext, &git_model.CommitStatusOptions{

Check failure on line 25 in models/git/commit_status_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 25 in models/git/commit_status_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 25 in models/git/commit_status_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values
ListOptions: db.ListOptions{Page: 1, PageSize: 50},
RepoID: repo1.ID,
SHA: sha1,
})
assert.NoError(t, err)
assert.Equal(t, int(maxResults), 5)
assert.Len(t, statuses, 5)
Expand All @@ -46,4 +50,13 @@
assert.Equal(t, "deploy/awesomeness", statuses[4].Context)
assert.Equal(t, structs.CommitStatusError, statuses[4].State)
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[4].APIURL(db.DefaultContext))

statuses, maxResults, err = db.FindAndCount[git_model.CommitStatus](db.DefaultContext, &git_model.CommitStatusOptions{
ListOptions: db.ListOptions{Page: 2, PageSize: 50},
RepoID: repo1.ID,
SHA: sha1,
})
assert.NoError(t, err)
assert.Equal(t, int(maxResults), 5)
assert.Empty(t, statuses)
}
5 changes: 4 additions & 1 deletion routers/api/v1/repo/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"fmt"
"net/http"

"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
Expand Down Expand Up @@ -194,11 +195,13 @@

listOptions := utils.GetListOptions(ctx)

statuses, maxResults, err := git_model.GetCommitStatuses(ctx, repo, sha, &git_model.CommitStatusOptions{
statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](ctx, &git_model.CommitStatusOptions{

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / backend

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / backend

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-sqlite

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-sqlite

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-mssql

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-mssql

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-pgsql

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-mysql5

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-mysql5

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-unit

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-unit

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-mysql8

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-mysql8

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / checks-backend

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-backend

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-backend

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-backend

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-e2e

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / test-e2e

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 3 variables but db.FindAndCount[git_model.CommitStatus] returns 2 values

Check failure on line 198 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]
ListOptions: listOptions,
RepoID: repo.ID,
SHA: sha,
SortType: ctx.FormTrim("sort"),
State: ctx.FormTrim("state"),
})

Check failure on line 204 in routers/api/v1/repo/status.go

View workflow job for this annotation

GitHub Actions / checks-backend

not enough arguments in call to db.FindAndCount[git_model.CommitStatus]
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommitStatuses", fmt.Errorf("GetCommitStatuses[%s, %s, %d]: %w", repo.FullName(), sha, ctx.FormInt("page"), err))
return
Expand Down
Loading