mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-21 00:24:57 +02:00
improvements
This commit is contained in:
parent
811e900d30
commit
0915b18157
@ -215,11 +215,9 @@ func (status *CommitStatus) HideActionsURL(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if status.Repo == nil {
|
if err := status.loadRepository(ctx); err != nil {
|
||||||
if err := status.loadRepository(ctx); err != nil {
|
log.Error("loadRepository: %v", err)
|
||||||
log.Error("loadRepository: %v", err)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := status.Repo.Link() + "/actions"
|
prefix := status.Repo.Link() + "/actions"
|
||||||
|
|||||||
@ -32,7 +32,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (status *CommitStatusSummary) loadRepository(ctx context.Context) error {
|
func (status *CommitStatusSummary) loadRepository(ctx context.Context) error {
|
||||||
if status.RepoID == 0 {
|
if status.RepoID == 0 || status.Repo != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,11 +56,9 @@ func (status *CommitStatusSummary) HideActionsURL(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if status.Repo == nil {
|
if err := status.loadRepository(ctx); err != nil {
|
||||||
if err := status.loadRepository(ctx); err != nil {
|
log.Error("loadRepository: %v", err)
|
||||||
log.Error("loadRepository: %v", err)
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := status.Repo.Link() + "/actions"
|
prefix := status.Repo.Link() + "/actions"
|
||||||
@ -74,7 +72,7 @@ type RepoSHA struct {
|
|||||||
SHA string
|
SHA string
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLatestCommitStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA) ([]*CommitStatusSummary, error) {
|
func GetLatestCombinedStatusForRepoAndSHAs(ctx context.Context, repoSHAs []RepoSHA) ([]*CommitStatusSummary, error) {
|
||||||
cond := builder.NewCond()
|
cond := builder.NewCond()
|
||||||
for _, rs := range repoSHAs {
|
for _, rs := range repoSHAs {
|
||||||
cond = cond.Or(builder.Eq{"repo_id": rs.RepoID, "sha": rs.SHA})
|
cond = cond.Or(builder.Eq{"repo_id": rs.RepoID, "sha": rs.SHA})
|
||||||
|
|||||||
@ -553,7 +553,7 @@ func SearchRepo(ctx *context.Context) {
|
|||||||
|
|
||||||
ctx.SetTotalCountHeader(count)
|
ctx.SetTotalCountHeader(count)
|
||||||
|
|
||||||
latestCommitStatuses, err := commitstatus_service.FindReposLastestCommitStatuses(ctx, repos)
|
latestCommitStatuses, err := commitstatus_service.FindReposLastestCombinedStatuses(ctx, repos)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("FindReposLastestCommitStatuses: %v", err)
|
log.Error("FindReposLastestCommitStatuses: %v", err)
|
||||||
ctx.JSON(http.StatusInternalServerError, nil)
|
ctx.JSON(http.StatusInternalServerError, nil)
|
||||||
|
|||||||
@ -28,16 +28,16 @@ func getCacheKey(repoID int64, brancheName string) string {
|
|||||||
return fmt.Sprintf("commit_status:%x", hashBytes)
|
return fmt.Sprintf("commit_status:%x", hashBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
type commitStatusCacheValue struct {
|
type combinedStatusCacheValue struct {
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
TargetURL string `json:"target_url"`
|
TargetURL string `json:"target_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheValue {
|
func getCombinedStatusCache(repoID int64, branchName string) *combinedStatusCacheValue {
|
||||||
c := cache.GetCache()
|
c := cache.GetCache()
|
||||||
statusStr, ok := c.Get(getCacheKey(repoID, branchName))
|
statusStr, ok := c.Get(getCacheKey(repoID, branchName))
|
||||||
if ok && statusStr != "" {
|
if ok && statusStr != "" {
|
||||||
var cv commitStatusCacheValue
|
var cv combinedStatusCacheValue
|
||||||
err := json.Unmarshal([]byte(statusStr), &cv)
|
err := json.Unmarshal([]byte(statusStr), &cv)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return &cv
|
return &cv
|
||||||
@ -47,9 +47,9 @@ func getCommitStatusCache(repoID int64, branchName string) *commitStatusCacheVal
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateCommitStatusCache(repoID int64, branchName string, state commitstatus.CombinedStatus, targetURL string) error {
|
func updateCombinedStatusCache(repoID int64, branchName string, state commitstatus.CombinedStatus, targetURL string) error {
|
||||||
c := cache.GetCache()
|
c := cache.GetCache()
|
||||||
bs, err := json.Marshal(commitStatusCacheValue{
|
bs, err := json.Marshal(combinedStatusCacheValue{
|
||||||
State: string(state),
|
State: string(state),
|
||||||
TargetURL: targetURL,
|
TargetURL: targetURL,
|
||||||
})
|
})
|
||||||
@ -60,7 +60,7 @@ func updateCommitStatusCache(repoID int64, branchName string, state commitstatus
|
|||||||
return c.Put(getCacheKey(repoID, branchName), string(bs), 3*24*60)
|
return c.Put(getCacheKey(repoID, branchName), string(bs), 3*24*60)
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteCommitStatusCache(repoID int64, branchName string) error {
|
func deleteCombinedStatusCache(repoID int64, branchName string) error {
|
||||||
c := cache.GetCache()
|
c := cache.GetCache()
|
||||||
return c.Delete(getCacheKey(repoID, branchName))
|
return c.Delete(getCacheKey(repoID, branchName))
|
||||||
}
|
}
|
||||||
@ -112,7 +112,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(repo.ID, repo.DefaultBranch); err != nil {
|
if err := deleteCombinedStatusCache(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -120,12 +120,12 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindReposLastestCommitStatuses loading repository default branch latest combinded commit status with cache
|
// FindReposLastestCombinedStatuses loading repository default branch latest combinded commit status with cache
|
||||||
func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatusSummary, error) {
|
func FindReposLastestCombinedStatuses(ctx context.Context, repos []*repo_model.Repository) ([]*git_model.CommitStatusSummary, error) {
|
||||||
results := make([]*git_model.CommitStatusSummary, len(repos))
|
results := make([]*git_model.CommitStatusSummary, len(repos))
|
||||||
allCached := true
|
allCached := true
|
||||||
for i, repo := range repos {
|
for i, repo := range repos {
|
||||||
if cv := getCommitStatusCache(repo.ID, repo.DefaultBranch); cv != nil {
|
if cv := getCombinedStatusCache(repo.ID, repo.DefaultBranch); cv != nil {
|
||||||
results[i] = &git_model.CommitStatusSummary{
|
results[i] = &git_model.CommitStatusSummary{
|
||||||
State: commitstatus.CombinedStatus(cv.State),
|
State: commitstatus.CombinedStatus(cv.State),
|
||||||
TargetURL: cv.TargetURL,
|
TargetURL: cv.TargetURL,
|
||||||
@ -158,7 +158,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
|
|||||||
repoSHAs = append(repoSHAs, git_model.RepoSHA{RepoID: id, SHA: sha})
|
repoSHAs = append(repoSHAs, git_model.RepoSHA{RepoID: id, SHA: sha})
|
||||||
}
|
}
|
||||||
|
|
||||||
summaryResults, err := git_model.GetLatestCommitStatusForRepoAndSHAs(ctx, repoSHAs)
|
summaryResults, err := git_model.GetLatestCombinedStatusForRepoAndSHAs(ctx, repoSHAs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("GetLatestCommitStatusForRepoAndSHAs: %v", err)
|
return nil, fmt.Errorf("GetLatestCommitStatusForRepoAndSHAs: %v", err)
|
||||||
}
|
}
|
||||||
@ -170,7 +170,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
|
|||||||
repoSHAs = slices.DeleteFunc(repoSHAs, func(repoSHA git_model.RepoSHA) bool {
|
repoSHAs = slices.DeleteFunc(repoSHAs, func(repoSHA git_model.RepoSHA) bool {
|
||||||
return repoSHA.RepoID == repo.ID
|
return repoSHA.RepoID == repo.ID
|
||||||
})
|
})
|
||||||
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
|
if err := updateCombinedStatusCache(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)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
@ -191,7 +191,7 @@ func FindReposLastestCommitStatuses(ctx context.Context, repos []*repo_model.Rep
|
|||||||
if results[i] == nil {
|
if results[i] == nil {
|
||||||
results[i] = git_model.CalcCommitStatusSummary(repoToItsLatestCommitStatuses[repo.ID])
|
results[i] = git_model.CalcCommitStatusSummary(repoToItsLatestCommitStatuses[repo.ID])
|
||||||
if results[i] != nil {
|
if results[i] != nil {
|
||||||
if err := updateCommitStatusCache(repo.ID, repo.DefaultBranch, results[i].State, results[i].TargetURL); err != nil {
|
if err := updateCombinedStatusCache(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…
x
Reference in New Issue
Block a user