-
Notifications
You must be signed in to change notification settings - Fork 5
/
check_test.go
615 lines (584 loc) · 18.1 KB
/
check_test.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
package resource_test
import (
"testing"
"time"
"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
resource "github.com/telia-oss/github-pr-resource"
"github.com/telia-oss/github-pr-resource/fakes"
)
var (
testPullRequests = []*resource.PullRequest{
createTestPR(1, "master", true, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(2, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(3, "master", false, false, 0, nil, true, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(4, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(5, "master", false, true, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(6, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(7, "develop", false, false, 0, []string{"enhancement"}, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(8, "master", false, false, 1, []string{"wontfix"}, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(9, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(10, "master", false, false, 0, nil, false, githubv4.PullRequestStateClosed, []resource.StatusContext{}),
createTestPR(11, "master", false, false, 0, nil, false, githubv4.PullRequestStateMerged, []resource.StatusContext{}),
createTestPR(12, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{}),
createTestPR(13, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{
{Context: "my-status-check", State: "SUCCESS"},
}),
// multiple status check
createTestPR(14, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{
{Context: "my-status-check", State: "SUCCESS"},
{Context: "my-failed-status-check", State: "FAILURE"},
}),
createTestPR(15, "master", false, false, 0, nil, false, githubv4.PullRequestStateOpen, []resource.StatusContext{
{Context: "my-status-check-2", State: "SUCCESS", CreatedAt: githubv4.DateTime{Time: time.Now().AddDate(0, 0, 1)}},
}),
}
)
func TestCheck(t *testing.T) {
tests := []struct {
description string
source resource.Source
version resource.Version
files [][]string
pullRequests []*resource.PullRequest
expected resource.CheckResponse
}{
{
description: "check returns the latest version if there is no previous",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
StatusFilters: []resource.StatusFilter{},
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
},
},
{
description: "check returns the previous version when its still latest",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
},
},
{
description: "check returns all new versions since the last",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.NewVersion(testPullRequests[3], testPullRequests[3].Tip.PushedDate.Time),
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2], testPullRequests[2].Tip.PushedDate.Time),
resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
},
},
{
description: "check will only return versions that match the specified paths",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
Paths: []string{"terraform/*/*.tf", "terraform/*/*/*.tf"},
},
version: resource.NewVersion(testPullRequests[3], testPullRequests[3].Tip.PushedDate.Time),
pullRequests: testPullRequests,
files: [][]string{
{"README.md", "travis.yml"},
{"terraform/modules/ecs/main.tf", "README.md"},
{"terraform/modules/variables.tf", "travis.yml"},
},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2], testPullRequests[2].Tip.PushedDate.Time),
},
},
{
description: "check will skip versions which only match the ignore paths",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
IgnorePaths: []string{"*.md", "*.yml"},
},
version: resource.NewVersion(testPullRequests[3], testPullRequests[3].Tip.PushedDate.Time),
pullRequests: testPullRequests,
files: [][]string{
{"README.md", "travis.yml"},
{"terraform/modules/ecs/main.tf", "README.md"},
{"terraform/modules/variables.tf", "travis.yml"},
},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2], testPullRequests[2].Tip.PushedDate.Time),
},
},
{
description: "check correctly ignores [skip ci] when specified",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
DisableCISkip: true,
},
version: resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[0], testPullRequests[0].Tip.PushedDate.Time),
},
},
{
description: "check correctly ignores drafts when drafts are ignored",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
IgnoreDrafts: true,
},
version: resource.NewVersion(testPullRequests[3], testPullRequests[3].Tip.PushedDate.Time),
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
},
},
{
description: "check does not ignore drafts when drafts are not ignored",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
IgnoreDrafts: false,
},
version: resource.NewVersion(testPullRequests[3], testPullRequests[3].Tip.PushedDate.Time),
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2], testPullRequests[2].Tip.PushedDate.Time),
resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
},
},
{
description: "check correctly ignores cross repo pull requests",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
DisableForks: true,
},
version: resource.NewVersion(testPullRequests[5], testPullRequests[5].Tip.PushedDate.Time),
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[3], testPullRequests[3].Tip.PushedDate.Time),
resource.NewVersion(testPullRequests[2], testPullRequests[2].Tip.PushedDate.Time),
resource.NewVersion(testPullRequests[1], testPullRequests[1].Tip.PushedDate.Time),
},
},
{
description: "check supports specifying base branch",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
BaseBranch: "develop",
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[6], testPullRequests[6].Tip.PushedDate.Time),
},
},
{
description: "check correctly ignores PRs with no approved reviews when specified",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
},
version: resource.NewVersion(testPullRequests[8], testPullRequests[8].Tip.PushedDate.Time),
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[7], testPullRequests[7].Tip.PushedDate.Time),
},
},
{
description: "check returns latest version from a PR with at least one of the desired labels on it",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
Labels: []string{"enhancement"},
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[6], testPullRequests[6].Tip.PushedDate.Time),
},
},
{
description: "check returns latest version from a PR with a single state filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
States: []githubv4.PullRequestState{githubv4.PullRequestStateClosed},
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[9], testPullRequests[9].Tip.PushedDate.Time),
},
},
{
description: "check filters out versions from a PR which do not match the state filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
States: []githubv4.PullRequestState{githubv4.PullRequestStateOpen},
},
version: resource.Version{},
pullRequests: testPullRequests[9:11],
files: [][]string{},
expected: resource.CheckResponse(nil),
},
{
description: "check returns versions from a PR with multiple state filters",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
States: []githubv4.PullRequestState{githubv4.PullRequestStateClosed, githubv4.PullRequestStateMerged},
},
version: resource.NewVersion(testPullRequests[11], testPullRequests[11].Tip.PushedDate.Time),
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[10], testPullRequests[10].Tip.PushedDate.Time),
resource.NewVersion(testPullRequests[9], testPullRequests[9].Tip.PushedDate.Time),
},
},
{
description: "check returns a PR that has a complete status for a status check",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
StatusFilters: []resource.StatusFilter{
{Context: "my-status-check", State: "success"},
},
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[12], testPullRequests[12].Tip.PushedDate.Time),
},
},
{
description: "check returns a PR where the check created_at is greater than the resource version",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
StatusFilters: []resource.StatusFilter{
{Context: "my-status-check-2", State: "success"},
},
},
version: resource.NewVersion(testPullRequests[9], testPullRequests[9].Tip.PushedDate.Time),
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[9], testPullRequests[9].Tip.PushedDate.Time),
},
},
{
description: "check returns a PR that has multiple required status checks",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
StatusFilters: []resource.StatusFilter{
{Context: "my-status-check", State: "success"},
{Context: "my-failed-status-check", State: "failure"},
},
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
// todo: pull request index
resource.NewVersion(testPullRequests[13], testPullRequests[13].Tip.PushedDate.Time),
},
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
github := new(fakes.FakeGithub)
pullRequests := []*resource.PullRequest{}
filterStates := []githubv4.PullRequestState{githubv4.PullRequestStateOpen}
if len(tc.source.States) > 0 {
filterStates = tc.source.States
}
for i := range tc.pullRequests {
for j := range filterStates {
if filterStates[j] == tc.pullRequests[i].PullRequestObject.State {
pullRequests = append(pullRequests, tc.pullRequests[i])
break
}
}
}
github.ListPullRequestsReturns(pullRequests, nil)
for i, file := range tc.files {
github.ListModifiedFilesReturnsOnCall(i, file, nil)
}
input := resource.CheckRequest{Source: tc.source, Version: tc.version}
output, err := resource.Check(input, github)
if assert.NoError(t, err) {
assert.Equal(t, tc.expected, output)
}
assert.Equal(t, 1, github.ListPullRequestsCallCount())
})
}
}
func TestContainsSkipCI(t *testing.T) {
tests := []struct {
description string
message string
want bool
}{
{
description: "does not just match any symbol in the regexp",
message: "(",
want: false,
},
{
description: "does not match when it should not",
message: "test",
want: false,
},
{
description: "matches [ci skip]",
message: "[ci skip]",
want: true,
},
{
description: "matches [skip ci]",
message: "[skip ci]",
want: true,
},
{
description: "matches trailing skip ci",
message: "trailing [skip ci]",
want: true,
},
{
description: "matches leading skip ci",
message: "[skip ci] leading",
want: true,
},
{
description: "is case insensitive",
message: "case[Skip CI]insensitive",
want: true,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
got := resource.ContainsSkipCI(tc.message)
assert.Equal(t, tc.want, got)
})
}
}
func TestFilterPath(t *testing.T) {
cases := []struct {
description string
pattern string
files []string
want []string
}{
{
description: "returns all matching files",
pattern: "*.txt",
files: []string{
"file1.txt",
"test/file2.txt",
},
want: []string{
"file1.txt",
},
},
{
description: "works with wildcard",
pattern: "test/*",
files: []string{
"file1.txt",
"test/file2.txt",
},
want: []string{
"test/file2.txt",
},
},
{
description: "excludes unmatched files",
pattern: "*/*.txt",
files: []string{
"test/file1.go",
"test/file2.txt",
},
want: []string{
"test/file2.txt",
},
},
{
description: "handles prefix matches",
pattern: "foo/",
files: []string{
"foo/a",
"foo/a.txt",
"foo/a/b/c/d.txt",
"foo",
"bar",
"bar/a.txt",
},
want: []string{
"foo/a",
"foo/a.txt",
"foo/a/b/c/d.txt",
},
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
got, err := resource.FilterPath(tc.files, tc.pattern)
if assert.NoError(t, err) {
assert.Equal(t, tc.want, got)
}
})
}
}
func TestFilterIgnorePath(t *testing.T) {
cases := []struct {
description string
pattern string
files []string
want []string
}{
{
description: "excludes all matching files",
pattern: "*.txt",
files: []string{
"file1.txt",
"test/file2.txt",
},
want: []string{
"test/file2.txt",
},
},
{
description: "works with wildcard",
pattern: "test/*",
files: []string{
"file1.txt",
"test/file2.txt",
},
want: []string{
"file1.txt",
},
},
{
description: "includes unmatched files",
pattern: "*/*.txt",
files: []string{
"test/file1.go",
"test/file2.txt",
},
want: []string{
"test/file1.go",
},
},
{
description: "handles prefix matches",
pattern: "foo/",
files: []string{
"foo/a",
"foo/a.txt",
"foo/a/b/c/d.txt",
"foo",
"bar",
"bar/a.txt",
},
want: []string{
"foo",
"bar",
"bar/a.txt",
},
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
got, err := resource.FilterIgnorePath(tc.files, tc.pattern)
if assert.NoError(t, err) {
assert.Equal(t, tc.want, got)
}
})
}
}
func TestIsInsidePath(t *testing.T) {
cases := []struct {
description string
parent string
expectChildren []string
expectNotChildren []string
want bool
}{
{
description: "basic test",
parent: "foo/bar",
expectChildren: []string{
"foo/bar",
"foo/bar/baz",
},
expectNotChildren: []string{
"foo/barbar",
"foo/baz/bar",
},
},
{
description: "does not match parent directories against child files",
parent: "foo/",
expectChildren: []string{
"foo/bar",
},
expectNotChildren: []string{
"foo",
},
},
{
description: "matches parents without trailing slash",
parent: "foo/bar",
expectChildren: []string{
"foo/bar",
"foo/bar/baz",
},
},
{
description: "handles children that are shorter than the parent",
parent: "foo/bar/baz",
expectNotChildren: []string{
"foo",
"foo/bar",
},
},
}
for _, tc := range cases {
t.Run(tc.description, func(t *testing.T) {
for _, expectedChild := range tc.expectChildren {
if !resource.IsInsidePath(tc.parent, expectedChild) {
t.Errorf("Expected \"%s\" to be inside \"%s\"", expectedChild, tc.parent)
}
}
for _, expectedNotChild := range tc.expectNotChildren {
if resource.IsInsidePath(tc.parent, expectedNotChild) {
t.Errorf("Expected \"%s\" to not be inside \"%s\"", expectedNotChild, tc.parent)
}
}
})
}
}