-
Notifications
You must be signed in to change notification settings - Fork 123
feat(github): add configurable GitOps command prefix #2443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,23 +5,11 @@ import ( | |
| "regexp" | ||
| "strings" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/acl" | ||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1" | ||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/events" | ||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" | ||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/params/triggertype" | ||
| ) | ||
|
|
||
| var ( | ||
| testAllRegex = regexp.MustCompile(`(?m)^/test\s*$`) | ||
| retestAllRegex = regexp.MustCompile(`(?m)^/retest\s*$`) | ||
| testSingleRegex = regexp.MustCompile(`(?m)^/test[ \t]+\S+`) | ||
| retestSingleRegex = regexp.MustCompile(`(?m)^/retest[ \t]+\S+`) | ||
| oktotestRegex = regexp.MustCompile(acl.OKToTestCommentRegexp) | ||
| cancelAllRegex = regexp.MustCompile(`(?m)^(/cancel)\s*$`) | ||
| cancelSingleRegex = regexp.MustCompile(`(?m)^(/cancel)[ \t]+\S+`) | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type EventType string | ||
|
|
@@ -42,56 +30,93 @@ var ( | |
| OkToTestCommentEventType = EventType("ok-to-test-comment") | ||
| ) | ||
|
|
||
| const ( | ||
| testComment = "/test" | ||
| retestComment = "/retest" | ||
| cancelComment = "/cancel" | ||
| ) | ||
| func RetestAllRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%sretest\s*$`, prefix)) | ||
| } | ||
|
|
||
| func RetestSingleRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%sretest[ \t]+\S+`, prefix)) | ||
| } | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func TestAllRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%stest\s*$`, prefix)) | ||
| } | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func TestSingleRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%stest[ \t]+\S+`, prefix)) | ||
| } | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func OkToTestRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(^|\n)\s*%sok-to-test(?:\s+([a-fA-F0-9]{7,40}))?\s*(\r\n|\r|\n|$)`, prefix)) | ||
| } | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func CancelAllRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%scancel\s*$`, prefix)) | ||
| } | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func CancelSingleRegex(prefix string) *regexp.Regexp { | ||
| return regexp.MustCompile(fmt.Sprintf(`(?m)^\s*%scancel[ \t]+\S+`, prefix)) | ||
| } | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+33
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions use
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func CommentEventType(comment string) EventType { | ||
| func CommentEventType(comment, prefix string) EventType { | ||
| switch { | ||
| case retestAllRegex.MatchString(comment): | ||
| case RetestAllRegex(prefix).MatchString(comment): | ||
| return RetestAllCommentEventType | ||
| case retestSingleRegex.MatchString(comment): | ||
| case RetestSingleRegex(prefix).MatchString(comment): | ||
| return RetestSingleCommentEventType | ||
| case testAllRegex.MatchString(comment): | ||
| case TestAllRegex(prefix).MatchString(comment): | ||
| return TestAllCommentEventType | ||
| case testSingleRegex.MatchString(comment): | ||
| case TestSingleRegex(prefix).MatchString(comment): | ||
| return TestSingleCommentEventType | ||
| case oktotestRegex.MatchString(comment): | ||
| case OkToTestRegex(prefix).MatchString(comment): | ||
| return OkToTestCommentEventType | ||
| case cancelAllRegex.MatchString(comment): | ||
| case CancelAllRegex(prefix).MatchString(comment): | ||
| return CancelCommentAllEventType | ||
| case cancelSingleRegex.MatchString(comment): | ||
| case CancelSingleRegex(prefix).MatchString(comment): | ||
| return CancelCommentSingleEventType | ||
| default: | ||
| return NoOpsCommentEventType | ||
| } | ||
| } | ||
|
|
||
| // SetEventTypeAndTargetPR function will set the event type and target test pipeline run in an event. | ||
| func SetEventTypeAndTargetPR(event *info.Event, comment string) { | ||
| commentType := CommentEventType(comment) | ||
| if commentType == RetestSingleCommentEventType || commentType == TestSingleCommentEventType { | ||
| event.TargetTestPipelineRun = GetPipelineRunFromTestComment(comment) | ||
| func SetEventTypeAndTargetPR(event *info.Event, comment, prefix string) { | ||
| commentType := CommentEventType(comment, prefix) | ||
| if commentType == RetestSingleCommentEventType { | ||
| typeOfComment := prefix + "retest" | ||
| event.TargetTestPipelineRun = getNameFromComment(typeOfComment, comment) | ||
| } | ||
| if commentType == TestSingleCommentEventType { | ||
| typeOfComment := prefix + "test" | ||
| event.TargetTestPipelineRun = getNameFromComment(typeOfComment, comment) | ||
| } | ||
| if commentType == CancelCommentAllEventType || commentType == CancelCommentSingleEventType { | ||
| event.CancelPipelineRuns = true | ||
| } | ||
| if commentType == CancelCommentSingleEventType { | ||
| event.TargetCancelPipelineRun = GetPipelineRunFromCancelComment(comment) | ||
| typeOfComment := prefix + "cancel" | ||
| event.TargetCancelPipelineRun = getNameFromComment(typeOfComment, comment) | ||
| } | ||
| event.EventType = commentType.String() | ||
| event.TriggerComment = comment | ||
| } | ||
|
|
||
| func IsOkToTestComment(comment string) bool { | ||
| return oktotestRegex.MatchString(comment) | ||
| func IsOkToTestComment(comment, prefix string) bool { | ||
| return OkToTestRegex(prefix).MatchString(comment) | ||
| } | ||
|
|
||
| func IsTestRetestComment(comment, prefix string) bool { | ||
| return TestSingleRegex(prefix).MatchString(comment) || TestAllRegex(prefix).MatchString(comment) || | ||
| RetestSingleRegex(prefix).MatchString(comment) || RetestAllRegex(prefix).MatchString(comment) | ||
| } | ||
|
|
||
| func IsCancelComment(comment, prefix string) bool { | ||
| return CancelAllRegex(prefix).MatchString(comment) || CancelSingleRegex(prefix).MatchString(comment) | ||
| } | ||
|
|
||
| // GetSHAFromOkToTestComment extracts the optional SHA from an /ok-to-test comment. | ||
| func GetSHAFromOkToTestComment(comment string) string { | ||
| matches := oktotestRegex.FindStringSubmatch(comment) | ||
| func GetSHAFromOkToTestComment(comment, prefix string) string { | ||
| matches := OkToTestRegex(prefix).FindStringSubmatch(comment) | ||
| if len(matches) > 2 { | ||
| return strings.TrimSpace(matches[2]) | ||
| } | ||
|
|
@@ -143,17 +168,6 @@ func AnyOpsKubeLabelInSelector() string { | |
| OnCommentEventType.String()) | ||
| } | ||
|
|
||
| func GetPipelineRunFromTestComment(comment string) string { | ||
| if strings.Contains(comment, testComment) { | ||
| return getNameFromComment(testComment, comment) | ||
| } | ||
| return getNameFromComment(retestComment, comment) | ||
| } | ||
|
|
||
| func GetPipelineRunFromCancelComment(comment string) string { | ||
| return getNameFromComment(cancelComment, comment) | ||
| } | ||
|
|
||
| func getNameFromComment(typeOfComment, comment string) string { | ||
| splitTest := strings.Split(strings.TrimSpace(comment), typeOfComment) | ||
| if len(splitTest) < 2 { | ||
|
|
@@ -171,48 +185,3 @@ func getNameFromComment(typeOfComment, comment string) string { | |
| // trim spaces | ||
| return strings.TrimSpace(firstArg[1]) | ||
| } | ||
|
|
||
| func GetPipelineRunAndBranchNameFromTestComment(comment string) (string, string, error) { | ||
| if strings.Contains(comment, testComment) { | ||
| return getPipelineRunAndBranchNameFromComment(testComment, comment) | ||
| } | ||
| return getPipelineRunAndBranchNameFromComment(retestComment, comment) | ||
| } | ||
|
|
||
| func GetPipelineRunAndBranchNameFromCancelComment(comment string) (string, string, error) { | ||
| return getPipelineRunAndBranchNameFromComment(cancelComment, comment) | ||
| } | ||
|
|
||
| // getPipelineRunAndBranchNameFromComment function will take GitOps comment and split the comment | ||
| // by /test, /retest or /cancel to return branch name and pipelinerun name. | ||
| func getPipelineRunAndBranchNameFromComment(typeOfComment, comment string) (string, string, error) { | ||
| var prName, branchName string | ||
| splitTest := strings.Split(comment, typeOfComment) | ||
|
|
||
| // after the split get the second part of the typeOfComment (/test, /retest or /cancel) | ||
| // as second part can be branch name or pipelinerun name and branch name | ||
| // ex: /test branch:nightly, /test prname branch:nightly | ||
| if splitTest[1] != "" && strings.Contains(splitTest[1], ":") { | ||
| branchData := strings.Split(splitTest[1], ":") | ||
|
|
||
| // make sure no other word is supported other than branch word | ||
| if !strings.Contains(branchData[0], "branch") { | ||
| return prName, branchName, fmt.Errorf("the GitOps comment%s does not contain a branch word", branchData[0]) | ||
| } | ||
| branchName = strings.Split(strings.TrimSpace(branchData[1]), " ")[0] | ||
|
|
||
| // if data after the split contains prname then fetch that | ||
| prData := strings.Split(strings.TrimSpace(branchData[0]), " ") | ||
| if len(prData) > 1 { | ||
| prName = strings.TrimSpace(prData[0]) | ||
| } | ||
| } else { | ||
| // get the second part of the typeOfComment (/test, /retest or /cancel) | ||
| // as second part contains pipelinerun name | ||
| // ex: /test prname | ||
| getFirstLine := strings.Split(splitTest[1], "\n") | ||
| // trim spaces | ||
| prName = strings.TrimSpace(getFirstLine[0]) | ||
| } | ||
| return prName, branchName, nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.