From 577e1a09a02fde4ace704129a2ebb35ecfd528c6 Mon Sep 17 00:00:00 2001 From: felixlut Date: Fri, 11 Aug 2023 08:56:08 +0200 Subject: [PATCH 1/4] add ignore_archived_repos argument to github_organization datasource --- github/data_source_github_organization.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/github/data_source_github_organization.go b/github/data_source_github_organization.go index 05c66b3eda..63f816dd15 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, @@ -104,8 +109,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 { From 886f53f649d448ded1837f8328b02074242179a8 Mon Sep 17 00:00:00 2001 From: felixlut Date: Fri, 11 Aug 2023 08:58:20 +0200 Subject: [PATCH 2/4] add test for checking that the archived repo is ignored --- .../data_source_github_organization_test.go | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/github/data_source_github_organization_test.go b/github/data_source_github_organization_test.go index 23a35f72a1..b55d485018 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/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) @@ -54,4 +55,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) + }) + + }) + } From e11f5fb775fe663dce1e07051a3274beba312f8d Mon Sep 17 00:00:00 2001 From: felixlut Date: Fri, 11 Aug 2023 09:01:18 +0200 Subject: [PATCH 3/4] add documentation for ignore_archived_repos --- website/docs/d/organization.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/docs/d/organization.html.markdown b/website/docs/d/organization.html.markdown index 07995de558..009dee0b7b 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 From c5655b71a511eab4329f8dd0f36dd8ce5b72a104 Mon Sep 17 00:00:00 2001 From: felixlut Date: Sun, 18 Feb 2024 17:55:43 +0100 Subject: [PATCH 4/4] fix ordering of imports --- github/data_source_github_organization_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_organization_test.go b/github/data_source_github_organization_test.go index e39f5d0ff1..4b66a6e3bd 100644 --- a/github/data_source_github_organization_test.go +++ b/github/data_source_github_organization_test.go @@ -4,8 +4,8 @@ import ( "fmt" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubOrganizationDataSource(t *testing.T) {