-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_test.go
639 lines (597 loc) · 17.9 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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
package resource_test
import (
"testing"
"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 (
standardTestPRs = []*resource.PullRequest{
createTestPR(0, "master", "anonymous", true, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(2, "master", "anonymous", false, false, 0, nil, true, githubv4.PullRequestStateOpen),
createTestPR(3, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(4, "master", "anonymous", false, true, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(5, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(6, "develop", "anonymous", false, false, 0, []string{"enhancement"}, false, githubv4.PullRequestStateOpen),
createTestPR(7, "master", "anonymous", false, false, 1, []string{"wontfix"}, false, githubv4.PullRequestStateOpen),
createTestPR(8, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(9, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateClosed),
createTestPR(10, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateMerged),
createTestPR(11, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
}
multiTeamTestPRs = []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "foo-member", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(2, "master", "bar-member", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
}
)
func TestCheck(t *testing.T) {
tests := []struct {
description string
source resource.Source
version resource.Version
files [][]string
pullRequests []*resource.PullRequest
expectedIndexes []int
}{
{
description: "check returns the latest version if there is no previous",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
pullRequests: standardTestPRs,
files: [][]string{},
expectedIndexes: []int{1},
},
{
description: "check returns all open PRs if there is a previous",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
},
version: resource.NewVersion(standardTestPRs[1]),
pullRequests: standardTestPRs,
files: [][]string{},
expectedIndexes: []int{11, 8, 7, 6, 5, 4, 3, 2, 1},
},
{
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(standardTestPRs[3]),
pullRequests: standardTestPRs,
files: [][]string{
{"README.md", "travis.yml"},
{"terraform/modules/ecs/main.tf", "README.md"},
{"terraform/modules/variables.tf", "travis.yml"},
},
expectedIndexes: []int{3, 2},
},
{
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(standardTestPRs[3]),
pullRequests: standardTestPRs,
files: [][]string{
{"README.md", "travis.yml"},
{"terraform/modules/ecs/main.tf", "README.md"},
{"terraform/modules/variables.tf", "travis.yml"},
},
expectedIndexes: []int{3, 2},
},
{
description: "check correctly ignores [skip ci] when specified",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
DisableCISkip: true,
},
version: resource.NewVersion(standardTestPRs[1]),
pullRequests: standardTestPRs,
expectedIndexes: []int{11, 8, 7, 6, 5, 4, 3, 2, 1, 0},
},
{
description: "check correctly ignores drafts when drafts are ignored",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
IgnoreDrafts: true,
},
version: resource.NewVersion(standardTestPRs[3]),
pullRequests: standardTestPRs,
expectedIndexes: []int{11, 8, 7, 6, 5, 4, 3, 1},
},
{
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(standardTestPRs[3]),
pullRequests: standardTestPRs,
expectedIndexes: []int{11, 8, 7, 6, 5, 4, 3, 2, 1},
},
{
description: "check correctly ignores cross repo pull requests",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
DisableForks: true,
},
version: resource.NewVersion(standardTestPRs[5]),
pullRequests: standardTestPRs,
expectedIndexes: []int{11, 8, 7, 6, 5, 3, 2, 1},
},
{
description: "check supports specifying base branch",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
BaseBranch: "develop",
},
pullRequests: standardTestPRs,
files: [][]string{},
expectedIndexes: []int{6},
},
{
description: "check always allows prs from team members",
pullRequests: []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "foo-member", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
},
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedTeams: []string{"foo"},
},
expectedIndexes: []int{1},
},
{
description: "check always allows prs from team members from more than one team",
pullRequests: multiTeamTestPRs,
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedTeams: []string{"foo", "bar"},
},
version: resource.NewVersion(multiTeamTestPRs[1]),
expectedIndexes: []int{2, 1},
},
{
description: "check does not allow prs from non-members",
pullRequests: []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
},
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedTeams: []string{"foo"},
},
expectedIndexes: nil,
},
{
description: "check allows approved prs from non-members",
pullRequests: []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 1, nil, false, githubv4.PullRequestStateOpen),
},
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedTeams: []string{"foo"},
},
expectedIndexes: []int{0},
},
{
description: "check always allows approved prs from team members",
pullRequests: []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "foo-member", false, false, 1, nil, false, githubv4.PullRequestStateOpen),
},
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedTeams: []string{"foo"},
},
expectedIndexes: []int{1},
},
{
description: "check always allows prs from trusted users",
pullRequests: []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "dependabot[bot]", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
},
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedUsers: []string{"dependabot[bot]"},
},
expectedIndexes: []int{1},
},
{
description: "check always allows approved prs from dependabot",
pullRequests: []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "dependabot[bot]", false, false, 1, nil, false, githubv4.PullRequestStateOpen),
},
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
RequiredReviewApprovals: 1,
TrustedUsers: []string{"dependabot[bot]"},
},
expectedIndexes: []int{1},
},
{
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"},
},
pullRequests: standardTestPRs,
files: [][]string{},
expectedIndexes: []int{6},
},
{
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},
},
pullRequests: standardTestPRs,
files: [][]string{},
expectedIndexes: []int{9},
},
{
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},
},
pullRequests: standardTestPRs[9:11],
files: [][]string{},
expectedIndexes: 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(standardTestPRs[11]),
pullRequests: standardTestPRs,
files: [][]string{},
expectedIndexes: []int{9, 10},
},
}
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].State {
pullRequests = append(pullRequests, tc.pullRequests[i])
break
}
}
}
github.ListPullRequestsReturns(pullRequests, nil)
for i, file := range tc.files {
github.ListModifiedFilesReturnsOnCall(i, file, nil)
}
github.ListTeamMembersStub = func(teamName string) ([]string, error) {
switch teamName {
case "foo":
return []string{"foo-member"}, nil
case "bar":
return []string{"bar-member"}, nil
}
return []string{}, nil
}
input := resource.CheckRequest{Source: tc.source, Version: tc.version}
output, err := resource.Check(input, github)
var expectedOutput resource.CheckResponse
for _, idx := range tc.expectedIndexes {
expectedOutput = append(expectedOutput, resource.NewVersion(tc.pullRequests[idx]))
}
if assert.NoError(t, err) {
assert.Equal(t, expectedOutput, 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)
}
}
})
}
}
func TestCheckTeamMemberCache(t *testing.T) {
t.Run("foo", func(t *testing.T) {
github := new(fakes.FakeGithub)
github.ListTeamMembersReturns([]string{"member"}, nil)
prs := []*resource.PullRequest{
createTestPR(0, "master", "anonymous", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(1, "master", "member", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(2, "master", "member", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
createTestPR(3, "master", "member", false, false, 0, nil, false, githubv4.PullRequestStateOpen),
}
github.ListPullRequestsReturns(prs, nil)
input := resource.CheckRequest{
Source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
TrustedTeams: []string{"team"},
},
Version: resource.NewVersion(prs[0]),
}
_, err := resource.Check(input, github)
assert.Equal(t, nil, err)
assert.Equal(t, 1, github.ListTeamMembersCallCount())
})
}