mirror of
https://github.com/go-gitea/gitea.git
synced 2024-11-30 21:45:41 +08:00
parent
430fe6c0c1
commit
a0ca311165
|
@ -14,6 +14,7 @@ import (
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/cache"
|
"code.gitea.io/gitea/modules/cache"
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
|
"code.gitea.io/gitea/modules/json"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
api "code.gitea.io/gitea/modules/structs"
|
api "code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/services/automerge"
|
"code.gitea.io/gitea/services/automerge"
|
||||||
|
@ -24,15 +25,41 @@ func getCacheKey(repoID int64, brancheName string) string {
|
||||||
return fmt.Sprintf("commit_status:%x", hashBytes)
|
return fmt.Sprintf("commit_status:%x", hashBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateCommitStatusCache(ctx context.Context, repoID int64, branchName string, status api.CommitStatusState) error {
|
type commitStatusCacheValue struct {
|
||||||
c := cache.GetCache()
|
State string `json:"state"`
|
||||||
if c == nil {
|
TargetURL string `json:"target_url"`
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return c.Put(getCacheKey(repoID, branchName), string(status), 3*24*60)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteCommitStatusCache(ctx context.Context, repoID int64, branchName string) error {
|
func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheValue {
|
||||||
|
c := cache.GetCache()
|
||||||
|
statusStr, ok := c.Get(getCacheKey(repoID, branchName)).(string)
|
||||||
|
if ok && statusStr != "" {
|
||||||
|
var cv commitStatusCacheValue
|
||||||
|
err := json.Unmarshal([]byte(statusStr), &cv)
|
||||||
|
if err == nil && cv.State != "" {
|
||||||
|
return &cv
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("getCommitStatusCache: json.Unmarshal failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func updateCommitStatusCache(repoID int64, branchName string, state api.CommitStatusState, targetURL string) error {
|
||||||
|
c := cache.GetCache()
|
||||||
|
bs, err := json.Marshal(commitStatusCacheValue{
|
||||||
|
State: state.String(),
|
||||||
|
TargetURL: targetURL,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("updateCommitStatusCache: json.Marshal failed: %v", err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return c.Put(getCacheKey(repoID, branchName), string(bs), 3*24*60)
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteCommitStatusCache(repoID int64, branchName string) error {
|
||||||
c := cache.GetCache()
|
c := cache.GetCache()
|
||||||
if c == nil {
|
if c == nil {
|
||||||
return nil
|
return nil
|
||||||
|
@ -76,7 +103,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
|
||||||
}
|
}
|
||||||
|
|
||||||
if commit.ID.String() == defaultBranchCommit.ID.String() { // since one commit status updated, the combined commit status should be invalid
|
if commit.ID.String() == defaultBranchCommit.ID.String() { // since one commit status updated, the combined commit status should be invalid
|
||||||
if err := deleteCommitStatusCache(ctx, repo.ID, repo.DefaultBranch); err != nil {
|
if err := deleteCommitStatusCache(repo.ID, repo.DefaultBranch); err != nil {
|
||||||
log.Error("deleteCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
log.Error("deleteCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -93,12 +120,11 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
|
||||||
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
|
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
|
||||||
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
|
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatus, error) {
|
||||||
results := make([]*git_model.CommitStatus, len(repos))
|
results := make([]*git_model.CommitStatus, len(repos))
|
||||||
c := cache.GetCache()
|
for i, repo := range repos {
|
||||||
if c != nil {
|
if cv := getCommitStatusCache(repo.ID, repo.DefaultBranch); cv != nil {
|
||||||
for i, repo := range repos {
|
results[i] = &git_model.CommitStatus{
|
||||||
status, ok := c.Get(getCacheKey(repo.ID, repo.DefaultBranch)).(string)
|
State: api.CommitStatusState(cv.State),
|
||||||
if ok && status != "" {
|
TargetURL: cv.TargetURL,
|
||||||
results[i] = &git_model.CommitStatus{State: api.CommitStatusState(status)}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -127,7 +153,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
|
||||||
if results[i] == nil {
|
if results[i] == nil {
|
||||||
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
|
results[i] = git_model.CalcCommitStatus(repoToItsLatestCommitStatuses[repo.ID])
|
||||||
if results[i].State != "" {
|
if results[i].State != "" {
|
||||||
if err := updateCommitStatusCache(ctx, repo.ID, repo.DefaultBranch, results[i].State); err != nil {
|
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
|
||||||
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
log.Error("updateCommitStatusCache[%d:%s] failed: %v", repo.ID, repo.DefaultBranch, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user