@@ -303,6 +303,23 @@ func TestGithubOrganizationRulesets(t *testing.T) {
303303 target = "branch"
304304 enforcement = "active"
305305
306+ bypass_actors {
307+ actor_type = "DeployKey"
308+ bypass_mode = "always"
309+ }
310+
311+ bypass_actors {
312+ actor_id = 5
313+ actor_type = "RepositoryRole"
314+ bypass_mode = "always"
315+ }
316+
317+ bypass_actors {
318+ actor_id = 1
319+ actor_type = "OrganizationAdmin"
320+ bypass_mode = "always"
321+ }
322+
306323 conditions {
307324 ref_name {
308325 include = ["~ALL"]
@@ -323,39 +340,147 @@ func TestGithubOrganizationRulesets(t *testing.T) {
323340 dismiss_stale_reviews_on_push = true
324341 require_last_push_approval = true
325342 }
343+ }
344+ }
345+ ` , randomID )
326346
327- bypass_actors {
328- actor_type = "DeployKey"
329- bypass_mode = "always"
330- }
347+ check := resource .ComposeTestCheckFunc (
348+ resource .TestCheckResourceAttr (
349+ "github_organization_ruleset.test" , "bypass_actors.#" ,
350+ "3" ,
351+ ),
352+ resource .TestCheckResourceAttr (
353+ "github_organization_ruleset.test" , "bypass_actors.0.actor_type" ,
354+ "DeployKey" ,
355+ ),
356+ resource .TestCheckResourceAttr (
357+ "github_organization_ruleset.test" , "bypass_actors.0.bypass_mode" ,
358+ "always" ,
359+ ),
360+ resource .TestCheckResourceAttr (
361+ "github_organization_ruleset.test" , "bypass_actors.1.actor_id" ,
362+ "5" ,
363+ ),
364+ resource .TestCheckResourceAttr (
365+ "github_organization_ruleset.test" , "bypass_actors.1.actor_type" ,
366+ "RepositoryRole" ,
367+ ),
368+ resource .TestCheckResourceAttr (
369+ "github_organization_ruleset.test" , "bypass_actors.1.bypass_mode" ,
370+ "always" ,
371+ ),
372+ resource .TestCheckResourceAttr (
373+ "github_organization_ruleset.test" , "bypass_actors.2.actor_id" ,
374+ "1" ,
375+ ),
376+ resource .TestCheckResourceAttr (
377+ "github_organization_ruleset.test" , "bypass_actors.2.actor_type" ,
378+ "OrganizationAdmin" ,
379+ ),
380+ resource .TestCheckResourceAttr (
381+ "github_organization_ruleset.test" , "bypass_actors.2.bypass_mode" ,
382+ "always" ,
383+ ),
384+ )
331385
332- bypass_actors {
333- actor_id = 5
334- actor_type = "RepositoryRole"
335- bypass_mode = "always"
336- }
386+ testCase := func (t * testing.T , mode string ) {
387+ resource .Test (t , resource.TestCase {
388+ PreCheck : func () { skipUnlessMode (t , mode ) },
389+ Providers : testAccProviders ,
390+ Steps : []resource.TestStep {
391+ {
392+ Config : config ,
393+ Check : check ,
394+ },
395+ },
396+ })
397+ }
398+
399+ t .Run ("with an enterprise account" , func (t * testing.T ) {
400+ testCase (t , enterprise )
401+ })
402+
403+ })
404+
405+ t .Run ("Creates organization ruleset with all bypass_modes" , func (t * testing.T ) {
337406
338- bypass_actors {
339- actor_id = 0
340- actor_type = "OrganizationAdmin"
341- bypass_mode = "always"
407+ config := fmt .Sprintf (`
408+ resource "github_organization_ruleset" "test" {
409+ name = "test-bypass-modes-%s"
410+ target = "branch"
411+ enforcement = "active"
412+
413+ bypass_actors {
414+ actor_id = 1
415+ actor_type = "OrganizationAdmin"
416+ bypass_mode = "always"
417+ }
418+
419+ bypass_actors {
420+ actor_id = 5
421+ actor_type = "RepositoryRole"
422+ bypass_mode = "pull_request"
423+ }
424+
425+ bypass_actors {
426+ actor_id = 2
427+ actor_type = "RepositoryRole"
428+ bypass_mode = "exempt"
429+ }
430+
431+ conditions {
432+ ref_name {
433+ include = ["~ALL"]
434+ exclude = []
342435 }
343436 }
437+
438+ rules {
439+ creation = true
440+ }
344441 }
345442 ` , randomID )
346443
347444 check := resource .ComposeTestCheckFunc (
445+ resource .TestCheckResourceAttr (
446+ "github_organization_ruleset.test" , "bypass_actors.#" ,
447+ "3" ,
448+ ),
449+ resource .TestCheckResourceAttr (
450+ "github_organization_ruleset.test" , "bypass_actors.0.actor_id" ,
451+ "1" ,
452+ ),
348453 resource .TestCheckResourceAttr (
349454 "github_organization_ruleset.test" , "bypass_actors.0.actor_type" ,
350- "0 " ,
455+ "OrganizationAdmin " ,
351456 ),
352457 resource .TestCheckResourceAttr (
353- "github_organization_ruleset.test" , "bypass_actors.1.actor_type" ,
458+ "github_organization_ruleset.test" , "bypass_actors.0.bypass_mode" ,
459+ "always" ,
460+ ),
461+ resource .TestCheckResourceAttr (
462+ "github_organization_ruleset.test" , "bypass_actors.1.actor_id" ,
354463 "5" ,
355464 ),
465+ resource .TestCheckResourceAttr (
466+ "github_organization_ruleset.test" , "bypass_actors.1.actor_type" ,
467+ "RepositoryRole" ,
468+ ),
469+ resource .TestCheckResourceAttr (
470+ "github_organization_ruleset.test" , "bypass_actors.1.bypass_mode" ,
471+ "pull_request" ,
472+ ),
473+ resource .TestCheckResourceAttr (
474+ "github_organization_ruleset.test" , "bypass_actors.2.actor_id" ,
475+ "2" ,
476+ ),
356477 resource .TestCheckResourceAttr (
357478 "github_organization_ruleset.test" , "bypass_actors.2.actor_type" ,
358- "0" ,
479+ "RepositoryRole" ,
480+ ),
481+ resource .TestCheckResourceAttr (
482+ "github_organization_ruleset.test" , "bypass_actors.2.bypass_mode" ,
483+ "exempt" ,
359484 ),
360485 )
361486
@@ -378,4 +503,76 @@ func TestGithubOrganizationRulesets(t *testing.T) {
378503
379504 })
380505
506+ t .Run ("Updates organization ruleset bypass_mode without error" , func (t * testing.T ) {
507+
508+ config := fmt .Sprintf (`
509+ resource "github_organization_ruleset" "test" {
510+ name = "test-bypass-update-%s"
511+ target = "branch"
512+ enforcement = "active"
513+
514+ bypass_actors {
515+ actor_id = 1
516+ actor_type = "OrganizationAdmin"
517+ bypass_mode = "always"
518+ }
519+
520+ conditions {
521+ ref_name {
522+ include = ["~ALL"]
523+ exclude = []
524+ }
525+ }
526+
527+ rules {
528+ creation = true
529+ }
530+ }
531+ ` , randomID )
532+
533+ configUpdated := strings .Replace (
534+ config ,
535+ `bypass_mode = "always"` ,
536+ `bypass_mode = "exempt"` ,
537+ 1 ,
538+ )
539+
540+ checks := map [string ]resource.TestCheckFunc {
541+ "before" : resource .ComposeTestCheckFunc (
542+ resource .TestCheckResourceAttr (
543+ "github_organization_ruleset.test" , "bypass_actors.0.bypass_mode" ,
544+ "always" ,
545+ ),
546+ ),
547+ "after" : resource .ComposeTestCheckFunc (
548+ resource .TestCheckResourceAttr (
549+ "github_organization_ruleset.test" , "bypass_actors.0.bypass_mode" ,
550+ "exempt" ,
551+ ),
552+ ),
553+ }
554+
555+ testCase := func (t * testing.T , mode string ) {
556+ resource .Test (t , resource.TestCase {
557+ PreCheck : func () { skipUnlessMode (t , mode ) },
558+ Providers : testAccProviders ,
559+ Steps : []resource.TestStep {
560+ {
561+ Config : config ,
562+ Check : checks ["before" ],
563+ },
564+ {
565+ Config : configUpdated ,
566+ Check : checks ["after" ],
567+ },
568+ },
569+ })
570+ }
571+
572+ t .Run ("with an enterprise account" , func (t * testing.T ) {
573+ testCase (t , enterprise )
574+ })
575+
576+ })
577+
381578}
0 commit comments