Skip to content

Commit 3e0ffda

Browse files
committed
If a repository return no commitstatus, then still cache it but not query it from database (go-gitea#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 224c48e commit 3e0ffda

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

services/repository/commitstatus/commitstatus.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,10 @@ func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheVal
3939
if ok && statusStr != "" {
4040
var cv commitStatusCacheValue
4141
err := json.Unmarshal([]byte(statusStr), &cv)
42-
if err == nil && cv.State != "" {
42+
if err == nil {
4343
return &cv
4444
}
45-
if err != nil {
46-
log.Warn("getCommitStatusCache: json.Unmarshal failed: %v", err)
47-
}
45+
log.Warn("getCommitStatusCache: json.Unmarshal failed: %v", err)
4846
}
4947
return nil
5048
}
@@ -126,15 +124,22 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
126124
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
127125
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
128126
results := make([]*git_model.CommitStatus, len(repos))
127+
allCached := true
129128
for i, repo := range repos {
130129
if cv := getCommitStatusCache(repo.ID, repo.DefaultBranch); cv != nil {
131130
results[i] = &git_model.CommitStatus{
132131
State: api.CommitStatusState(cv.State),
133132
TargetURL: cv.TargetURL,
134133
}
134+
} else {
135+
allCached = false
135136
}
136137
}
137138

139+
if allCached {
140+
return results, nil
141+
}
142+
138143
// collect the latest commit of each repo
139144
// at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment
140145
repoBranchNames := make(map[int64]string, len(repos))
@@ -158,7 +163,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
158163
for i, repo := range repos {
159164
if results[i] == nil {
160165
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
161-
if results[i].State != "" {
166+
if results[i] != nil {
162167
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
163168
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
164169
}

0 commit comments

Comments
 (0)