Skip to content

Commit c685eef

Browse files
authored
If a repository return no commitstatus, then still cache it but not query it from database (#30700)
The previous repository default branch commit status cache will only store if the commit status has value. So the repository which have no any commit status will always be fetched from database. This PR will store the empty state of commit status of a repository into cache because the cache will be updated once there is a commit status stored.
1 parent 4ff5493 commit c685eef

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

services/repository/commitstatus/commitstatus.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,10 @@ func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheVal
3838
if ok && statusStr != "" {
3939
var cv commitStatusCacheValue
4040
err := json.Unmarshal([]byte(statusStr), &cv)
41-
if err == nil && cv.State != "" {
41+
if err == nil {
4242
return &cv
4343
}
44-
if err != nil {
45-
log.Warn("getCommitStatusCache: json.Unmarshal failed: %v", err)
46-
}
44+
log.Warn("getCommitStatusCache: json.Unmarshal failed: %v", err)
4745
}
4846
return nil
4947
}
@@ -128,15 +126,22 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
128126
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
129127
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
130128
results := make([]*git_model.CommitStatus, len(repos))
129+
allCached := true
131130
for i, repo := range repos {
132131
if cv := getCommitStatusCache(repo.ID, repo.DefaultBranch); cv != nil {
133132
results[i] = &git_model.CommitStatus{
134133
State: api.CommitStatusState(cv.State),
135134
TargetURL: cv.TargetURL,
136135
}
136+
} else {
137+
allCached = false
137138
}
138139
}
139140

141+
if allCached {
142+
return results, nil
143+
}
144+
140145
// collect the latest commit of each repo
141146
// at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment
142147
repoBranchNames := make(map[int64]string, len(repos))
@@ -165,10 +170,10 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
165170
for i, repo := range repos {
166171
if repo.ID == summary.RepoID {
167172
results[i] = summary
168-
_ = slices.DeleteFunc(repoSHAs, func(repoSHA git_model.RepoSHA) bool {
173+
repoSHAs = slices.DeleteFunc(repoSHAs, func(repoSHA git_model.RepoSHA) bool {
169174
return repoSHA.RepoID == repo.ID
170175
})
171-
if results[i].State != "" {
176+
if results[i] != nil {
172177
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
173178
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
174179
}
@@ -177,6 +182,9 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
177182
}
178183
}
179184
}
185+
if len(repoSHAs) == 0 {
186+
return results, nil
187+
}
180188

181189
// call the database O(1) times to get the commit statuses for all repos
182190
repoToItsLatestCommitStatuses, err := git_model.GetLatestCommitStatusForPairs(ctx, repoSHAs)
@@ -187,7 +195,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
187195
for i, repo := range repos {
188196
if results[i] == nil {
189197
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
190-
if results[i].State != "" {
198+
if results[i] != nil {
191199
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
192200
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
193201
}

0 commit comments

Comments
 (0)