@@ -15,6 +15,7 @@ import (
15
15
"code.gitea.io/gitea/modules/cache"
16
16
"code.gitea.io/gitea/modules/git"
17
17
"code.gitea.io/gitea/modules/gitrepo"
18
+ "code.gitea.io/gitea/modules/json"
18
19
"code.gitea.io/gitea/modules/log"
19
20
api "code.gitea.io/gitea/modules/structs"
20
21
"code.gitea.io/gitea/services/automerge"
@@ -25,12 +26,41 @@ func getCacheKey(repoID int64, brancheName string) string {
25
26
return fmt .Sprintf ("commit_status:%x" , hashBytes )
26
27
}
27
28
28
- func updateCommitStatusCache (ctx context.Context , repoID int64 , branchName string , status api.CommitStatusState ) error {
29
+ type commitStatusCacheValue struct {
30
+ State string `json:"state"`
31
+ TargetURL string `json:"target_url"`
32
+ }
33
+
34
+ func getCommitStatusCache (repoID int64 , branchName string ) * commitStatusCacheValue {
35
+ c := cache .GetCache ()
36
+ statusStr , ok := c .Get (getCacheKey (repoID , branchName )).(string )
37
+ if ok && statusStr != "" {
38
+ var cv commitStatusCacheValue
39
+ err := json .Unmarshal ([]byte (statusStr ), & cv )
40
+ if err == nil && cv .State != "" {
41
+ return & cv
42
+ }
43
+ if err != nil {
44
+ log .Warn ("getCommitStatusCache: json.Unmarshal failed: %v" , err )
45
+ }
46
+ }
47
+ return nil
48
+ }
49
+
50
+ func updateCommitStatusCache (repoID int64 , branchName string , state api.CommitStatusState , targetURL string ) error {
29
51
c := cache .GetCache ()
30
- return c .Put (getCacheKey (repoID , branchName ), string (status ), 3 * 24 * 60 )
52
+ bs , err := json .Marshal (commitStatusCacheValue {
53
+ State : state .String (),
54
+ TargetURL : targetURL ,
55
+ })
56
+ if err != nil {
57
+ log .Warn ("updateCommitStatusCache: json.Marshal failed: %v" , err )
58
+ return nil
59
+ }
60
+ return c .Put (getCacheKey (repoID , branchName ), string (bs ), 3 * 24 * 60 )
31
61
}
32
62
33
- func deleteCommitStatusCache (ctx context. Context , repoID int64 , branchName string ) error {
63
+ func deleteCommitStatusCache (repoID int64 , branchName string ) error {
34
64
c := cache .GetCache ()
35
65
return c .Delete (getCacheKey (repoID , branchName ))
36
66
}
@@ -74,7 +104,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
74
104
}
75
105
76
106
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 {
107
+ if err := deleteCommitStatusCache (repo .ID , repo .DefaultBranch ); err != nil {
78
108
log .Error ("deleteCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
79
109
}
80
110
}
@@ -91,12 +121,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
91
121
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
92
122
func FindReposLastestCommitStatuses (ctx context.Context , repos []* repo_model.Repository ) ([]* git_model.CommitStatus , error ) {
93
123
results := make ([]* git_model.CommitStatus , len (repos ))
94
- c := cache .GetCache ()
95
-
96
124
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 )}
125
+ if cv := getCommitStatusCache (repo .ID , repo .DefaultBranch ); cv != nil {
126
+ results [i ] = & git_model.CommitStatus {
127
+ State : api .CommitStatusState (cv .State ),
128
+ TargetURL : cv .TargetURL ,
129
+ }
100
130
}
101
131
}
102
132
@@ -124,7 +154,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
124
154
if results [i ] == nil {
125
155
results [i ] = git_model .CalcCommitStatus (repoToItsLatestCommitStatuses [repo .ID ])
126
156
if results [i ].State != "" {
127
- if err := updateCommitStatusCache (ctx , repo .ID , repo .DefaultBranch , results [i ].State ); err != nil {
157
+ if err := updateCommitStatusCache (repo .ID , repo .DefaultBranch , results [i ].State , results [ i ]. TargetURL ); err != nil {
128
158
log .Error ("updateCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
129
159
}
130
160
}
0 commit comments