From 55c08efec8eb33abc0336f4249fb27044ca1e1b0 Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Fri, 29 Nov 2024 01:21:58 +0100 Subject: [PATCH 1/8] feat: Add `github_organization_repositories` data source --- ...source_github_organization_repositories.go | 77 +++++++++++++++++++ ...e_github_organization_repositories_test.go | 71 +++++++++++++++++ github/provider.go | 1 + .../d/organization_repositories.html.markdown | 31 ++++++++ 4 files changed, 180 insertions(+) create mode 100644 github/data_source_github_organization_repositories.go create mode 100644 github/data_source_github_organization_repositories_test.go create mode 100644 website/docs/d/organization_repositories.html.markdown diff --git a/github/data_source_github_organization_repositories.go b/github/data_source_github_organization_repositories.go new file mode 100644 index 0000000000..4a3fac2914 --- /dev/null +++ b/github/data_source_github_organization_repositories.go @@ -0,0 +1,77 @@ +package github + +import ( + "github.com/google/go-github/v66/github" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceGithubOrganizationRepositories() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGithubOrganizationRepositoriesRead, + Schema: map[string]*schema.Schema{ + "repositories": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "repo_id": { + Type: schema.TypeInt, + Computed: true, + }, + "node_id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "archived": { + Type: schema.TypeBool, + Computed: true, + }, + "visibility": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceGithubOrganizationRepositoriesRead(d *schema.ResourceData, meta interface{}) error { + org := meta.(*Owner).name + client3 := meta.(*Owner).v3client + ctx := meta.(*Owner).StopContext + + options := github.RepositoryListByOrgOptions{ + ListOptions: github.ListOptions{PerPage: 100}, + } + var allRepositories []map[string]interface{} + for { + repositories, resp, err := client3.Repositories.ListByOrg(ctx, org, &options) + if err != nil { + return err + } + for _, repository := range repositories { + repo := make(map[string]interface{}) + repo["repo_id"] = repository.GetID() + repo["node_id"] = repository.GetNodeID() + repo["name"] = repository.GetName() + repo["archived"] = repository.GetArchived() + repo["visibility"] = repository.GetVisibility() + allRepositories = append(allRepositories, repo) + } + if resp.NextPage == 0 { + break + } + options.Page = resp.NextPage + } + + d.SetId(org) + d.Set("repositories", allRepositories) + + return nil +} diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go new file mode 100644 index 0000000000..e192a71039 --- /dev/null +++ b/github/data_source_github_organization_repositories_test.go @@ -0,0 +1,71 @@ +package github + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { + t.Run("manages repositories", func(t *testing.T) { + config := ` + resource "github_repository" "test1" { + name = "test1" + } + + resource "github_repository" "test2" { + name = "test2" + archived = true + depends_on = [github_repository.test1] + } + ` + + config2 := config + ` + data "github_organization_repositories" "all" {} + ` + + const resourceName = "data.github_organization_repositories.all" + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, "webhooks.#", "2"), + resource.TestCheckResourceAttr(resourceName, "webhooks.0.name", "test1"), + resource.TestCheckResourceAttr(resourceName, "webhooks.0.archived", "false"), + resource.TestCheckResourceAttr(resourceName, "webhooks.0.visibility", "private"), + resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.repo_id"), + resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.node_id"), + resource.TestCheckResourceAttr(resourceName, "webhooks.1.name", "test2"), + resource.TestCheckResourceAttr(resourceName, "webhooks.1.archived", "true"), + resource.TestCheckResourceAttr(resourceName, "webhooks.1.visibility", "private"), + resource.TestCheckResourceAttrSet(resourceName, "webhooks.1.repo_id"), + resource.TestCheckResourceAttrSet(resourceName, "webhooks.1.node_id"), + ) + + 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: resource.ComposeTestCheckFunc(), + }, + { + Config: config2, + 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/github/provider.go b/github/provider.go index a9a04d0474..a0f534330d 100644 --- a/github/provider.go +++ b/github/provider.go @@ -232,6 +232,7 @@ func Provider() *schema.Provider { "github_organization_custom_role": dataSourceGithubOrganizationCustomRole(), "github_organization_external_identities": dataSourceGithubOrganizationExternalIdentities(), "github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(), + "github_organization_repositories": dataSourceGithubOrganizationRepositories(), "github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(), "github_organization_teams": dataSourceGithubOrganizationTeams(), "github_organization_webhooks": dataSourceGithubOrganizationWebhooks(), diff --git a/website/docs/d/organization_repositories.html.markdown b/website/docs/d/organization_repositories.html.markdown new file mode 100644 index 0000000000..793b3a90e9 --- /dev/null +++ b/website/docs/d/organization_repositories.html.markdown @@ -0,0 +1,31 @@ +--- +layout: "github" +page_title: "GitHub: github_organization_repositories" +description: |- + Read details of all repositories of an organization. +--- + +# github\_organization\_repositories + +Use this data source to retrieve all repositories of the organization. + +## Example Usage + +To retrieve *all* repositories of the organization: + +```hcl +data "github_organization_repositories" "all" {} +``` + +## Attributes Reference + +* `repository` - An Array of GitHub repositories. Each `repository` block consists of the fields documented below. +___ + +The `repository` block consists of: + + * `repo_id` - GitHub ID for the repository. + * `node_id` - The Node ID of the repository. + * `name` - The name of the repository. + * `archived` - Whether the repository is archived. + * `visibility` - Whether the repository is public, private or internal. From 83a5ab52e0916394c363994a9ac9d7853688a95e Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Fri, 29 Nov 2024 01:37:05 +0100 Subject: [PATCH 2/8] Adjust test --- github/data_source_github_organization_repositories_test.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go index e192a71039..9ca7aee9a5 100644 --- a/github/data_source_github_organization_repositories_test.go +++ b/github/data_source_github_organization_repositories_test.go @@ -10,12 +10,14 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { t.Run("manages repositories", func(t *testing.T) { config := ` resource "github_repository" "test1" { - name = "test1" + name = "test1" + visibility = "private" } resource "github_repository" "test2" { name = "test2" archived = true + visibility = "public" depends_on = [github_repository.test1] } ` @@ -34,7 +36,7 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.node_id"), resource.TestCheckResourceAttr(resourceName, "webhooks.1.name", "test2"), resource.TestCheckResourceAttr(resourceName, "webhooks.1.archived", "true"), - resource.TestCheckResourceAttr(resourceName, "webhooks.1.visibility", "private"), + resource.TestCheckResourceAttr(resourceName, "webhooks.1.visibility", "public"), resource.TestCheckResourceAttrSet(resourceName, "webhooks.1.repo_id"), resource.TestCheckResourceAttrSet(resourceName, "webhooks.1.node_id"), ) From 7d5f5afabe0b6cf5802be060a37a9ad071c7e12a Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Sun, 1 Mar 2026 14:56:54 +0100 Subject: [PATCH 3/8] Review --- github/data_source_github_organization.go | 1 + ...source_github_organization_repositories.go | 21 +++++-- ...e_github_organization_repositories_test.go | 59 +++++++++++-------- 3 files changed, 51 insertions(+), 30 deletions(-) diff --git a/github/data_source_github_organization.go b/github/data_source_github_organization.go index fd705e786b..a7d042fe71 100644 --- a/github/data_source_github_organization.go +++ b/github/data_source_github_organization.go @@ -50,6 +50,7 @@ func dataSourceGithubOrganization() *schema.Resource { Elem: &schema.Schema{ Type: schema.TypeString, }, + Deprecated: "Use `github_organization_repositories` data source instead. Expect this field to be removed in next major version.", }, "members": { Type: schema.TypeList, diff --git a/github/data_source_github_organization_repositories.go b/github/data_source_github_organization_repositories.go index 4a3fac2914..6ea99bd36d 100644 --- a/github/data_source_github_organization_repositories.go +++ b/github/data_source_github_organization_repositories.go @@ -1,14 +1,22 @@ package github import ( - "github.com/google/go-github/v66/github" + "context" + + "github.com/google/go-github/v83/github" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func dataSourceGithubOrganizationRepositories() *schema.Resource { return &schema.Resource{ - Read: dataSourceGithubOrganizationRepositoriesRead, + ReadContext: dataSourceGithubOrganizationRepositoriesRead, Schema: map[string]*schema.Schema{ + "ignore_archived_repositories": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, "repositories": { Type: schema.TypeList, Computed: true, @@ -41,21 +49,24 @@ func dataSourceGithubOrganizationRepositories() *schema.Resource { } } -func dataSourceGithubOrganizationRepositoriesRead(d *schema.ResourceData, meta interface{}) error { +func dataSourceGithubOrganizationRepositoriesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { org := meta.(*Owner).name client3 := meta.(*Owner).v3client - ctx := meta.(*Owner).StopContext options := github.RepositoryListByOrgOptions{ ListOptions: github.ListOptions{PerPage: 100}, } + ignoreArchived := d.Get("ignore_archived_repositories").(bool) var allRepositories []map[string]interface{} for { repositories, resp, err := client3.Repositories.ListByOrg(ctx, org, &options) if err != nil { - return err + return diag.FromErr(err) } for _, repository := range repositories { + if ignoreArchived && repository.GetArchived() { + continue + } repo := make(map[string]interface{}) repo["repo_id"] = repository.GetID() repo["node_id"] = repository.GetNodeID() diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go index 9ca7aee9a5..844386466a 100644 --- a/github/data_source_github_organization_repositories_test.go +++ b/github/data_source_github_organization_repositories_test.go @@ -1,58 +1,67 @@ package github import ( + "fmt" "testing" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { t.Run("manages repositories", func(t *testing.T) { - config := ` + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repo1Name := fmt.Sprintf("%srepo-%s-1", testResourcePrefix, randomID) + repo2Name := fmt.Sprintf("%srepo-%s-2", testResourcePrefix, randomID) + + config := fmt.Sprintf(` resource "github_repository" "test1" { - name = "test1" + name = "%s" visibility = "private" } resource "github_repository" "test2" { - name = "test2" + name = "%s" archived = true visibility = "public" depends_on = [github_repository.test1] } - ` + `, repo1Name, repo2Name) - config2 := config + ` + configAll := config + ` data "github_organization_repositories" "all" {} ` - const resourceName = "data.github_organization_repositories.all" - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "webhooks.#", "2"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.name", "test1"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.archived", "false"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.visibility", "private"), - resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.repo_id"), - resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.node_id"), - resource.TestCheckResourceAttr(resourceName, "webhooks.1.name", "test2"), - resource.TestCheckResourceAttr(resourceName, "webhooks.1.archived", "true"), - resource.TestCheckResourceAttr(resourceName, "webhooks.1.visibility", "public"), - resource.TestCheckResourceAttrSet(resourceName, "webhooks.1.repo_id"), - resource.TestCheckResourceAttrSet(resourceName, "webhooks.1.node_id"), - ) + configSkipArchived := config + ` + data "github_organization_repositories" "skip_archived" { + ignore_archived_repositories = true + depends_on = [github_repository.test2] + } + ` + + const resourceAll = "data.github_organization_repositories.all" + const resourceSkipArchived = "data.github_organization_repositories.skip_archived" - testCase := func(t *testing.T, mode string) { + testCase := func(t *testing.T, mode testMode) { resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, + PreCheck: func() { skipUnlessMode(t, mode) }, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, Check: resource.ComposeTestCheckFunc(), }, { - Config: config2, - Check: check, + Config: configAll, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), + ), + }, + { + Config: configSkipArchived, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), + ), }, }, }) From 20cf0308e62c8d55845890cc49168d80b4b0cad8 Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Sun, 1 Mar 2026 16:38:47 +0100 Subject: [PATCH 4/8] Review --- ...e_github_organization_repositories_test.go | 75 ++++++++++--------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go index 844386466a..a9e54c72ec 100644 --- a/github/data_source_github_organization_repositories_test.go +++ b/github/data_source_github_organization_repositories_test.go @@ -14,7 +14,20 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { repo1Name := fmt.Sprintf("%srepo-%s-1", testResourcePrefix, randomID) repo2Name := fmt.Sprintf("%srepo-%s-2", testResourcePrefix, randomID) - config := fmt.Sprintf(` + config1 := fmt.Sprintf(` + resource "github_repository" "test1" { + name = "%s" + visibility = "private" + } + + resource "github_repository" "test2" { + name = "%s" + visibility = "public" + depends_on = [github_repository.test1] + } + `, repo1Name, repo2Name) + + config2 := fmt.Sprintf(` resource "github_repository" "test1" { name = "%s" visibility = "private" @@ -28,11 +41,11 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { } `, repo1Name, repo2Name) - configAll := config + ` + configAll := config2 + ` data "github_organization_repositories" "all" {} ` - configSkipArchived := config + ` + configSkipArchived := config2 + ` data "github_organization_repositories" "skip_archived" { ignore_archived_repositories = true depends_on = [github_repository.test2] @@ -42,41 +55,29 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { const resourceAll = "data.github_organization_repositories.all" const resourceSkipArchived = "data.github_organization_repositories.skip_archived" - testCase := func(t *testing.T, mode testMode) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - ProviderFactories: providerFactories, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: configAll, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), - ), - }, - { - Config: configSkipArchived, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config1, }, - }) - } - - 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) + { + Config: config2, + }, + { + Config: configAll, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), + ), + }, + { + Config: configSkipArchived, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), + ), + }, + }, }) }) } From 4df0ecb6a963f7287d400a539bc9cea24b3c7d9c Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Sun, 1 Mar 2026 23:01:42 +0100 Subject: [PATCH 5/8] Reduce duplication --- ...e_github_organization_repositories_test.go | 39 +++++-------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go index a9e54c72ec..532750cff8 100644 --- a/github/data_source_github_organization_repositories_test.go +++ b/github/data_source_github_organization_repositories_test.go @@ -14,7 +14,7 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { repo1Name := fmt.Sprintf("%srepo-%s-1", testResourcePrefix, randomID) repo2Name := fmt.Sprintf("%srepo-%s-2", testResourcePrefix, randomID) - config1 := fmt.Sprintf(` + config := ` resource "github_repository" "test1" { name = "%s" visibility = "private" @@ -23,35 +23,16 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { resource "github_repository" "test2" { name = "%s" visibility = "public" + archived = %t depends_on = [github_repository.test1] } - `, repo1Name, repo2Name) - - config2 := fmt.Sprintf(` - resource "github_repository" "test1" { - name = "%s" - visibility = "private" - } - - resource "github_repository" "test2" { - name = "%s" - archived = true - visibility = "public" - depends_on = [github_repository.test1] - } - `, repo1Name, repo2Name) - - configAll := config2 + ` - data "github_organization_repositories" "all" {} ` - - configSkipArchived := config2 + ` - data "github_organization_repositories" "skip_archived" { - ignore_archived_repositories = true - depends_on = [github_repository.test2] + configWithDS := config + ` + data "github_organization_repositories" "all" { + ignore_archived_repositories = %t + depends_on = [github_repository.test2] } ` - const resourceAll = "data.github_organization_repositories.all" const resourceSkipArchived = "data.github_organization_repositories.skip_archived" @@ -60,19 +41,19 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: config1, + Config: fmt.Sprintf(config, repo1Name, repo2Name, false), }, { - Config: config2, + Config: fmt.Sprintf(config, repo1Name, repo2Name, true), }, { - Config: configAll, + Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, false), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), ), }, { - Config: configSkipArchived, + Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, true), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), ), From d20ada8bbf14e3aeec6d06a239fd6fb419845ed3 Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Sun, 1 Mar 2026 23:12:29 +0100 Subject: [PATCH 6/8] Update github/data_source_github_organization_repositories_test.go Co-authored-by: Timo Sand --- github/data_source_github_organization_repositories_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go index 532750cff8..db26b60e3a 100644 --- a/github/data_source_github_organization_repositories_test.go +++ b/github/data_source_github_organization_repositories_test.go @@ -47,7 +47,7 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { Config: fmt.Sprintf(config, repo1Name, repo2Name, true), }, { - Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, false), + Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, true, false), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), ), From 7f68ece248223dd62f81a100413347400168a784 Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Sun, 1 Mar 2026 23:12:34 +0100 Subject: [PATCH 7/8] Update github/data_source_github_organization_repositories_test.go Co-authored-by: Timo Sand --- github/data_source_github_organization_repositories_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/data_source_github_organization_repositories_test.go b/github/data_source_github_organization_repositories_test.go index db26b60e3a..d4ebe0056f 100644 --- a/github/data_source_github_organization_repositories_test.go +++ b/github/data_source_github_organization_repositories_test.go @@ -53,7 +53,7 @@ func TestAccGithubOrganizationRepositoriesDataSource(t *testing.T) { ), }, { - Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, true), + Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, true, true), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), ), From 7516055fd2503175ee061bf683a4cf04b3dc13c6 Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Tue, 3 Mar 2026 11:19:02 +0100 Subject: [PATCH 8/8] Review --- ...source_github_organization_repositories.go | 39 +++++++++---------- .../d/organization_repositories.html.markdown | 2 +- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/github/data_source_github_organization_repositories.go b/github/data_source_github_organization_repositories.go index 6ea99bd36d..0eb777b49f 100644 --- a/github/data_source_github_organization_repositories.go +++ b/github/data_source_github_organization_repositories.go @@ -22,7 +22,7 @@ func dataSourceGithubOrganizationRepositories() *schema.Resource { Computed: true, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "repo_id": { + "id": { Type: schema.TypeInt, Computed: true, }, @@ -49,36 +49,33 @@ func dataSourceGithubOrganizationRepositories() *schema.Resource { } } -func dataSourceGithubOrganizationRepositoriesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - org := meta.(*Owner).name - client3 := meta.(*Owner).v3client +func dataSourceGithubOrganizationRepositoriesRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { + meta := m.(*Owner) + client := meta.v3client + org := meta.name options := github.RepositoryListByOrgOptions{ ListOptions: github.ListOptions{PerPage: 100}, } ignoreArchived := d.Get("ignore_archived_repositories").(bool) - var allRepositories []map[string]interface{} - for { - repositories, resp, err := client3.Repositories.ListByOrg(ctx, org, &options) + var allRepositories []map[string]any + iter := client.Repositories.ListByOrgIter(ctx, org, &options) + for repository, err := range iter { if err != nil { return diag.FromErr(err) } - for _, repository := range repositories { - if ignoreArchived && repository.GetArchived() { - continue - } - repo := make(map[string]interface{}) - repo["repo_id"] = repository.GetID() - repo["node_id"] = repository.GetNodeID() - repo["name"] = repository.GetName() - repo["archived"] = repository.GetArchived() - repo["visibility"] = repository.GetVisibility() - allRepositories = append(allRepositories, repo) + archived := repository.GetArchived() + if ignoreArchived && archived { + continue } - if resp.NextPage == 0 { - break + repo := map[string]any{ + "id": repository.GetID(), + "node_id": repository.GetNodeID(), + "name": repository.GetName(), + "archived": archived, + "visibility": repository.GetVisibility(), } - options.Page = resp.NextPage + allRepositories = append(allRepositories, repo) } d.SetId(org) diff --git a/website/docs/d/organization_repositories.html.markdown b/website/docs/d/organization_repositories.html.markdown index 793b3a90e9..0313c9c484 100644 --- a/website/docs/d/organization_repositories.html.markdown +++ b/website/docs/d/organization_repositories.html.markdown @@ -24,7 +24,7 @@ ___ The `repository` block consists of: - * `repo_id` - GitHub ID for the repository. + * `id` - GitHub ID for the repository. * `node_id` - The Node ID of the repository. * `name` - The name of the repository. * `archived` - Whether the repository is archived.