From 4ca9b21e498a422dd23d9a1d9c2e0e112db05d8d Mon Sep 17 00:00:00 2001 From: Felix Luthman <34520175+felixlut@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:16:26 +0100 Subject: [PATCH] GitHub org ignore archived repos (#1833) * add ignore_archived_repos argument to github_organization datasource * add test for checking that the archived repo is ignored * add documentation for ignore_archived_repos * fix ordering of imports --------- Co-authored-by: Keegan Campbell --- github/data_source_github_organization.go | 14 +++- .../data_source_github_organization_test.go | 66 +++++++++++++++++++ website/docs/d/organization.html.markdown | 4 ++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/github/data_source_github_organization.go b/github/data_source_github_organization.go index 0c95852f26..ec42b5f28b 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 d4ff5578fd..4b66a6e3bd 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 f7b13acfdd..b230ad2838 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