1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-26 09:09:36 -05:00

Fix GetCommitStatuses (#28787) (#28804)

Backport #28787

Replaces #28802
This commit is contained in:
KN4CK3R 2024-01-15 19:30:12 +01:00 committed by GitHub
parent cbf366643b
commit ae99233db0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 37 deletions

View file

@ -25,7 +25,6 @@ import (
"code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/translation"
"xorm.io/builder" "xorm.io/builder"
"xorm.io/xorm"
) )
// CommitStatus holds a single Status of a single Commit // CommitStatus holds a single Status of a single Commit
@ -220,60 +219,58 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
// CommitStatusOptions holds the options for query commit statuses // CommitStatusOptions holds the options for query commit statuses
type CommitStatusOptions struct { type CommitStatusOptions struct {
db.ListOptions db.ListOptions
RepoID int64
SHA string
State string State string
SortType string SortType string
} }
// GetCommitStatuses returns all statuses for a given commit. func (opts *CommitStatusOptions) ToConds() builder.Cond {
func GetCommitStatuses(ctx context.Context, repo *repo_model.Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) { var cond builder.Cond = builder.Eq{
if opts.Page <= 0 { "repo_id": opts.RepoID,
opts.Page = 1 "sha": opts.SHA,
}
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
}
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 { switch opts.State {
case "pending", "success", "error", "failure", "warning": 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) { func (opts *CommitStatusOptions) ToOrders() string {
switch sortType { switch opts.SortType {
case "oldest": case "oldest":
sess.Asc("created_unix") return "created_unix ASC"
case "recentupdate": case "recentupdate":
sess.Desc("updated_unix") return "updated_unix DESC"
case "leastupdate": case "leastupdate":
sess.Asc("updated_unix") return "updated_unix ASC"
case "leastindex": case "leastindex":
sess.Desc("index") return "`index` DESC"
case "highestindex": case "highestindex":
sess.Asc("index") return "`index` ASC"
default: default:
sess.Desc("created_unix") return "created_unix DESC"
} }
} }
// GetCommitStatuses returns all statuses for a given commit.
func GetCommitStatuses(ctx context.Context, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
sess := db.GetEngine(ctx).
Where(opts.ToConds()).
OrderBy(opts.ToOrders())
db.SetSessionPagination(sess, opts)
statuses := make([]*CommitStatus, 0, opts.PageSize)
count, err := sess.FindAndCount(&statuses)
return statuses, count, err
}
// CommitStatusIndex represents a table for commit status index // CommitStatusIndex represents a table for commit status index
type CommitStatusIndex struct { type CommitStatusIndex struct {
ID int64 ID int64

View file

@ -22,7 +22,11 @@ func TestGetCommitStatuses(t *testing.T) {
sha1 := "1234123412341234123412341234123412341234" 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 := git_model.GetCommitStatuses(db.DefaultContext, &git_model.CommitStatusOptions{
ListOptions: db.ListOptions{Page: 1, PageSize: 50},
RepoID: repo1.ID,
SHA: sha1,
})
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, int(maxResults), 5) assert.Equal(t, int(maxResults), 5)
assert.Len(t, statuses, 5) assert.Len(t, statuses, 5)
@ -46,4 +50,13 @@ func TestGetCommitStatuses(t *testing.T) {
assert.Equal(t, "deploy/awesomeness", statuses[4].Context) assert.Equal(t, "deploy/awesomeness", statuses[4].Context)
assert.Equal(t, structs.CommitStatusError, statuses[4].State) 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)) assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[4].APIURL(db.DefaultContext))
statuses, maxResults, err = git_model.GetCommitStatuses(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)
} }

View file

@ -194,8 +194,10 @@ func getCommitStatuses(ctx *context.APIContext, sha string) {
listOptions := utils.GetListOptions(ctx) listOptions := utils.GetListOptions(ctx)
statuses, maxResults, err := git_model.GetCommitStatuses(ctx, repo, sha, &git_model.CommitStatusOptions{ statuses, maxResults, err := git_model.GetCommitStatuses(ctx, &git_model.CommitStatusOptions{
ListOptions: listOptions, ListOptions: listOptions,
RepoID: repo.ID,
SHA: sha,
SortType: ctx.FormTrim("sort"), SortType: ctx.FormTrim("sort"),
State: ctx.FormTrim("state"), State: ctx.FormTrim("state"),
}) })