@@ -38,12 +38,10 @@ func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheVal
38
38
if ok && statusStr != "" {
39
39
var cv commitStatusCacheValue
40
40
err := json .Unmarshal ([]byte (statusStr ), & cv )
41
- if err == nil && cv . State != "" {
41
+ if err == nil {
42
42
return & cv
43
43
}
44
- if err != nil {
45
- log .Warn ("getCommitStatusCache: json.Unmarshal failed: %v" , err )
46
- }
44
+ log .Warn ("getCommitStatusCache: json.Unmarshal failed: %v" , err )
47
45
}
48
46
return nil
49
47
}
@@ -128,15 +126,22 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
128
126
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
129
127
func FindReposLastestCommitStatuses (ctx context.Context , repos []* repo_model.Repository ) ([]* git_model.CommitStatus , error ) {
130
128
results := make ([]* git_model.CommitStatus , len (repos ))
129
+ allCached := true
131
130
for i , repo := range repos {
132
131
if cv := getCommitStatusCache (repo .ID , repo .DefaultBranch ); cv != nil {
133
132
results [i ] = & git_model.CommitStatus {
134
133
State : api .CommitStatusState (cv .State ),
135
134
TargetURL : cv .TargetURL ,
136
135
}
136
+ } else {
137
+ allCached = false
137
138
}
138
139
}
139
140
141
+ if allCached {
142
+ return results , nil
143
+ }
144
+
140
145
// collect the latest commit of each repo
141
146
// at most there are dozens of repos (limited by MaxResponseItems), so it's not a big problem at the moment
142
147
repoBranchNames := make (map [int64 ]string , len (repos ))
@@ -165,10 +170,10 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
165
170
for i , repo := range repos {
166
171
if repo .ID == summary .RepoID {
167
172
results [i ] = summary
168
- _ = slices .DeleteFunc (repoSHAs , func (repoSHA git_model.RepoSHA ) bool {
173
+ repoSHAs = slices .DeleteFunc (repoSHAs , func (repoSHA git_model.RepoSHA ) bool {
169
174
return repoSHA .RepoID == repo .ID
170
175
})
171
- if results [i ]. State != "" {
176
+ if results [i ] != nil {
172
177
if err := updateCommitStatusCache (repo .ID , repo .DefaultBranch , results [i ].State , results [i ].TargetURL ); err != nil {
173
178
log .Error ("updateCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
174
179
}
@@ -177,6 +182,9 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
177
182
}
178
183
}
179
184
}
185
+ if len (repoSHAs ) == 0 {
186
+ return results , nil
187
+ }
180
188
181
189
// call the database O(1) times to get the commit statuses for all repos
182
190
repoToItsLatestCommitStatuses , err := git_model .GetLatestCommitStatusForPairs (ctx , repoSHAs )
@@ -187,7 +195,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
187
195
for i , repo := range repos {
188
196
if results [i ] == nil {
189
197
results [i ] = git_model .CalcCommitStatus (repoToItsLatestCommitStatuses [repo .ID ])
190
- if results [i ]. State != "" {
198
+ if results [i ] != nil {
191
199
if err := updateCommitStatusCache (repo .ID , repo .DefaultBranch , results [i ].State , results [i ].TargetURL ); err != nil {
192
200
log .Error ("updateCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
193
201
}
0 commit comments