summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorZettat123 <zettat123@gmail.com>2023-05-17 16:11:13 +0800
committerGitHub <noreply@github.com>2023-05-17 16:11:13 +0800
commite7c2231dee356df5cbe5a47c07e31e3a8d090a6f (patch)
treeef63813e9fb42d5a4eec22516f4320bc8c947107 /services
parent9fb0945a0959f1c0f0c9e75980e8a0cc5355184c (diff)
downloadgitea-e7c2231dee356df5cbe5a47c07e31e3a8d090a6f.tar.gz
gitea-e7c2231dee356df5cbe5a47c07e31e3a8d090a6f.zip
Support for status check pattern (#24633)
This PR is to allow users to specify status checks by patterns. Users can enter patterns in the "Status Check Pattern" `textarea` to match status checks and each line specifies a pattern. If "Status Check" is enabled, patterns cannot be empty and user must enter at least one pattern. Users will no longer be able to choose status checks from the table. But a __*`Matched`*__ mark will be added to the matched checks to help users enter patterns. Benefits: - Even if no status checks have been completed, users can specify necessary status checks in advance. - More flexible. Users can specify a series of status checks by one pattern. Before: ![image](https://github.com/go-gitea/gitea/assets/15528715/635738ad-580c-49cd-941d-c721e5b99be4) After: ![image](https://github.com/go-gitea/gitea/assets/15528715/16aa7b1b-abf1-4170-9bfa-ae6fc9803a82) --------- Co-authored-by: silverwind <me@silverwind.io>
Diffstat (limited to 'services')
-rw-r--r--services/forms/repo_form.go2
-rw-r--r--services/pull/commit_status.go54
2 files changed, 33 insertions, 23 deletions
diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go
index 41d7dc7d2b..d705ecad3f 100644
--- a/services/forms/repo_form.go
+++ b/services/forms/repo_form.go
@@ -199,7 +199,7 @@ type ProtectBranchForm struct {
MergeWhitelistUsers string
MergeWhitelistTeams string
EnableStatusCheck bool
- StatusCheckContexts []string
+ StatusCheckContexts string
RequiredApprovals int64
EnableApprovalsWhitelist bool
ApprovalsWhitelistUsers string
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
}