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