diff --git a/github/data_source_github_organization.go b/github/data_source_github_organization.go index 0c95852f2..ec42b5f28 100644 --- a/github/data_source_github_organization.go +++ b/github/data_source_github_organization.go @@ -17,6 +17,11 @@ func dataSourceGithubOrganization() *schema.Resource { Type: schema.TypeString, Required: true, }, + "ignore_archived_repos": { + Type: schema.TypeBool, + Default: false, + Optional: true, + }, "orgname": { Type: schema.TypeString, Computed: true, @@ -176,8 +181,15 @@ func dataSourceGithubOrganizationRead(d *schema.ResourceData, meta interface{}) break } } + + ignoreArchiveRepos := d.Get("ignore_archived_repos").(bool) for index := range allRepos { - repoList = append(repoList, allRepos[index].GetFullName()) + repo := allRepos[index] + if ignoreArchiveRepos && repo.GetArchived() { + continue + } + + repoList = append(repoList, repo.GetFullName()) } var query struct { diff --git a/github/data_source_github_organization_test.go b/github/data_source_github_organization_test.go index d4ff5578f..4b66a6e3b 100644 --- a/github/data_source_github_organization_test.go +++ b/github/data_source_github_organization_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) @@ -72,4 +73,69 @@ func TestAccGithubOrganizationDataSource(t *testing.T) { }) }) + + t.Run("queries for an organization with archived repos", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + config := fmt.Sprintf(` + resource "github_repository" "archived" { + name = "tf-acc-archived-%s" + archived = true + } + + data "github_organization" "skip_archived" { + name = "%s" + ignore_archived_repos = true + depends_on = [ + github_repository.archived, + ] + } + data "github_organization" "all_repos" { + name = "%s" + ignore_archived_repos = false + depends_on = [ + github_repository.archived, + ] + } + + output "should_be_false" { + value = contains(data.github_organization.skip_archived.repositories, github_repository.archived.full_name) + } + output "should_be_true" { + value = contains(data.github_organization.all_repos.repositories, github_repository.archived.full_name) + } + `, randomID, testOrganization, testOrganization) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckOutput("should_be_false", "false"), + resource.TestCheckOutput("should_be_true", "true"), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + + }) + } diff --git a/website/docs/d/organization.html.markdown b/website/docs/d/organization.html.markdown index f7b13acfd..b230ad283 100644 --- a/website/docs/d/organization.html.markdown +++ b/website/docs/d/organization.html.markdown @@ -17,6 +17,10 @@ data "github_organization" "example" { } ``` +## Argument Reference + +* `ignore_archived_repos` - Whether or not to include archived repos in the `repositories` list + ## Attributes Reference * `id` - The ID of the organization