Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit 6276eaf

Browse files
committed
Add test case for exceptBranches and fix naming
1 parent 8071f56 commit 6276eaf

File tree

8 files changed

+177
-15
lines changed

8 files changed

+177
-15
lines changed

internal/manager/receive.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"fmt"
77
"io"
88
"net/http"
9-
"path/filepath"
9+
"path"
1010
"regexp"
1111
"strings"
1212

@@ -246,7 +246,8 @@ func pipelineMatches(pInfo PipelineInfo, pipeline config.Pipeline) bool {
246246
if pipeline.Trigger == nil {
247247
return true
248248
}
249-
return pipelineEventsMatch(pInfo, pipeline) && pipelineBranchesMatch(pInfo, pipeline) && pipelineCommentMatches(pInfo, pipeline)
249+
return pipelineEventsMatch(pInfo, pipeline) && pipelineBranchesMatch(pInfo, pipeline) &&
250+
pipelineExcludedBranchesDoNotMatch(pInfo, pipeline) && pipelineCommentMatches(pInfo, pipeline)
250251
}
251252

252253
func anyPatternMatches(s string, patterns []string) bool {
@@ -255,7 +256,7 @@ func anyPatternMatches(s string, patterns []string) bool {
255256
}
256257

257258
for _, pattern := range patterns {
258-
if matched, err := filepath.Match(pattern, s); matched && err == nil {
259+
if matched, err := path.Match(pattern, s); matched && err == nil {
259260
return true
260261
}
261262
}
@@ -267,7 +268,14 @@ func pipelineEventsMatch(pInfo PipelineInfo, pipeline config.Pipeline) bool {
267268
}
268269

269270
func pipelineBranchesMatch(pInfo PipelineInfo, pipeline config.Pipeline) bool {
270-
return anyPatternMatches(pInfo.GitRef, pipeline.Trigger.Branches) && !anyPatternMatches(pInfo.GitRef, pipeline.Trigger.ExceptBranches)
271+
return anyPatternMatches(pInfo.GitRef, pipeline.Trigger.Branches)
272+
}
273+
274+
func pipelineExcludedBranchesDoNotMatch(pInfo PipelineInfo, pipeline config.Pipeline) bool {
275+
if len(pipeline.Trigger.ExceptBranches) == 0 {
276+
return true
277+
}
278+
return !anyPatternMatches(pInfo.GitRef, pipeline.Trigger.ExceptBranches)
271279
}
272280

273281
func pipelineCommentMatches(pInfo PipelineInfo, pipeline config.Pipeline) bool {

internal/manager/receive_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,27 @@ func TestWebhookHandling(t *testing.T) {
217217
wantPipelineConfig: true,
218218
wantPipelineTaskNames: []string{"go-helm-build-comment-added-select-foo"},
219219
},
220+
"pr:comment:added request on excluded branch triggers branch-specific pipeline": {
221+
requestBodyFixture: "manager/payload-pr-comment-added-select-foo.json",
222+
bitbucketClient: &bitbucket.TestClient{
223+
Files: map[string][]byte{
224+
"ods.yaml": readTestdataFile(t, "fixtures/manager/multi-pipeline-ods.yaml"),
225+
},
226+
PullRequests: []bitbucket.PullRequest{
227+
{
228+
Open: true,
229+
ID: 1,
230+
ToRef: bitbucket.Ref{
231+
ID: "refs/heads/master",
232+
},
233+
},
234+
},
235+
},
236+
wantBody: string(readTestdataFile(t, "golden/manager/response-payload-pr-comment-added-select-foo.json")),
237+
wantStatus: http.StatusOK,
238+
wantPipelineConfig: true,
239+
wantPipelineTaskNames: []string{"go-helm-build-opened-pr-foo"},
240+
},
220241
"pr:comment:added request triggers catch-all pipeline": {
221242
requestBodyFixture: "manager/payload-pr-comment-added-other.json",
222243
bitbucketClient: &bitbucket.TestClient{

pkg/config/ods.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ const (
3030

3131
var ODSFileCandidates = []string{ODSYAMLFile, ODSYMLFile}
3232

33-
// legacyODS represents the legacy ODS pipeline configuration for one repository.
33+
// simplifiedODS represents the legacy ODS pipeline configuration for one repository.
3434
// This is used to still support repositories that haven't migrated new format yet.
35-
type legacyODS struct {
35+
type simplifiedODS struct {
3636
// Repositories specifies the subrepositores, making the current repository
3737
// an "umbrella" repository.
3838
Repositories []Repository `json:"repositories,omitempty"`
@@ -166,27 +166,27 @@ func (o *ODS) Environment(environment string) (*Environment, error) {
166166
return nil, fmt.Errorf("no environment matched '%s', have: %s", environment, strings.Join(envs, ", "))
167167
}
168168

169-
func readLegacy(body []byte) (*ODS, error) {
169+
func readSimplified(body []byte) (*ODS, error) {
170170
if len(body) == 0 {
171171
return nil, errors.New("config is empty")
172172
}
173-
var legacyConfig *legacyODS
173+
var legacyConfig *simplifiedODS
174174
err := yaml.UnmarshalStrict(body, &legacyConfig, func(dec *json.Decoder) *json.Decoder {
175175
dec.DisallowUnknownFields()
176176
return dec
177177
})
178178
if err != nil {
179179
return nil, err
180180
}
181-
odsConfig := convertLegacy(legacyConfig)
181+
odsConfig := convertSimplified(legacyConfig)
182182

183183
if err = odsConfig.Validate(); err != nil {
184184
return nil, err
185185
}
186186
return odsConfig, nil
187187
}
188188

189-
func convertLegacy(ods *legacyODS) *ODS {
189+
func convertSimplified(ods *simplifiedODS) *ODS {
190190
return &ODS{
191191
Repositories: ods.Repositories,
192192
Environments: ods.Environments,
@@ -207,7 +207,7 @@ func Read(body []byte) (*ODS, error) {
207207
return dec
208208
})
209209
if err != nil {
210-
odsConfig, err = readLegacy(body)
210+
odsConfig, err = readSimplified(body)
211211
if err != nil {
212212
return nil, fmt.Errorf("could not unmarshal config: %w", err)
213213
}

pkg/config/ods_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func TestReadFromFile(t *testing.T) {
2929
}
3030
}
3131

32-
func TestReadFromLegacyFormatFile(t *testing.T) {
33-
ods, err := ReadFromFile(filepath.Join(projectpath.Root, "test/testdata/fixtures/config/ods-legacy.yaml"))
32+
func TestReadFromSimplifiedFormatFile(t *testing.T) {
33+
ods, err := ReadFromFile(filepath.Join(projectpath.Root, "test/testdata/fixtures/config/ods-simplified.yaml"))
3434
if err != nil {
3535
t.Fatal(err)
3636
}

test/testdata/fixtures/manager/multi-pipeline-ods.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
pipeline:
22
- trigger:
33
event: ["pr:comment:added"]
4-
branches: ["feature/other"]
4+
branches: ["feature/*"]
5+
exceptBranches: ["feature/foo"]
56
prComment: "/select"
67
tasks:
78
- name: go-helm-build-comment-added-select-foo
@@ -27,7 +28,7 @@ pipeline:
2728
- name: source
2829
workspace: shared-workspace
2930
- trigger:
30-
event: ["pr:opened"]
31+
event: ["pr:opened", "pr:comment:added"]
3132
branches: ["feature/foo"]
3233
tasks:
3334
- name: go-helm-build-opened-pr-foo
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"eventKey":"pr:comment:added",
3+
"date":"2017-09-19T11:21:06+1000",
4+
"actor":{
5+
"name":"max.mustermann@acme.org",
6+
"emailAddress":"max.mustermann@acme.org",
7+
"id":1,
8+
"displayName":"Mustermann,Max ACME",
9+
"active":true,
10+
"slug":"max.mustermann_acme.org",
11+
"type":"NORMAL"
12+
},
13+
"pullRequest":{
14+
"id":1,
15+
"version":0,
16+
"title":"a new file added",
17+
"state":"OPEN",
18+
"open":true,
19+
"closed":false,
20+
"createdDate":1505783860548,
21+
"updatedDate":1505783878981,
22+
"fromRef":{
23+
"id":"refs/heads/feature/foo",
24+
"displayId":"feature/foo",
25+
"latestCommit":"ef8755f06ee4b28c96a847a95cb8ec8ed6ddd1ca",
26+
"repository":{
27+
"slug":"foo-bar",
28+
"id":84,
29+
"name":"foo-bar",
30+
"scmId":"git",
31+
"state":"AVAILABLE",
32+
"statusMessage":"Available",
33+
"forkable":true,
34+
"project":{
35+
"key":"FOO",
36+
"id":84,
37+
"name":"Max Mustermann Playground",
38+
"public":false,
39+
"type":"NORMAL"
40+
},
41+
"public":false
42+
}
43+
},
44+
"toRef":{
45+
"id":"refs/heads/master",
46+
"displayId":"master",
47+
"latestCommit":"178864a7d521b6f5e720b386b2c2b0ef8563e0dc",
48+
"repository":{
49+
"slug":"foo-bar",
50+
"id":84,
51+
"name":"foo-bar",
52+
"scmId":"git",
53+
"state":"AVAILABLE",
54+
"statusMessage":"Available",
55+
"forkable":true,
56+
"project":{
57+
"key":"FOO",
58+
"id":84,
59+
"name":"Max Mustermann Playground",
60+
"public":false,
61+
"type":"NORMAL"
62+
},
63+
"public":false
64+
}
65+
},
66+
"locked":false,
67+
"author":{
68+
"user":{
69+
"name":"max.mustermann@acme.org",
70+
"emailAddress":"max.mustermann@acme.org",
71+
"id":1,
72+
"displayName":"Mustermann,Max ACME",
73+
"active":true,
74+
"slug":"max.mustermann_acme.org",
75+
"type":"NORMAL"
76+
},
77+
"role":"AUTHOR",
78+
"approved":false,
79+
"status":"UNAPPROVED"
80+
},
81+
"reviewers":[
82+
83+
],
84+
"participants":[
85+
86+
]
87+
},
88+
"comment":{
89+
"properties":{
90+
"repositoryId":84
91+
},
92+
"id":62,
93+
"version":0,
94+
"text":"/select",
95+
"author":{
96+
"name":"max.mustermann@acme.org",
97+
"emailAddress":"max.mustermann@acme.org",
98+
"id":1,
99+
"displayName":"Mustermann,Max ACME",
100+
"active":true,
101+
"slug":"max.mustermann_acme.org",
102+
"type":"NORMAL"
103+
},
104+
"createdDate":1505784066751,
105+
"updatedDate":1505784066751,
106+
"comments":[
107+
108+
],
109+
"tasks":[
110+
111+
]
112+
},
113+
"commentParentId":43
114+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"project": "foo",
3+
"component": "bar",
4+
"repository": "foo-bar",
5+
"stage": "dev",
6+
"environment": "",
7+
"version": "",
8+
"gitRef": "feature/foo",
9+
"gitFullRef": "refs/heads/feature/foo",
10+
"gitSha": "ef8755f06ee4b28c96a847a95cb8ec8ed6ddd1ca",
11+
"repoBase": "https://domain.com",
12+
"gitURI": "https://domain.com/foo/foo-bar.git",
13+
"namespace": "bar-cd",
14+
"trigger-event": "pr:comment:added",
15+
"comment": "/select",
16+
"prKey": 1,
17+
"prBase": "refs/heads/master"
18+
}

0 commit comments

Comments
 (0)