@@ -379,4 +379,179 @@ EOT
379379 },
380380 })
381381 })
382+
383+ t .Run ("queries a repository using owner and name" , func (t * testing.T ) {
384+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
385+
386+ config := fmt .Sprintf (`
387+ resource "github_repository" "test" {
388+ name = "tf-acc-%s"
389+ }
390+
391+ data "github_repository" "test" {
392+ name = github_repository.test.name
393+ owner = "%s"
394+ }
395+ ` , randomID , testOrganization )
396+
397+ check := resource .ComposeTestCheckFunc (
398+ resource .TestCheckResourceAttr (
399+ "data.github_repository.test" , "owner" ,
400+ testOrganization ,
401+ ),
402+ )
403+
404+ testCase := func (t * testing.T , mode string ) {
405+ resource .Test (t , resource.TestCase {
406+ PreCheck : func () { skipUnlessMode (t , mode ) },
407+ Providers : testAccProviders ,
408+ Steps : []resource.TestStep {
409+ {
410+ Config : config ,
411+ Check : check ,
412+ },
413+ },
414+ })
415+ }
416+
417+ t .Run ("with an anonymous account" , func (t * testing.T ) {
418+ testCase (t , anonymous )
419+ })
420+
421+ t .Run ("with an individual account" , func (t * testing.T ) {
422+ testCase (t , individual )
423+ })
424+ t .Run ("with an organization account" , func (t * testing.T ) {
425+ testCase (t , organization )
426+ })
427+ })
428+
429+ t .Run ("validates conflicts between full_name, name, and owner" , func (t * testing.T ) {
430+ randomID := acctest .RandStringFromCharSet (5 , acctest .CharSetAlphaNum )
431+
432+ config := fmt .Sprintf (`
433+ resource "github_repository" "test" {
434+ name = "tf-acc-%[1]s"
435+ vulnerability_alerts = true
436+ }
437+ ` , randomID )
438+
439+ // Test invalid combinations
440+ invalidConfigs := []string {
441+ // full_name with name
442+ fmt .Sprintf (`
443+ resource "github_repository" "test" {
444+ name = "tf-acc-%[1]s"
445+ vulnerability_alerts = true
446+ }
447+
448+ data "github_repository" "test" {
449+ full_name = "%[2]s/tf-acc-%[1]s"
450+ name = "tf-acc-%[1]s"
451+ }
452+ ` , randomID , testOrganization ),
453+ // full_name with owner
454+ fmt .Sprintf (`
455+ resource "github_repository" "test" {
456+ name = "tf-acc-%[1]s"
457+ }
458+
459+ data "github_repository" "test" {
460+ full_name = "%[2]s/tf-acc-%[1]s"
461+ owner = "%[2]s"
462+ }
463+ ` , randomID , testOrganization ),
464+ // full_name with both name and owner
465+ fmt .Sprintf (`
466+ resource "github_repository" "test" {
467+ name = "tf-acc-%[1]s"
468+ }
469+
470+ data "github_repository" "test" {
471+ full_name = "%[2]s/tf-acc-%[1]s"
472+ name = "tf-acc-%[1]s"
473+ owner = "%[2]s"
474+ }
475+ ` , randomID , testOrganization ),
476+ }
477+
478+ // Test valid combinations
479+ validConfigs := []string {
480+ // Just full_name
481+ fmt .Sprintf (`
482+ resource "github_repository" "test" {
483+ name = "tf-acc-%[1]s"
484+ }
485+
486+ data "github_repository" "test" {
487+ full_name = "%[2]s/tf-acc-%[1]s"
488+ }
489+ ` , randomID , testOrganization ),
490+ // Just name (uses provider owner)
491+ fmt .Sprintf (`
492+ resource "github_repository" "test" {
493+ name = "tf-acc-%[1]s"
494+ }
495+
496+ data "github_repository" "test" {
497+ name = "tf-acc-%[1]s"
498+ }
499+ ` , randomID ),
500+ // name with owner
501+ fmt .Sprintf (`
502+ resource "github_repository" "test" {
503+ name = "tf-acc-%[1]s"
504+ }
505+
506+ data "github_repository" "test" {
507+ name = "tf-acc-%[1]s"
508+ owner = "%[2]s"
509+ }
510+ ` , randomID , testOrganization ),
511+ }
512+
513+ testCase := func (t * testing.T , mode string ) {
514+ resource .Test (t , resource.TestCase {
515+ PreCheck : func () { skipUnlessMode (t , mode ) },
516+ Providers : testAccProviders ,
517+ Steps : []resource.TestStep {
518+ // Create the repository first
519+ {
520+ Config : config ,
521+ },
522+ // Test that invalid configs fail
523+ {
524+ Config : invalidConfigs [0 ],
525+ ExpectError : regexp .MustCompile ("(?i)conflicts with" ),
526+ },
527+ {
528+ Config : invalidConfigs [1 ],
529+ ExpectError : regexp .MustCompile ("(?i)conflicts with" ),
530+ },
531+ {
532+ Config : invalidConfigs [2 ],
533+ ExpectError : regexp .MustCompile ("(?i)conflicts with" ),
534+ },
535+ // Test that valid configs succeed
536+ {
537+ Config : validConfigs [0 ],
538+ },
539+ {
540+ Config : validConfigs [1 ],
541+ },
542+ {
543+ Config : validConfigs [2 ],
544+ },
545+ },
546+ })
547+ }
548+
549+ t .Run ("with an individual account" , func (t * testing.T ) {
550+ testCase (t , individual )
551+ })
552+
553+ t .Run ("with an organization account" , func (t * testing.T ) {
554+ testCase (t , organization )
555+ })
556+ })
382557}
0 commit comments