2024-03-06 17:47:19 +05:30
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package commitstatus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2024-04-12 07:11:50 +05:30
|
|
|
"slices"
|
2024-03-06 17:47:19 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2024-04-12 16:32:42 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2024-03-06 17:47:19 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
"code.gitea.io/gitea/services/automerge"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getCacheKey(repoID int64, brancheName string) string {
|
|
|
|
hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%d:%s", repoID, brancheName)))
|
|
|
|
return fmt.Sprintf("commit_status:%x", hashBytes)
|
|
|
|
}
|
|
|
|
|
2024-04-12 16:32:42 +05:30
|
|
|
type commitStatusCacheValue struct {
|
|
|
|
State string `json:"state"`
|
|
|
|
TargetURL string `json:"target_url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheValue {
|
|
|
|
c := cache.GetCache()
|
|
|
|
statusStr, ok := c.Get(getCacheKey(repoID, branchName)).(string)
|
|
|
|
if ok && statusStr != "" {
|
|
|
|
var cv commitStatusCacheValue
|
|
|
|
err := json.Unmarshal([]byte(statusStr), &cv)
|
|
|
|
if err == nil && cv.State != "" {
|
|
|
|
return &cv
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("getCommitStatusCache: json.Unmarshal failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateCommitStatusCache(repoID int64, branchName string, state api.CommitStatusState, targetURL string) error {
|
2024-03-06 17:47:19 +05:30
|
|
|
c := cache.GetCache()
|
2024-04-12 16:32:42 +05:30
|
|
|
bs, err := json.Marshal(commitStatusCacheValue{
|
|
|
|
State: state.String(),
|
|
|
|
TargetURL: targetURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("updateCommitStatusCache: json.Marshal failed: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.Put(getCacheKey(repoID, branchName), string(bs), 3*24*60)
|
2024-03-06 17:47:19 +05:30
|
|
|
}
|
|
|
|
|
2024-04-12 16:32:42 +05:30
|
|
|
func deleteCommitStatusCache(repoID int64, branchName string) error {
|
2024-03-06 17:47:19 +05:30
|
|
|
c := cache.GetCache()
|
|
|
|
return c.Delete(getCacheKey(repoID, branchName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCommitStatus creates a new CommitStatus given a bunch of parameters
|
|
|
|
// NOTE: All text-values will be trimmed from whitespaces.
|
|
|
|
// Requires: Repo, Creator, SHA
|
|
|
|
func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creator *user_model.User, sha string, status *git_model.CommitStatus) error {
|
|
|
|
repoPath := repo.RepoPath()
|
|
|
|
|
|
|
|
// confirm that commit is exist
|
|
|
|
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("OpenRepository[%s]: %w", repoPath, err)
|
|
|
|
}
|
|
|
|
defer closer.Close()
|
|
|
|
|
|
|
|
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetCommit(sha)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetCommit[%s]: %w", sha, err)
|
|
|
|
}
|
|
|
|
if len(sha) != objectFormat.FullLength() {
|
|
|
|
// use complete commit sha
|
|
|
|
sha = commit.ID.String()
|
|
|
|
}
|
|
|
|
|
2024-04-12 07:11:50 +05:30
|
|
|
if err := db.WithTx(ctx, func(ctx context.Context) error {
|
|
|
|
if err := git_model.NewCommitStatus(ctx, git_model.NewCommitStatusOptions{
|
|
|
|
Repo: repo,
|
|
|
|
Creator: creator,
|
|
|
|
SHA: commit.ID,
|
|
|
|
CommitStatus: status,
|
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return git_model.UpdateCommitStatusSummary(ctx, repo.ID, commit.ID.String())
|
2024-03-06 17:47:19 +05:30
|
|
|
}); err != nil {
|
2024-04-12 07:11:50 +05:30
|
|
|
return err
|
2024-03-06 17:47:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
defaultBranchCommit, err := gitRepo.GetBranchCommit(repo.DefaultBranch)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetBranchCommit[%s]: %w", repo.DefaultBranch, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if commit.ID.String() == defaultBranchCommit.ID.String() { // since one commit status updated, the combined commit status should be invalid
|
2024-04-12 16:32:42 +05:30
|
|
|
if err := deleteCommitStatusCache(repo.ID, repo.DefaultBranch); err != nil {
|
2024-03-06 17:47:19 +05:30
|
|
|
log.Error("deleteCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.State.IsSuccess() {
|
2024-05-21 20:53:22 +05:30
|
|
|
if err := automerge.StartPRCheckAndAutoMergeBySHA(ctx, sha, repo); err != nil {
|
2024-03-06 17:47:19 +05:30
|
|
|
return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:19:37 +05:30
|
|
|
// FindReposLastestCommitStatuses loading repository default branch latest combined commit status with cache
|
2024-03-06 17:47:19 +05:30
|
|
|
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
|
2024-05-01 01:02:23 +05:30
|
|
|
if len(repos) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-03-06 17:47:19 +05:30
|
|
|
results := make([]*git_model.CommitStatus, len(repos))
|
|
|
|
for i, repo := range repos {
|
2024-04-12 16:32:42 +05:30
|
|
|
if cv := getCommitStatusCache(repo.ID, repo.DefaultBranch); cv != nil {
|
|
|
|
results[i] = &git_model.CommitStatus{
|
|
|
|
State: api.CommitStatusState(cv.State),
|
|
|
|
TargetURL: cv.TargetURL,
|
|
|
|
}
|
2024-03-06 17:47:19 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect the latest commit of each repo
|
|
|
|
// at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment
|
|
|
|
repoBranchNames := make(map[int64]string, len(repos))
|
|
|
|
for i, repo := range repos {
|
|
|
|
if results[i] == nil {
|
|
|
|
repoBranchNames[repo.ID] = repo.DefaultBranch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("FindBranchesByRepoAndBranchName: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-12 07:11:50 +05:30
|
|
|
var repoSHAs []git_model.RepoSHA
|
|
|
|
for id, sha := range repoIDsToLatestCommitSHAs {
|
|
|
|
repoSHAs = append(repoSHAs, git_model.RepoSHA{RepoID: id, SHA: sha})
|
|
|
|
}
|
|
|
|
|
|
|
|
summaryResults, err := git_model.GetLatestCommitStatusForRepoAndSHAs(ctx, repoSHAs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GetLatestCommitStatusForRepoAndSHAs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, summary := range summaryResults {
|
|
|
|
for i, repo := range repos {
|
|
|
|
if repo.ID == summary.RepoID {
|
|
|
|
results[i] = summary
|
|
|
|
_ = slices.DeleteFunc(repoSHAs, func(repoSHA git_model.RepoSHA) bool {
|
|
|
|
return repoSHA.RepoID == repo.ID
|
|
|
|
})
|
|
|
|
if results[i].State != "" {
|
2024-04-12 16:32:42 +05:30
|
|
|
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
|
2024-04-12 07:11:50 +05:30
|
|
|
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-06 17:47:19 +05:30
|
|
|
// call the database O(1) times to get the commit statuses for all repos
|
2024-04-12 07:11:50 +05:30
|
|
|
repoToItsLatestCommitStatuses, err := git_model.GetLatestCommitStatusForPairs(ctx, repoSHAs)
|
2024-03-06 17:47:19 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("GetLatestCommitStatusForPairs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, repo := range repos {
|
|
|
|
if results[i] == nil {
|
|
|
|
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
|
2024-04-16 15:32:01 +05:30
|
|
|
if results[i] != nil && results[i].State != "" {
|
2024-04-12 16:32:42 +05:30
|
|
|
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
|
2024-03-06 17:47:19 +05:30
|
|
|
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|