forked from openshift-pipelines/pipelines-as-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
188 lines (163 loc) · 5.92 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package provider
import (
"fmt"
"net/url"
"regexp"
"strings"
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"gopkg.in/yaml.v2"
)
var (
testRetestAllRegex = regexp.MustCompile(`(?m)^(/retest|/test)\s*$`)
testRetestSingleRegex = regexp.MustCompile(`(?m)^(/test|/retest)[ \t]+\S+`)
oktotestRegex = regexp.MustCompile(`(?m)^/ok-to-test\s*$`)
cancelAllRegex = regexp.MustCompile(`(?m)^(/cancel)\s*$`)
cancelSingleRegex = regexp.MustCompile(`(?m)^(/cancel)[ \t]+\S+`)
)
const (
testComment = "/test"
retestComment = "/retest"
cancelComment = "/cancel"
)
const (
GitHubApp = "GitHubApp"
)
type CommentType int
const (
StartingPipelineType CommentType = iota
PipelineRunStatusType
QueueingPipelineType
)
func GetHTMLTemplate(commentType CommentType) string {
switch commentType {
case StartingPipelineType:
return formatting.StartingPipelineRunHTML
case PipelineRunStatusType:
return formatting.PipelineRunStatusHTML
case QueueingPipelineType:
return formatting.QueuingPipelineRunHTML
}
return ""
}
func GetMarkdownTemplate(commentType CommentType) string {
switch commentType {
case StartingPipelineType:
return formatting.StartingPipelineRunMarkdown
case PipelineRunStatusType:
return formatting.PipelineRunStatusMarkDown
case QueueingPipelineType:
return formatting.QueuingPipelineRunMarkdown
}
return ""
}
func Valid(value string, validValues []string) bool {
for _, v := range validValues {
if v == value {
return true
}
}
return false
}
func IsTestRetestComment(comment string) bool {
return testRetestSingleRegex.MatchString(comment) || testRetestAllRegex.MatchString(comment)
}
func IsOkToTestComment(comment string) bool {
return oktotestRegex.MatchString(comment)
}
func IsCancelComment(comment string) bool {
return cancelAllRegex.MatchString(comment) || cancelSingleRegex.MatchString(comment)
}
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(comment, typeOfComment)
// now get the first line
getFirstLine := strings.Split(splitTest[1], "\n")
// trim spaces
return strings.TrimSpace(getFirstLine[0])
}
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
// avoid parsing error due to branch name contains /test, /retest or /cancel,
// here only split the first keyword and not split the later keywords.
splitTest := strings.SplitN(comment, typeOfComment, 2)
// 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, /test prname branch:nightly key=value
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
// adapt for the comment contains the key=value pair
prName = strings.Split(strings.TrimSpace(getFirstLine[0]), " ")[0]
}
return prName, branchName, nil
}
// CompareHostOfURLS compares the host of two parsed URLs and returns true if
// they are.
func CompareHostOfURLS(uri1, uri2 string) bool {
u1, err := url.Parse(uri1)
if err != nil || u1.Host == "" {
return false
}
u2, err := url.Parse(uri2)
if err != nil || u2.Host == "" {
return false
}
return u1.Host == u2.Host
}
func ValidateYaml(content []byte, filename string) error {
var validYaml any
if err := yaml.Unmarshal(content, &validYaml); err != nil {
return fmt.Errorf("error unmarshalling yaml file %s: %w", filename, err)
}
return nil
}
// GetCheckName returns the name of the check to be created based on the status
// and the pacopts.
// If the pacopts.ApplicationName is set, it will be used as the check name.
// Otherwise, the OriginalPipelineRunName will be used.
// If the OriginalPipelineRunName is not set, an empty string will be returned.
// The check name will be in the format "ApplicationName / OriginalPipelineRunName".
func GetCheckName(status StatusOpts, pacopts *info.PacOpts) string {
if pacopts.ApplicationName != "" {
if status.OriginalPipelineRunName == "" {
return pacopts.ApplicationName
}
return fmt.Sprintf("%s / %s", pacopts.ApplicationName, status.OriginalPipelineRunName)
}
return status.OriginalPipelineRunName
}