|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package commitstatus |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "crypto/sha256" |
| 9 | + "fmt" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + git_model "code.gitea.io/gitea/models/git" |
| 13 | + repo_model "code.gitea.io/gitea/models/repo" |
| 14 | + user_model "code.gitea.io/gitea/models/user" |
| 15 | + "code.gitea.io/gitea/modules/cache" |
| 16 | + "code.gitea.io/gitea/modules/git" |
| 17 | + "code.gitea.io/gitea/modules/gitrepo" |
| 18 | + "code.gitea.io/gitea/modules/log" |
| 19 | + api "code.gitea.io/gitea/modules/structs" |
| 20 | + "code.gitea.io/gitea/services/automerge" |
| 21 | +) |
| 22 | + |
| 23 | +func getCacheKey(repoID int64, brancheName string) string { |
| 24 | + hashBytes := sha256.Sum256([]byte(fmt.Sprintf("%d:%s", repoID, brancheName))) |
| 25 | + return fmt.Sprintf("commit_status:%x", hashBytes) |
| 26 | +} |
| 27 | + |
| 28 | +func updateCommitStatusCache(ctx context.Context, repoID int64, branchName string, status api.CommitStatusState) error { |
| 29 | + c := cache.GetCache() |
| 30 | + return c.Put(getCacheKey(repoID, branchName), string(status), 3*24*60) |
| 31 | +} |
| 32 | + |
| 33 | +func deleteCommitStatusCache(ctx context.Context, repoID int64, branchName string) error { |
| 34 | + c := cache.GetCache() |
| 35 | + return c.Delete(getCacheKey(repoID, branchName)) |
| 36 | +} |
| 37 | + |
| 38 | +// CreateCommitStatus creates a new CommitStatus given a bunch of parameters |
| 39 | +// NOTE: All text-values will be trimmed from whitespaces. |
| 40 | +// Requires: Repo, Creator, SHA |
| 41 | +func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creator *user_model.User, sha string, status *git_model.CommitStatus) error { |
| 42 | + repoPath := repo.RepoPath() |
| 43 | + |
| 44 | + // confirm that commit is exist |
| 45 | + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("OpenRepository[%s]: %w", repoPath, err) |
| 48 | + } |
| 49 | + defer closer.Close() |
| 50 | + |
| 51 | + objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName) |
| 52 | + |
| 53 | + commit, err := gitRepo.GetCommit(sha) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("GetCommit[%s]: %w", sha, err) |
| 56 | + } |
| 57 | + if len(sha) != objectFormat.FullLength() { |
| 58 | + // use complete commit sha |
| 59 | + sha = commit.ID.String() |
| 60 | + } |
| 61 | + |
| 62 | + if err := git_model.NewCommitStatus(ctx, git_model.NewCommitStatusOptions{ |
| 63 | + Repo: repo, |
| 64 | + Creator: creator, |
| 65 | + SHA: commit.ID, |
| 66 | + CommitStatus: status, |
| 67 | + }); err != nil { |
| 68 | + return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err) |
| 69 | + } |
| 70 | + |
| 71 | + defaultBranchCommit, err := gitRepo.GetBranchCommit(repo.DefaultBranch) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("GetBranchCommit[%s]: %w", repo.DefaultBranch, err) |
| 74 | + } |
| 75 | + |
| 76 | + if commit.ID.String() == defaultBranchCommit.ID.String() { // since one commit status updated, the combined commit status should be invalid |
| 77 | + if err := deleteCommitStatusCache(ctx, repo.ID, repo.DefaultBranch); err != nil { |
| 78 | + log.Error("deleteCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if status.State.IsSuccess() { |
| 83 | + if err := automerge.MergeScheduledPullRequest(ctx, sha, repo); err != nil { |
| 84 | + return fmt.Errorf("MergeScheduledPullRequest[repo_id: %d, user_id: %d, sha: %s]: %w", repo.ID, creator.ID, sha, err) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache |
| 92 | +func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) { |
| 93 | + results := make([]*git_model.CommitStatus, len(repos)) |
| 94 | + c := cache.GetCache() |
| 95 | + |
| 96 | + for i, repo := range repos { |
| 97 | + status, ok := c.Get(getCacheKey(repo.ID, repo.DefaultBranch)).(string) |
| 98 | + if ok && status != "" { |
| 99 | + results[i] = &git_model.CommitStatus{State: api.CommitStatusState(status)} |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // collect the latest commit of each repo |
| 104 | + // at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment |
| 105 | + repoBranchNames := make(map[int64]string, len(repos)) |
| 106 | + for i, repo := range repos { |
| 107 | + if results[i] == nil { |
| 108 | + repoBranchNames[repo.ID] = repo.DefaultBranch |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + repoIDsToLatestCommitSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, repoBranchNames) |
| 113 | + if err != nil { |
| 114 | + return nil, fmt.Errorf("FindBranchesByRepoAndBranchName: %v", err) |
| 115 | + } |
| 116 | + |
| 117 | + // call the database O(1) times to get the commit statuses for all repos |
| 118 | + repoToItsLatestCommitStatuses, err := git_model.GetLatestCommitStatusForPairs(ctx, repoIDsToLatestCommitSHAs, db.ListOptionsAll) |
| 119 | + if err != nil { |
| 120 | + return nil, fmt.Errorf("GetLatestCommitStatusForPairs: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + for i, repo := range repos { |
| 124 | + if results[i] == nil { |
| 125 | + results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID]) |
| 126 | + if results[i].State != "" { |
| 127 | + if err := updateCommitStatusCache(ctx, repo.ID, repo.DefaultBranch, results[i].State); err != nil { |
| 128 | + log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err) |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return results, nil |
| 135 | +} |
0 commit comments