@@ -428,4 +428,179 @@ EOT
428428 testCase (t , organization )
429429 })
430430 })
431+
432+ t .Run ("queries a repository using owner and name" , func (t * testing.T ) {
433+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
434+
435+ config := fmt .Sprintf (`
436+ resource "github_repository" "test" {
437+ name = "tf-acc-%s"
438+ }
439+
440+ data "github_repository" "test" {
441+ name = github_repository.test.name
442+ owner = "%s"
443+ }
444+ ` , randomID , testOrganization )
445+
446+ check := resource .ComposeTestCheckFunc (
447+ resource .TestCheckResourceAttr (
448+ "data.github_repository.test" , "owner" ,
449+ testOrganization ,
450+ ),
451+ )
452+
453+ testCase := func (t * testing.T , mode string ) {
454+ resource .Test (t , resource.TestCase {
455+ PreCheck : func () { skipUnlessMode (t , mode ) },
456+ Providers : testAccProviders ,
457+ Steps : []resource.TestStep {
458+ {
459+ Config : config ,
460+ Check : check ,
461+ },
462+ },
463+ })
464+ }
465+
466+ t .Run ("with an anonymous account" , func (t * testing.T ) {
467+ testCase (t , anonymous )
468+ })
469+
470+ t .Run ("with an individual account" , func (t * testing.T ) {
471+ testCase (t , individual )
472+ })
473+ t .Run ("with an organization account" , func (t * testing.T ) {
474+ testCase (t , organization )
475+ })
476+ })
477+
478+ t .Run ("validates conflicts between full_name, name, and owner" , func (t * testing.T ) {
479+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
480+
481+ config := fmt .Sprintf (`
482+ resource "github_repository" "test" {
483+ name = "tf-acc-%[1]s"
484+ vulnerability_alerts = true
485+ }
486+ ` , randomID )
487+
488+ // Test invalid combinations
489+ invalidConfigs := []string {
490+ // full_name with name
491+ fmt .Sprintf (`
492+ resource "github_repository" "test" {
493+ name = "tf-acc-%[1]s"
494+ vulnerability_alerts = true
495+ }
496+
497+ data "github_repository" "test" {
498+ full_name = "%[2]s/tf-acc-%[1]s"
499+ name = "tf-acc-%[1]s"
500+ }
501+ ` , randomID , testOrganization ),
502+ // full_name with owner
503+ fmt .Sprintf (`
504+ resource "github_repository" "test" {
505+ name = "tf-acc-%[1]s"
506+ }
507+
508+ data "github_repository" "test" {
509+ full_name = "%[2]s/tf-acc-%[1]s"
510+ owner = "%[2]s"
511+ }
512+ ` , randomID , testOrganization ),
513+ // full_name with both name and owner
514+ fmt .Sprintf (`
515+ resource "github_repository" "test" {
516+ name = "tf-acc-%[1]s"
517+ }
518+
519+ data "github_repository" "test" {
520+ full_name = "%[2]s/tf-acc-%[1]s"
521+ name = "tf-acc-%[1]s"
522+ owner = "%[2]s"
523+ }
524+ ` , randomID , testOrganization ),
525+ }
526+
527+ // Test valid combinations
528+ validConfigs := []string {
529+ // Just full_name
530+ fmt .Sprintf (`
531+ resource "github_repository" "test" {
532+ name = "tf-acc-%[1]s"
533+ }
534+
535+ data "github_repository" "test" {
536+ full_name = "%[2]s/tf-acc-%[1]s"
537+ }
538+ ` , randomID , testOrganization ),
539+ // Just name (uses provider owner)
540+ fmt .Sprintf (`
541+ resource "github_repository" "test" {
542+ name = "tf-acc-%[1]s"
543+ }
544+
545+ data "github_repository" "test" {
546+ name = "tf-acc-%[1]s"
547+ }
548+ ` , randomID ),
549+ // name with owner
550+ fmt .Sprintf (`
551+ resource "github_repository" "test" {
552+ name = "tf-acc-%[1]s"
553+ }
554+
555+ data "github_repository" "test" {
556+ name = "tf-acc-%[1]s"
557+ owner = "%[2]s"
558+ }
559+ ` , randomID , testOrganization ),
560+ }
561+
562+ testCase := func (t * testing.T , mode string ) {
563+ resource .Test (t , resource.TestCase {
564+ PreCheck : func () { skipUnlessMode (t , mode ) },
565+ Providers : testAccProviders ,
566+ Steps : []resource.TestStep {
567+ // Create the repository first
568+ {
569+ Config : config ,
570+ },
571+ // Test that invalid configs fail
572+ {
573+ Config : invalidConfigs [0 ],
574+ ExpectError : regexp .MustCompile ("(?i)conflicts with" ),
575+ },
576+ {
577+ Config : invalidConfigs [1 ],
578+ ExpectError : regexp .MustCompile ("(?i)conflicts with" ),
579+ },
580+ {
581+ Config : invalidConfigs [2 ],
582+ ExpectError : regexp .MustCompile ("(?i)conflicts with" ),
583+ },
584+ // Test that valid configs succeed
585+ {
586+ Config : validConfigs [0 ],
587+ },
588+ {
589+ Config : validConfigs [1 ],
590+ },
591+ {
592+ Config : validConfigs [2 ],
593+ },
594+ },
595+ })
596+ }
597+
598+ t .Run ("with an individual account" , func (t * testing.T ) {
599+ testCase (t , individual )
600+ })
601+
602+ t .Run ("with an organization account" , func (t * testing.T ) {
603+ testCase (t , organization )
604+ })
605+ })
431606}
0 commit comments