@@ -16,6 +16,7 @@ import (
16
16
"code.gitea.io/gitea/modules/cache"
17
17
"code.gitea.io/gitea/modules/git"
18
18
"code.gitea.io/gitea/modules/gitrepo"
19
+ "code.gitea.io/gitea/modules/json"
19
20
"code.gitea.io/gitea/modules/log"
20
21
api "code.gitea.io/gitea/modules/structs"
21
22
"code.gitea.io/gitea/services/automerge"
@@ -26,12 +27,41 @@ func getCacheKey(repoID int64, brancheName string) string {
26
27
return fmt .Sprintf ("commit_status:%x" , hashBytes )
27
28
}
28
29
29
- func updateCommitStatusCache (ctx context.Context , repoID int64 , branchName string , status api.CommitStatusState ) error {
30
+ type commitStatusCacheValue struct {
31
+ State string `json:"state"`
32
+ TargetURL string `json:"target_url"`
33
+ }
34
+
35
+ func getCommitStatusCache (repoID int64 , branchName string ) * commitStatusCacheValue {
36
+ c := cache .GetCache ()
37
+ statusStr , ok := c .Get (getCacheKey (repoID , branchName )).(string )
38
+ if ok && statusStr != "" {
39
+ var cv commitStatusCacheValue
40
+ err := json .Unmarshal ([]byte (statusStr ), & cv )
41
+ if err == nil && cv .State != "" {
42
+ return & cv
43
+ }
44
+ if err != nil {
45
+ log .Warn ("getCommitStatusCache: json.Unmarshal failed: %v" , err )
46
+ }
47
+ }
48
+ return nil
49
+ }
50
+
51
+ func updateCommitStatusCache (repoID int64 , branchName string , state api.CommitStatusState , targetURL string ) error {
30
52
c := cache .GetCache ()
31
- return c .Put (getCacheKey (repoID , branchName ), string (status ), 3 * 24 * 60 )
53
+ bs , err := json .Marshal (commitStatusCacheValue {
54
+ State : state .String (),
55
+ TargetURL : targetURL ,
56
+ })
57
+ if err != nil {
58
+ log .Warn ("updateCommitStatusCache: json.Marshal failed: %v" , err )
59
+ return nil
60
+ }
61
+ return c .Put (getCacheKey (repoID , branchName ), string (bs ), 3 * 24 * 60 )
32
62
}
33
63
34
- func deleteCommitStatusCache (ctx context. Context , repoID int64 , branchName string ) error {
64
+ func deleteCommitStatusCache (repoID int64 , branchName string ) error {
35
65
c := cache .GetCache ()
36
66
return c .Delete (getCacheKey (repoID , branchName ))
37
67
}
@@ -81,7 +111,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
81
111
}
82
112
83
113
if commit .ID .String () == defaultBranchCommit .ID .String () { // since one commit status updated, the combined commit status should be invalid
84
- if err := deleteCommitStatusCache (ctx , repo .ID , repo .DefaultBranch ); err != nil {
114
+ if err := deleteCommitStatusCache (repo .ID , repo .DefaultBranch ); err != nil {
85
115
log .Error ("deleteCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
86
116
}
87
117
}
@@ -98,12 +128,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
98
128
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
99
129
func FindReposLastestCommitStatuses (ctx context.Context , repos []* repo_model.Repository ) ([]* git_model.CommitStatus , error ) {
100
130
results := make ([]* git_model.CommitStatus , len (repos ))
101
- c := cache .GetCache ()
102
-
103
131
for i , repo := range repos {
104
- status , ok := c .Get (getCacheKey (repo .ID , repo .DefaultBranch )).(string )
105
- if ok && status != "" {
106
- results [i ] = & git_model.CommitStatus {State : api .CommitStatusState (status )}
132
+ if cv := getCommitStatusCache (repo .ID , repo .DefaultBranch ); cv != nil {
133
+ results [i ] = & git_model.CommitStatus {
134
+ State : api .CommitStatusState (cv .State ),
135
+ TargetURL : cv .TargetURL ,
136
+ }
107
137
}
108
138
}
109
139
@@ -139,7 +169,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
139
169
return repoSHA .RepoID == repo .ID
140
170
})
141
171
if results [i ].State != "" {
142
- if err := updateCommitStatusCache (ctx , repo .ID , repo .DefaultBranch , results [i ].State ); err != nil {
172
+ if err := updateCommitStatusCache (repo .ID , repo .DefaultBranch , results [i ].State , results [ i ]. TargetURL ); err != nil {
143
173
log .Error ("updateCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
144
174
}
145
175
}
@@ -158,7 +188,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
158
188
if results [i ] == nil {
159
189
results [i ] = git_model .CalcCommitStatus (repoToItsLatestCommitStatuses [repo .ID ])
160
190
if results [i ].State != "" {
161
- if err := updateCommitStatusCache (ctx , repo .ID , repo .DefaultBranch , results [i ].State ); err != nil {
191
+ if err := updateCommitStatusCache (repo .ID , repo .DefaultBranch , results [i ].State , results [ i ]. TargetURL ); err != nil {
162
192
log .Error ("updateCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
163
193
}
164
194
}
0 commit comments