Repository: go-gitea/gitea
File: models/git/commit_status.go (Go)

Code before:
	MaxIndex int64  `xorm:"index"`
}

var getBase = func(ctx context.Context, repoID int64, sha string) *xorm.Session {
	return db.GetEngine(ctx).Table(&CommitStatus{}).
		Where("repo_id = ?", repoID).And("sha = ?", sha)
}

// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
	indices := make([]int64, 0, 10)
	sess := getBase(ctx, repoID, sha).
		Select("max( `index` ) as `index`").
		GroupBy("context_hash").
		OrderBy("max( `index` ) desc")
	if !listOptions.IsListAll() {
		sess = db.SetSessionPagination(sess, &listOptions)
	}
	if err := sess.Find(&indices); err != nil {
		return nil, err
	}
	statuses := make([]*CommitStatus, 0, len(indices))
	if len(indices) == 0 {
		return statuses, nil
	}
	return statuses, getBase(ctx, repoID, sha).And(builder.In("`index`", indices)).Find(&statuses)
}

func CountLatestCommitStatus(ctx context.Context, repoID int64, sha string) (int64, error) {
	return getBase(ctx, repoID, sha).
		Select("count(context_hash)").
		GroupBy("context_hash").
		Count()
}

// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
func GetLatestCommitStatusForPairs(ctx context.Context, repoSHAs []RepoSHA) (map[int64][]*CommitStatus, error) {
	type result struct {
		Index  int64
		RepoID int64
		SHA    string
	}

	results := make([]result, 0, len(repoSHAs))

	getBase := func() *xorm.Session {
		return db.GetEngine(ctx).Table(&CommitStatus{})
	}

	// Create a disjunction of conditions for each repoID and SHA pair
	conds := make([]builder.Cond, 0, len(repoSHAs))

Review comment (bug):
~~I do not think it is right.~~

~~`Find(&statuses)` might change the value of `statuses`, then the first returned `statuses` might be wrong.~~

Maybe Golang spec guarantees that the function call will be executed before reading the variable values (not sure), I still think we should avoid using such trick syntax.

Code after:
	MaxIndex int64  `xorm:"index"`
}

func makeRepoCommitQuery(ctx context.Context, repoID int64, sha string) *xorm.Session {
	return db.GetEngine(ctx).Table(&CommitStatus{}).
		Where("repo_id = ?", repoID).And("sha = ?", sha)
}

// GetLatestCommitStatus returns all statuses with a unique context for a given commit.
func GetLatestCommitStatus(ctx context.Context, repoID int64, sha string, listOptions db.ListOptions) ([]*CommitStatus, error) {
	indices := make([]int64, 0, 10)
	sess := makeRepoCommitQuery(ctx, repoID, sha).
		Select("max( `index` ) as `index`").
		GroupBy("context_hash").
		OrderBy("max( `index` ) desc")
	if !listOptions.IsListAll() {
		sess = db.SetSessionPagination(sess, &listOptions)
	}
	if err := sess.Find(&indices); err != nil {
		return nil, err
	}
	statuses := make([]*CommitStatus, 0, len(indices))
	if len(indices) == 0 {
		return statuses, nil
	}
	err := makeRepoCommitQuery(ctx, repoID, sha).And(builder.In("`index`", indices)).Find(&statuses)
	return statuses, err
}

func CountLatestCommitStatus(ctx context.Context, repoID int64, sha string) (int64, error) {
	return makeRepoCommitQuery(ctx, repoID, sha).
		Select("count(context_hash)").
		GroupBy("context_hash").
		Count()
}

// GetLatestCommitStatusForPairs returns all statuses with a unique context for a given list of repo-sha pairs
func GetLatestCommitStatusForPairs(ctx context.Context, repoSHAs []RepoSHA) (map[int64][]*CommitStatus, error) {
	type result struct {
		Index  int64
		RepoID int64
		SHA    string
	}

	results := make([]result, 0, len(repoSHAs))

	getBase := func() *xorm.Session {
		return db.GetEngine(ctx).Table(&CommitStatus{})
	}

	// Create a disjunction of conditions for each repoID and SHA pair