Skip to content

Commit 7ffc0ac

Browse files
lunnywolfogre
andauthored
Fix commit status cache which missed target_url (#30426) (#30444)
Fix #30421 Backport #30426 Co-authored-by: Jason Song <[email protected]>
1 parent 2efc81d commit 7ffc0ac

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

services/repository/commitstatus/commitstatus.go

+40-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/cache"
1616
"code.gitea.io/gitea/modules/git"
1717
"code.gitea.io/gitea/modules/gitrepo"
18+
"code.gitea.io/gitea/modules/json"
1819
"code.gitea.io/gitea/modules/log"
1920
api "code.gitea.io/gitea/modules/structs"
2021
"code.gitea.io/gitea/services/automerge"
@@ -25,12 +26,41 @@ func getCacheKey(repoID int64, brancheName string) string {
2526
return fmt.Sprintf("commit_status:%x", hashBytes)
2627
}
2728

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 {
2951
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)
3161
}
3262

33-
func deleteCommitStatusCache(ctx context.Context, repoID int64, branchName string) error {
63+
func deleteCommitStatusCache(repoID int64, branchName string) error {
3464
c := cache.GetCache()
3565
return c.Delete(getCacheKey(repoID, branchName))
3666
}
@@ -74,7 +104,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
74104
}
75105

76106
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 {
78108
log.Error("deleteCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
79109
}
80110
}
@@ -91,12 +121,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
91121
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
92122
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
93123
results := make([]*git_model.CommitStatus, len(repos))
94-
c := cache.GetCache()
95-
96124
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+
}
100130
}
101131
}
102132

@@ -124,7 +154,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
124154
if results[i] == nil {
125155
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
126156
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 {
128158
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
129159
}
130160
}

0 commit comments

Comments
 (0)