Skip to content

Commit f9c3e79

Browse files
lunnywolfogre
andauthored
Fix commit status cache which missed target_url (#30426)
Fix #30421 --------- Co-authored-by: Jason Song <[email protected]>
1 parent 25427e0 commit f9c3e79

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

services/repository/commitstatus/commitstatus.go

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/cache"
1717
"code.gitea.io/gitea/modules/git"
1818
"code.gitea.io/gitea/modules/gitrepo"
19+
"code.gitea.io/gitea/modules/json"
1920
"code.gitea.io/gitea/modules/log"
2021
api "code.gitea.io/gitea/modules/structs"
2122
"code.gitea.io/gitea/services/automerge"
@@ -26,12 +27,41 @@ func getCacheKey(repoID int64, brancheName string) string {
2627
return fmt.Sprintf("commit_status:%x", hashBytes)
2728
}
2829

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 {
3052
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)
3262
}
3363

34-
func deleteCommitStatusCache(ctx context.Context, repoID int64, branchName string) error {
64+
func deleteCommitStatusCache(repoID int64, branchName string) error {
3565
c := cache.GetCache()
3666
return c.Delete(getCacheKey(repoID, branchName))
3767
}
@@ -81,7 +111,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
81111
}
82112

83113
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 {
85115
log.Error("deleteCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
86116
}
87117
}
@@ -98,12 +128,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
98128
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
99129
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
100130
results := make([]*git_model.CommitStatus, len(repos))
101-
c := cache.GetCache()
102-
103131
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+
}
107137
}
108138
}
109139

@@ -139,7 +169,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
139169
return repoSHA.RepoID == repo.ID
140170
})
141171
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 {
143173
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
144174
}
145175
}
@@ -158,7 +188,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
158188
if results[i] == nil {
159189
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
160190
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 {
162192
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
163193
}
164194
}

0 commit comments

Comments
 (0)