@@ -14,6 +14,7 @@ import (
1414 user_model "code.gitea.io/gitea/models/user"
1515 "code.gitea.io/gitea/modules/cache"
1616 "code.gitea.io/gitea/modules/git"
17+ "code.gitea.io/gitea/modules/json"
1718 "code.gitea.io/gitea/modules/log"
1819 api "code.gitea.io/gitea/modules/structs"
1920 "code.gitea.io/gitea/services/automerge"
@@ -24,15 +25,41 @@ func getCacheKey(repoID int64, brancheName string) string {
2425 return fmt .Sprintf ("commit_status:%x" , hashBytes )
2526}
2627
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 {
2834 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 )
3057 return nil
3158 }
32- return c .Put (getCacheKey (repoID , branchName ), string (status ), 3 * 24 * 60 )
59+ return c .Put (getCacheKey (repoID , branchName ), string (bs ), 3 * 24 * 60 )
3360}
3461
35- func deleteCommitStatusCache (ctx context. Context , repoID int64 , branchName string ) error {
62+ func deleteCommitStatusCache (repoID int64 , branchName string ) error {
3663 c := cache .GetCache ()
3764 if c == nil {
3865 return nil
@@ -76,7 +103,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
76103 }
77104
78105 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 {
80107 log .Error ("deleteCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
81108 }
82109 }
@@ -93,12 +120,11 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
93120// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
94121func FindReposLastestCommitStatuses (ctx context.Context , repos []* repo_model.Repository ) ([]* git_model.CommitStatus , error ) {
95122 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 ,
102128 }
103129 }
104130 }
@@ -127,7 +153,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
127153 if results [i ] == nil {
128154 results [i ] = git_model .CalcCommitStatus (repoToItsLatestCommitStatuses [repo .ID ])
129155 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 {
131157 log .Error ("updateCommitStatusCache[%d:%s] failed: %v" , repo .ID , repo .DefaultBranch , err )
132158 }
133159 }
0 commit comments