-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathutil_rules_test.go
More file actions
1425 lines (1202 loc) · 41.4 KB
/
util_rules_test.go
File metadata and controls
1425 lines (1202 loc) · 41.4 KB
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package github
import (
"encoding/json"
"testing"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func TestExpandRulesBasicRules(t *testing.T) {
// Test expanding basic boolean rules with RepositoryRulesetRules
rulesMap := map[string]any{
"creation": true,
"deletion": true,
"required_linear_history": true,
"required_signatures": false,
"non_fast_forward": true,
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
// Check boolean rules - they use EmptyRuleParameters and are nil when false
if result.Creation == nil {
t.Error("Expected Creation rule to be set")
}
if result.Deletion == nil {
t.Error("Expected Deletion rule to be set")
}
if result.RequiredLinearHistory == nil {
t.Error("Expected RequiredLinearHistory rule to be set")
}
if result.RequiredSignatures != nil {
t.Error("Expected RequiredSignatures rule to be nil (false)")
}
if result.NonFastForward == nil {
t.Error("Expected NonFastForward rule to be set")
}
}
func TestFlattenRulesBasicRules(t *testing.T) {
// Test flattening basic boolean rules with RepositoryRulesetRules
rules := &github.RepositoryRulesetRules{
Creation: &github.EmptyRuleParameters{},
Deletion: &github.EmptyRuleParameters{},
RequiredLinearHistory: &github.EmptyRuleParameters{},
RequiredSignatures: nil, // false means nil
NonFastForward: &github.EmptyRuleParameters{},
}
result := flattenRules(t.Context(), rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
// Should contain the rules
if !rulesMap["creation"].(bool) {
t.Error("Expected creation rule to be true")
}
if !rulesMap["deletion"].(bool) {
t.Error("Expected deletion rule to be true")
}
if !rulesMap["required_linear_history"].(bool) {
t.Error("Expected required_linear_history rule to be true")
}
if rulesMap["required_signatures"].(bool) {
t.Error("Expected required_signatures rule to be false")
}
if !rulesMap["non_fast_forward"].(bool) {
t.Error("Expected non_fast_forward rule to be true")
}
}
func TestExpandRulesMaxFilePathLength(t *testing.T) {
// Test that max_file_path_length rule is properly expanded
maxPathLength := 512
rulesMap := map[string]any{
"max_file_path_length": []any{
map[string]any{
"max_file_path_length": maxPathLength,
},
},
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
if result.MaxFilePathLength == nil {
t.Fatal("Expected MaxFilePathLength rule to be set")
return
}
if result.MaxFilePathLength.MaxFilePathLength != maxPathLength {
t.Errorf("Expected MaxFilePathLength to be %d, got %d", maxPathLength, result.MaxFilePathLength.MaxFilePathLength)
}
}
func TestFlattenRulesMaxFilePathLength(t *testing.T) {
// Test that max_file_path_length rule is properly flattened
maxPathLength := 256
rules := &github.RepositoryRulesetRules{
MaxFilePathLength: &github.MaxFilePathLengthRuleParameters{
MaxFilePathLength: maxPathLength,
},
}
result := flattenRules(t.Context(), rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
maxFilePathLengthRules := rulesMap["max_file_path_length"].([]map[string]any)
if len(maxFilePathLengthRules) != 1 {
t.Fatalf("Expected 1 max_file_path_length rule, got %d", len(maxFilePathLengthRules))
}
if maxFilePathLengthRules[0]["max_file_path_length"] != maxPathLength {
t.Errorf("Expected max_file_path_length to be %d, got %v", maxPathLength, maxFilePathLengthRules[0]["max_file_path_length"])
}
}
func TestRoundTripMaxFilePathLength(t *testing.T) {
// Test that max_file_path_length rule survives expand -> flatten round trip
maxPathLength := 1024
// Start with terraform configuration
rulesMap := map[string]any{
"max_file_path_length": []any{
map[string]any{
"max_file_path_length": maxPathLength,
},
},
}
input := []any{rulesMap}
// Expand to GitHub API format
expandedRules := expandRules(input, false)
if expandedRules == nil {
t.Fatal("Expected expandedRules to not be nil")
}
// Flatten back to terraform format
flattenedResult := flattenRules(t.Context(), expandedRules, false)
if len(flattenedResult) != 1 {
t.Fatalf("Expected 1 flattened result, got %d", len(flattenedResult))
}
flattenedRulesMap := flattenedResult[0].(map[string]any)
maxFilePathLengthRules := flattenedRulesMap["max_file_path_length"].([]map[string]any)
if len(maxFilePathLengthRules) != 1 {
t.Fatalf("Expected 1 max_file_path_length rule after round trip, got %d", len(maxFilePathLengthRules))
}
if maxFilePathLengthRules[0]["max_file_path_length"] != maxPathLength {
t.Errorf("Expected max_file_path_length to be %d after round trip, got %v", maxPathLength, maxFilePathLengthRules[0]["max_file_path_length"])
}
}
func TestExpandRulesMaxFileSize(t *testing.T) {
// Test that max_file_size rule is properly expanded
maxFileSize := int64(1048576) // 1MB
rulesMap := map[string]any{
"max_file_size": []any{
map[string]any{
"max_file_size": float64(maxFileSize),
},
},
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
if result.MaxFileSize == nil {
t.Fatal("Expected MaxFileSize rule to be set")
return
}
if result.MaxFileSize.MaxFileSize != maxFileSize {
t.Errorf("Expected MaxFileSize to be %d, got %d", maxFileSize, result.MaxFileSize.MaxFileSize)
}
}
func TestFlattenRulesMaxFileSize(t *testing.T) {
// Test that max_file_size rule is properly flattened
maxFileSize := int64(5242880) // 5MB
rules := &github.RepositoryRulesetRules{
MaxFileSize: &github.MaxFileSizeRuleParameters{
MaxFileSize: maxFileSize,
},
}
result := flattenRules(t.Context(), rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
maxFileSizeRules := rulesMap["max_file_size"].([]map[string]any)
if len(maxFileSizeRules) != 1 {
t.Fatalf("Expected 1 max_file_size rule, got %d", len(maxFileSizeRules))
}
if maxFileSizeRules[0]["max_file_size"] != maxFileSize {
t.Errorf("Expected max_file_size to be %d, got %v", maxFileSize, maxFileSizeRules[0]["max_file_size"])
}
}
func TestExpandRulesFileExtensionRestriction(t *testing.T) {
// Test that file_extension_restriction rule is properly expanded
restrictedExtensions := []string{".exe", ".bat", ".com"}
rulesMap := map[string]any{
"file_extension_restriction": []any{
map[string]any{
"restricted_file_extensions": schema.NewSet(schema.HashString, []any{".exe", ".bat", ".com"}),
},
},
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
if result.FileExtensionRestriction == nil {
t.Fatal("Expected FileExtensionRestriction rule to be set")
return
}
if len(result.FileExtensionRestriction.RestrictedFileExtensions) != len(restrictedExtensions) {
t.Errorf("Expected %d restricted extensions, got %d", len(restrictedExtensions), len(result.FileExtensionRestriction.RestrictedFileExtensions))
}
resultExtensions := make(map[string]bool)
for _, ext := range result.FileExtensionRestriction.RestrictedFileExtensions {
resultExtensions[ext] = true
}
for _, expectedExt := range restrictedExtensions {
if !resultExtensions[expectedExt] {
t.Errorf("Expected extension %s not found in result", expectedExt)
}
}
}
func TestFlattenRulesFileExtensionRestriction(t *testing.T) {
// Test that file_extension_restriction rule is properly flattened
restrictedExtensions := []string{".exe", ".bat", ".com"}
rules := &github.RepositoryRulesetRules{
FileExtensionRestriction: &github.FileExtensionRestrictionRuleParameters{
RestrictedFileExtensions: restrictedExtensions,
},
}
result := flattenRules(t.Context(), rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
fileExtensionRules := rulesMap["file_extension_restriction"].([]map[string]any)
if len(fileExtensionRules) != 1 {
t.Fatalf("Expected 1 file_extension_restriction rule, got %d", len(fileExtensionRules))
}
actualExtensions := fileExtensionRules[0]["restricted_file_extensions"].([]string)
if len(actualExtensions) != len(restrictedExtensions) {
t.Errorf("Expected %d restricted extensions, got %d", len(restrictedExtensions), len(actualExtensions))
}
for i, ext := range restrictedExtensions {
if actualExtensions[i] != ext {
t.Errorf("Expected extension %s at index %d, got %s", ext, i, actualExtensions[i])
}
}
}
func TestCompletePushRulesetSupport(t *testing.T) {
// Test that all push-specific rules are supported together
rulesMap := map[string]any{
"file_path_restriction": []any{
map[string]any{
"restricted_file_paths": []any{"secrets/", "*.key", "private/"},
},
},
"max_file_size": []any{
map[string]any{
"max_file_size": 5, // 5MB
},
},
"max_file_path_length": []any{
map[string]any{
"max_file_path_length": 300,
},
},
"file_extension_restriction": []any{
map[string]any{
"restricted_file_extensions": schema.NewSet(schema.HashString, []any{".exe", ".bat", ".sh"}),
},
},
}
input := []any{rulesMap}
// Expand to GitHub API format
expandedRules := expandRules(input, false)
if expandedRules == nil {
t.Fatal("Expected expandedRules to not be nil")
return
}
// Count how many rules we have
ruleCount := 0
if expandedRules.FilePathRestriction != nil {
ruleCount++
}
if expandedRules.MaxFileSize != nil {
ruleCount++
}
if expandedRules.MaxFilePathLength != nil {
ruleCount++
}
if expandedRules.FileExtensionRestriction != nil {
ruleCount++
}
if ruleCount != 4 {
t.Fatalf("Expected 4 expanded rules for complete push ruleset, got %d", ruleCount)
}
// Flatten back to terraform format
flattenedResult := flattenRules(t.Context(), expandedRules, false)
if len(flattenedResult) != 1 {
t.Fatalf("Expected 1 flattened result, got %d", len(flattenedResult))
}
flattenedRulesMap := flattenedResult[0].(map[string]any)
// Verify file_path_restriction
filePathRules := flattenedRulesMap["file_path_restriction"].([]map[string]any)
if len(filePathRules) != 1 {
t.Fatalf("Expected 1 file_path_restriction rule, got %d", len(filePathRules))
}
restrictedPaths := filePathRules[0]["restricted_file_paths"].([]string)
if len(restrictedPaths) != 3 {
t.Errorf("Expected 3 restricted file paths, got %d", len(restrictedPaths))
}
// Verify max_file_size
maxFileSizeRules := flattenedRulesMap["max_file_size"].([]map[string]any)
if len(maxFileSizeRules) != 1 {
t.Fatalf("Expected 1 max_file_size rule, got %d", len(maxFileSizeRules))
}
if maxFileSizeRules[0]["max_file_size"] != int64(5) {
t.Errorf("Expected max_file_size to be 5, got %v", maxFileSizeRules[0]["max_file_size"])
}
// Verify max_file_path_length
maxFilePathLengthRules := flattenedRulesMap["max_file_path_length"].([]map[string]any)
if len(maxFilePathLengthRules) != 1 {
t.Fatalf("Expected 1 max_file_path_length rule, got %d", len(maxFilePathLengthRules))
}
if maxFilePathLengthRules[0]["max_file_path_length"] != 300 {
t.Errorf("Expected max_file_path_length to be 300, got %v", maxFilePathLengthRules[0]["max_file_path_length"])
}
// Verify file_extension_restriction
fileExtRules := flattenedRulesMap["file_extension_restriction"].([]map[string]any)
if len(fileExtRules) != 1 {
t.Fatalf("Expected 1 file_extension_restriction rule, got %d", len(fileExtRules))
}
restrictedExts := fileExtRules[0]["restricted_file_extensions"].([]string)
if len(restrictedExts) != 3 {
t.Errorf("Expected 3 restricted file extensions, got %d", len(restrictedExts))
}
}
func TestCopilotCodeReviewRoundTrip(t *testing.T) {
// Test that copilot_code_review rule survives expand -> flatten round trip
rulesMap := map[string]any{
"copilot_code_review": []any{
map[string]any{
"review_on_push": true,
"review_draft_pull_requests": false,
},
},
}
input := []any{rulesMap}
// Expand to GitHub API format
expandedRules := expandRules(input, false)
if expandedRules == nil {
t.Fatal("Expected expandedRules to not be nil")
}
if expandedRules.CopilotCodeReview == nil {
t.Fatal("Expected CopilotCodeReview rule to be set")
}
if expandedRules.CopilotCodeReview.ReviewOnPush != true {
t.Errorf("Expected ReviewOnPush to be true, got %v", expandedRules.CopilotCodeReview.ReviewOnPush)
}
if expandedRules.CopilotCodeReview.ReviewDraftPullRequests != false {
t.Errorf("Expected ReviewDraftPullRequests to be false, got %v", expandedRules.CopilotCodeReview.ReviewDraftPullRequests)
}
// Flatten back to terraform format
flattenedResult := flattenRules(t.Context(), expandedRules, false)
if len(flattenedResult) != 1 {
t.Fatalf("Expected 1 flattened result, got %d", len(flattenedResult))
}
flattenedRulesMap := flattenedResult[0].(map[string]any)
copilotRules := flattenedRulesMap["copilot_code_review"].([]map[string]any)
if len(copilotRules) != 1 {
t.Fatalf("Expected 1 copilot_code_review rule after round trip, got %d", len(copilotRules))
}
if copilotRules[0]["review_on_push"] != true {
t.Errorf("Expected review_on_push to be true, got %v", copilotRules[0]["review_on_push"])
}
if copilotRules[0]["review_draft_pull_requests"] != false {
t.Errorf("Expected review_draft_pull_requests to be false, got %v", copilotRules[0]["review_draft_pull_requests"])
}
}
func TestFlattenConditions_PushRuleset_WithRepositoryNameOnly(t *testing.T) {
// Push rulesets don't use ref_name - they only have repository_name or repository_id.
// flattenConditions should return the conditions even when RefName is nil.
conditions := &github.RepositoryRulesetConditions{
RefName: nil, // Push rulesets don't have ref_name
RepositoryName: &github.RepositoryRulesetRepositoryNamesConditionParameters{
Include: []string{"~ALL"},
Exclude: []string{},
},
}
result := flattenConditions(t.Context(), conditions, true) // org=true for organization rulesets
if len(result) != 1 {
t.Fatalf("Expected 1 conditions block, got %d", len(result))
}
conditionsMap := result[0].(map[string]any)
// ref_name should be empty for push rulesets
refNameSlice := conditionsMap["ref_name"]
if refNameSlice != nil {
t.Fatalf("Expected ref_name to be nil, got %T", conditionsMap["ref_name"])
}
// repository_name should be present
repoNameSlice, ok := conditionsMap["repository_name"].([]map[string]any)
if !ok {
t.Fatalf("Expected repository_name to be []map[string]any, got %T", conditionsMap["repository_name"])
}
if len(repoNameSlice) != 1 {
t.Fatalf("Expected 1 repository_name block, got %d", len(repoNameSlice))
}
include, ok := repoNameSlice[0]["include"].([]string)
if !ok {
t.Fatalf("Expected include to be []string, got %T", repoNameSlice[0]["include"])
}
if len(include) != 1 || include[0] != "~ALL" {
t.Errorf("Expected include to be [~ALL], got %v", include)
}
}
func TestFlattenConditions_BranchRuleset_WithRefNameAndRepositoryName(t *testing.T) {
// Branch/tag rulesets have both ref_name and repository_name.
// This test ensures we didn't break the existing behavior.
conditions := &github.RepositoryRulesetConditions{
RefName: &github.RepositoryRulesetRefConditionParameters{
Include: []string{"~DEFAULT_BRANCH", "refs/heads/main"},
Exclude: []string{"refs/heads/experimental-*"},
},
RepositoryName: &github.RepositoryRulesetRepositoryNamesConditionParameters{
Include: []string{"~ALL"},
Exclude: []string{"test-*"},
},
}
result := flattenConditions(t.Context(), conditions, true) // org=true for organization rulesets
if len(result) != 1 {
t.Fatalf("Expected 1 conditions block, got %d", len(result))
}
conditionsMap := result[0].(map[string]any)
// ref_name should be present for branch/tag rulesets
refNameSlice, ok := conditionsMap["ref_name"].([]map[string]any)
if !ok {
t.Fatalf("Expected ref_name to be []map[string]any, got %T", conditionsMap["ref_name"])
}
if len(refNameSlice) != 1 {
t.Fatalf("Expected 1 ref_name block, got %d", len(refNameSlice))
}
refInclude, ok := refNameSlice[0]["include"].([]string)
if !ok {
t.Fatalf("Expected ref_name include to be []string, got %T", refNameSlice[0]["include"])
}
if len(refInclude) != 2 {
t.Errorf("Expected 2 ref_name includes, got %d", len(refInclude))
}
refExclude, ok := refNameSlice[0]["exclude"].([]string)
if !ok {
t.Fatalf("Expected ref_name exclude to be []string, got %T", refNameSlice[0]["exclude"])
}
if len(refExclude) != 1 {
t.Errorf("Expected 1 ref_name exclude, got %d", len(refExclude))
}
// repository_name should also be present
repoNameSlice, ok := conditionsMap["repository_name"].([]map[string]any)
if !ok {
t.Fatalf("Expected repository_name to be []map[string]any, got %T", conditionsMap["repository_name"])
}
if len(repoNameSlice) != 1 {
t.Fatalf("Expected 1 repository_name block, got %d", len(repoNameSlice))
}
repoInclude, ok := repoNameSlice[0]["include"].([]string)
if !ok {
t.Fatalf("Expected repository_name include to be []string, got %T", repoNameSlice[0]["include"])
}
if len(repoInclude) != 1 || repoInclude[0] != "~ALL" {
t.Errorf("Expected repository_name include to be [~ALL], got %v", repoInclude)
}
repoExclude, ok := repoNameSlice[0]["exclude"].([]string)
if !ok {
t.Fatalf("Expected repository_name exclude to be []string, got %T", repoNameSlice[0]["exclude"])
}
if len(repoExclude) != 1 || repoExclude[0] != "test-*" {
t.Errorf("Expected repository_name exclude to be [test-*], got %v", repoExclude)
}
}
func TestFlattenConditions_PushRuleset_WithRepositoryIdOnly(t *testing.T) {
// Push rulesets can also use repository_id instead of repository_name.
conditions := &github.RepositoryRulesetConditions{
RefName: nil, // Push rulesets don't have ref_name
RepositoryID: &github.RepositoryRulesetRepositoryIDsConditionParameters{
RepositoryIDs: []int64{12345, 67890},
},
}
result := flattenConditions(t.Context(), conditions, true) // org=true for organization rulesets
if len(result) != 1 {
t.Fatalf("Expected 1 conditions block, got %d", len(result))
}
conditionsMap := result[0].(map[string]any)
// ref_name should be nil for push rulesets
refNameSlice := conditionsMap["ref_name"]
if refNameSlice != nil {
t.Fatalf("Expected ref_name to be nil, got %T", conditionsMap["ref_name"])
}
// repository_id should be present
repoIDs, ok := conditionsMap["repository_id"].([]int64)
if !ok {
t.Fatalf("Expected repository_id to be []int64, got %T", conditionsMap["repository_id"])
}
if len(repoIDs) != 2 {
t.Fatalf("Expected 2 repository IDs, got %d", len(repoIDs))
}
if repoIDs[0] != 12345 || repoIDs[1] != 67890 {
t.Errorf("Expected repository IDs [12345, 67890], got %v", repoIDs)
}
}
func TestExpandRequiredReviewers(t *testing.T) {
input := []any{
map[string]any{
"reviewer": []any{
map[string]any{
"id": 12345,
"type": "Team",
},
},
"file_patterns": []any{"*.go", "src/**/*.ts"},
"minimum_approvals": 2,
},
map[string]any{
"reviewer": []any{
map[string]any{
"id": 67890,
"type": "Team",
},
},
"file_patterns": []any{"docs/**/*.md"},
"minimum_approvals": 1,
},
}
result := expandRequiredReviewers(input)
if len(result) != 2 {
t.Fatalf("Expected 2 reviewers, got %d", len(result))
}
// Check first reviewer
if result[0].Reviewer == nil {
t.Fatal("Expected first reviewer to have a Reviewer")
}
if *result[0].Reviewer.ID != 12345 {
t.Errorf("Expected first reviewer ID to be 12345, got %d", *result[0].Reviewer.ID)
}
if *result[0].Reviewer.Type != github.RulesetReviewerTypeTeam {
t.Errorf("Expected first reviewer type to be Team, got %s", *result[0].Reviewer.Type)
}
if *result[0].MinimumApprovals != 2 {
t.Errorf("Expected first reviewer minimum approvals to be 2, got %d", *result[0].MinimumApprovals)
}
if len(result[0].FilePatterns) != 2 {
t.Fatalf("Expected first reviewer to have 2 file patterns, got %d", len(result[0].FilePatterns))
}
if result[0].FilePatterns[0] != "*.go" || result[0].FilePatterns[1] != "src/**/*.ts" {
t.Errorf("Unexpected file patterns for first reviewer: %v", result[0].FilePatterns)
}
// Check second reviewer
if result[1].Reviewer == nil {
t.Fatal("Expected second reviewer to have a Reviewer")
}
if *result[1].Reviewer.ID != 67890 {
t.Errorf("Expected second reviewer ID to be 67890, got %d", *result[1].Reviewer.ID)
}
if *result[1].MinimumApprovals != 1 {
t.Errorf("Expected second reviewer minimum approvals to be 1, got %d", *result[1].MinimumApprovals)
}
}
func TestExpandRequiredReviewersEmpty(t *testing.T) {
result := expandRequiredReviewers([]any{})
if result != nil {
t.Error("Expected nil for empty input")
}
result = expandRequiredReviewers(nil)
if result != nil {
t.Error("Expected nil for nil input")
}
}
func TestFlattenRequiredReviewers(t *testing.T) {
reviewerType := github.RulesetReviewerTypeTeam
reviewers := []*github.RulesetRequiredReviewer{
{
MinimumApprovals: new(2),
FilePatterns: []string{"*.go", "src/**/*.ts"},
Reviewer: &github.RulesetReviewer{
ID: new(int64(12345)),
Type: &reviewerType,
},
},
{
MinimumApprovals: new(1),
FilePatterns: []string{"docs/**/*.md"},
Reviewer: &github.RulesetReviewer{
ID: new(int64(67890)),
Type: &reviewerType,
},
},
}
result := flattenRequiredReviewers(reviewers)
if len(result) != 2 {
t.Fatalf("Expected 2 reviewers, got %d", len(result))
}
// Check first reviewer
if result[0]["minimum_approvals"] != 2 {
t.Errorf("Expected first reviewer minimum approvals to be 2, got %v", result[0]["minimum_approvals"])
}
filePatterns := result[0]["file_patterns"].([]string)
if len(filePatterns) != 2 {
t.Fatalf("Expected first reviewer to have 2 file patterns, got %d", len(filePatterns))
}
if filePatterns[0] != "*.go" || filePatterns[1] != "src/**/*.ts" {
t.Errorf("Unexpected file patterns for first reviewer: %v", filePatterns)
}
reviewerBlock := result[0]["reviewer"].([]map[string]any)
if len(reviewerBlock) != 1 {
t.Fatalf("Expected 1 reviewer block, got %d", len(reviewerBlock))
}
if reviewerBlock[0]["id"] != 12345 {
t.Errorf("Expected first reviewer ID to be 12345, got %v", reviewerBlock[0]["id"])
}
if reviewerBlock[0]["type"] != "Team" {
t.Errorf("Expected first reviewer type to be Team, got %v", reviewerBlock[0]["type"])
}
// Check second reviewer
if result[1]["minimum_approvals"] != 1 {
t.Errorf("Expected second reviewer minimum approvals to be 1, got %v", result[1]["minimum_approvals"])
}
}
func TestFlattenRequiredReviewersEmpty(t *testing.T) {
result := flattenRequiredReviewers(nil)
if result != nil {
t.Error("Expected nil for nil input")
}
result = flattenRequiredReviewers([]*github.RulesetRequiredReviewer{})
if result != nil {
t.Error("Expected nil for empty slice input")
}
}
func TestRoundTripRequiredReviewers(t *testing.T) {
// Start with Terraform-style input
input := []any{
map[string]any{
"reviewer": []any{
map[string]any{
"id": 12345,
"type": "Team",
},
},
"file_patterns": []any{"*.go", "src/**/*.ts"},
"minimum_approvals": 2,
},
}
// Expand to go-github types
expanded := expandRequiredReviewers(input)
// Flatten back to Terraform types
flattened := flattenRequiredReviewers(expanded)
// Verify the round trip maintains data
if len(flattened) != 1 {
t.Fatalf("Expected 1 reviewer after round trip, got %d", len(flattened))
}
if flattened[0]["minimum_approvals"] != 2 {
t.Errorf("Expected minimum_approvals to be 2 after round trip, got %v", flattened[0]["minimum_approvals"])
}
filePatterns := flattened[0]["file_patterns"].([]string)
if len(filePatterns) != 2 {
t.Fatalf("Expected 2 file patterns after round trip, got %d", len(filePatterns))
}
reviewerBlock := flattened[0]["reviewer"].([]map[string]any)
if len(reviewerBlock) != 1 {
t.Fatalf("Expected 1 reviewer block after round trip, got %d", len(reviewerBlock))
}
if reviewerBlock[0]["id"] != 12345 {
t.Errorf("Expected reviewer ID to be 12345 after round trip, got %v", reviewerBlock[0]["id"])
}
if reviewerBlock[0]["type"] != "Team" {
t.Errorf("Expected reviewer type to be Team after round trip, got %v", reviewerBlock[0]["type"])
}
}
func TestExpandRepositoryPropertyConditions_SingleInclude(t *testing.T) {
input := []any{
map[string]any{
"include": []any{
map[string]any{
"name": "env",
"source": "custom",
"property_values": []any{"prod"},
},
},
"exclude": []any{},
},
}
result := expandRepositoryPropertyConditions(input)
if result == nil {
t.Fatal("Expected result to not be nil")
}
if len(result.Include) != 1 {
t.Fatalf("Expected 1 include property, got %d", len(result.Include))
}
if len(result.Exclude) != 0 {
t.Fatalf("Expected 0 exclude properties, got %d", len(result.Exclude))
}
prop := result.Include[0]
if prop.Name != "env" {
t.Errorf("Expected name to be 'env', got %s", prop.Name)
}
if prop.Source == nil || *prop.Source != "custom" {
t.Errorf("Expected source to be 'custom', got %v", prop.Source)
}
if len(prop.PropertyValues) != 1 || prop.PropertyValues[0] != "prod" {
t.Errorf("Expected property_values to be ['prod'], got %v", prop.PropertyValues)
}
}
func TestExpandRepositoryPropertyConditions_IncludeAndExclude(t *testing.T) {
input := []any{
map[string]any{
"include": []any{
map[string]any{
"name": "env",
"source": "custom",
"property_values": []any{"prod"},
},
},
"exclude": []any{
map[string]any{
"name": "tier",
"source": "system",
"property_values": []any{"free"},
},
},
},
}
result := expandRepositoryPropertyConditions(input)
if result == nil {
t.Fatal("Expected result to not be nil")
}
if len(result.Include) != 1 {
t.Fatalf("Expected 1 include property, got %d", len(result.Include))
}
if len(result.Exclude) != 1 {
t.Fatalf("Expected 1 exclude property, got %d", len(result.Exclude))
}
includeProp := result.Include[0]
if includeProp.Name != "env" {
t.Errorf("Expected include name to be 'env', got %s", includeProp.Name)
}
if includeProp.Source == nil || *includeProp.Source != "custom" {
t.Errorf("Expected include source to be 'custom', got %v", includeProp.Source)
}
excludeProp := result.Exclude[0]
if excludeProp.Name != "tier" {
t.Errorf("Expected exclude name to be 'tier', got %s", excludeProp.Name)
}
if excludeProp.Source == nil || *excludeProp.Source != "system" {
t.Errorf("Expected exclude source to be 'system', got %v", excludeProp.Source)
}
}
func TestExpandRepositoryPropertyConditions_MultipleValues(t *testing.T) {
input := []any{
map[string]any{
"include": []any{
map[string]any{
"name": "env",
"source": "custom",
"property_values": []any{"prod", "staging", "dev"},
},
},
"exclude": []any{},
},
}
result := expandRepositoryPropertyConditions(input)
if result == nil {
t.Fatal("Expected result to not be nil")
}
if len(result.Include) != 1 {
t.Fatalf("Expected 1 include property, got %d", len(result.Include))
}
prop := result.Include[0]
if len(prop.PropertyValues) != 3 {
t.Fatalf("Expected 3 property values, got %d", len(prop.PropertyValues))
}
expectedValues := []string{"prod", "staging", "dev"}
for i, expected := range expectedValues {
if prop.PropertyValues[i] != expected {
t.Errorf("Expected property_values[%d] to be '%s', got '%s'", i, expected, prop.PropertyValues[i])
}
}
}
func TestExpandRepositoryPropertyConditions_MultipleProperties(t *testing.T) {
input := []any{
map[string]any{
"include": []any{
map[string]any{
"name": "env",
"source": "custom",
"property_values": []any{"prod"},
},
map[string]any{
"name": "tier",
"source": "system",
"property_values": []any{"premium", "enterprise"},
},
},
"exclude": []any{},
},
}
result := expandRepositoryPropertyConditions(input)
if result == nil {
t.Fatal("Expected result to not be nil")
}
if len(result.Include) != 2 {
t.Fatalf("Expected 2 include properties, got %d", len(result.Include))
}
// Check first property
if result.Include[0].Name != "env" {
t.Errorf("Expected first property name to be 'env', got %s", result.Include[0].Name)
}
if len(result.Include[0].PropertyValues) != 1 {
t.Errorf("Expected first property to have 1 value, got %d", len(result.Include[0].PropertyValues))
}
// Check second property
if result.Include[1].Name != "tier" {
t.Errorf("Expected second property name to be 'tier', got %s", result.Include[1].Name)
}
if len(result.Include[1].PropertyValues) != 2 {
t.Errorf("Expected second property to have 2 values, got %d", len(result.Include[1].PropertyValues))
}
}
func TestExpandRepositoryPropertyConditions_NilElements(t *testing.T) {
input := []any{
map[string]any{
"include": []any{
map[string]any{
"name": "env",
"source": "custom",
"property_values": []any{"prod"},