diff options
Diffstat (limited to 'services/pull/commit_status.go')
-rw-r--r-- | services/pull/commit_status.go | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index bfdb3f7291..51ba06da27 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -11,43 +11,53 @@ import ( git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" + "github.com/gobwas/glob" "github.com/pkg/errors" ) // MergeRequiredContextsCommitStatus returns a commit status state for given required contexts func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus, requiredContexts []string) structs.CommitStatusState { - if len(requiredContexts) == 0 { - status := git_model.CalcCommitStatus(commitStatuses) - if status != nil { - return status.State + // matchedCount is the number of `CommitStatus.Context` that match any context of `requiredContexts` + matchedCount := 0 + returnedStatus := structs.CommitStatusSuccess + + if len(requiredContexts) > 0 { + requiredContextsGlob := make(map[string]glob.Glob, len(requiredContexts)) + for _, ctx := range requiredContexts { + if gp, err := glob.Compile(ctx); err != nil { + log.Error("glob.Compile %s failed. Error: %v", ctx, err) + } else { + requiredContextsGlob[ctx] = gp + } } - return structs.CommitStatusSuccess - } - returnedStatus := structs.CommitStatusSuccess - for _, ctx := range requiredContexts { - var targetStatus structs.CommitStatusState for _, commitStatus := range commitStatuses { - if commitStatus.Context == ctx { - targetStatus = commitStatus.State - break + var targetStatus structs.CommitStatusState + for _, gp := range requiredContextsGlob { + if gp.Match(commitStatus.Context) { + targetStatus = commitStatus.State + matchedCount++ + break + } } - } - if targetStatus == "" { - targetStatus = structs.CommitStatusPending - commitStatuses = append(commitStatuses, &git_model.CommitStatus{ - State: targetStatus, - Context: ctx, - Description: "Pending", - }) + if targetStatus != "" && targetStatus.NoBetterThan(returnedStatus) { + returnedStatus = targetStatus + } } - if targetStatus.NoBetterThan(returnedStatus) { - returnedStatus = targetStatus + } + + if matchedCount == 0 { + status := git_model.CalcCommitStatus(commitStatuses) + if status != nil { + return status.State } + return structs.CommitStatusSuccess } + return returnedStatus } |