mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	* Fix #7136: Add paging and extend API GetCommitStatuses * update swagger * Update routers/api/v1/repo/status.go Co-Authored-By: techknowlogick <matti@mdranta.net> * Update routers/api/v1/repo/status.go Co-Authored-By: techknowlogick <matti@mdranta.net> * Update routers/api/v1/repo/status.go Co-Authored-By: techknowlogick <matti@mdranta.net> * Apply suggestions from code review
This commit is contained in:
		@@ -14,6 +14,8 @@ import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	api "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
	"code.gitea.io/gitea/modules/util"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// CommitStatusState holds the state of a Status
 | 
			
		||||
@@ -132,10 +134,57 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
 | 
			
		||||
	return lastStatus
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CommitStatusOptions holds the options for query commit statuses
 | 
			
		||||
type CommitStatusOptions struct {
 | 
			
		||||
	Page     int
 | 
			
		||||
	State    string
 | 
			
		||||
	SortType string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetCommitStatuses returns all statuses for a given commit.
 | 
			
		||||
func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
 | 
			
		||||
	statuses := make([]*CommitStatus, 0, 10)
 | 
			
		||||
	return statuses, x.Limit(10, page*10).Where("repo_id = ?", repo.ID).And("sha = ?", sha).Find(&statuses)
 | 
			
		||||
func GetCommitStatuses(repo *Repository, sha string, opts *CommitStatusOptions) ([]*CommitStatus, int64, error) {
 | 
			
		||||
	if opts.Page <= 0 {
 | 
			
		||||
		opts.Page = 1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	countSession := listCommitStatusesStatement(repo, sha, opts)
 | 
			
		||||
	maxResults, err := countSession.Count(new(CommitStatus))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("Count PRs: %v", err)
 | 
			
		||||
		return nil, maxResults, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	statuses := make([]*CommitStatus, 0, ItemsPerPage)
 | 
			
		||||
	findSession := listCommitStatusesStatement(repo, sha, opts)
 | 
			
		||||
	sortCommitStatusesSession(findSession, opts.SortType)
 | 
			
		||||
	findSession.Limit(ItemsPerPage, (opts.Page-1)*ItemsPerPage)
 | 
			
		||||
	return statuses, maxResults, findSession.Find(&statuses)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func listCommitStatusesStatement(repo *Repository, sha string, opts *CommitStatusOptions) *xorm.Session {
 | 
			
		||||
	sess := x.Where("repo_id = ?", repo.ID).And("sha = ?", sha)
 | 
			
		||||
	switch opts.State {
 | 
			
		||||
	case "pending", "success", "error", "failure", "warning":
 | 
			
		||||
		sess.And("state = ?", opts.State)
 | 
			
		||||
	}
 | 
			
		||||
	return sess
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func sortCommitStatusesSession(sess *xorm.Session, sortType string) {
 | 
			
		||||
	switch sortType {
 | 
			
		||||
	case "oldest":
 | 
			
		||||
		sess.Asc("created_unix")
 | 
			
		||||
	case "recentupdate":
 | 
			
		||||
		sess.Desc("updated_unix")
 | 
			
		||||
	case "leastupdate":
 | 
			
		||||
		sess.Asc("updated_unix")
 | 
			
		||||
	case "leastindex":
 | 
			
		||||
		sess.Desc("index")
 | 
			
		||||
	case "highestindex":
 | 
			
		||||
		sess.Asc("index")
 | 
			
		||||
	default:
 | 
			
		||||
		sess.Desc("created_unix")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user