diff --git a/github/data_source_github_organization.go b/github/data_source_github_organization.go index fc912843d3..7c454f94e1 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 new file mode 100644 index 0000000000..0eb777b49f --- /dev/null +++ b/github/data_source_github_organization_repositories.go @@ -0,0 +1,85 @@ +package github + +import ( + "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{ + ReadContext: dataSourceGithubOrganizationRepositoriesRead, + Schema: map[string]*schema.Schema{ + "ignore_archived_repositories": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, + "repositories": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "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(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]any + iter := client.Repositories.ListByOrgIter(ctx, org, &options) + for repository, err := range iter { + if err != nil { + return diag.FromErr(err) + } + archived := repository.GetArchived() + if ignoreArchived && archived { + continue + } + repo := map[string]any{ + "id": repository.GetID(), + "node_id": repository.GetNodeID(), + "name": repository.GetName(), + "archived": archived, + "visibility": repository.GetVisibility(), + } + allRepositories = append(allRepositories, repo) + } + + 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..d4ebe0056f --- /dev/null +++ b/github/data_source_github_organization_repositories_test.go @@ -0,0 +1,64 @@ +package github + +import ( + "fmt" + "testing" + + "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) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repo1Name := fmt.Sprintf("%srepo-%s-1", testResourcePrefix, randomID) + repo2Name := fmt.Sprintf("%srepo-%s-2", testResourcePrefix, randomID) + + config := ` + resource "github_repository" "test1" { + name = "%s" + visibility = "private" + } + + resource "github_repository" "test2" { + name = "%s" + visibility = "public" + archived = %t + depends_on = [github_repository.test1] + } + ` + 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" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: fmt.Sprintf(config, repo1Name, repo2Name, false), + }, + { + Config: fmt.Sprintf(config, repo1Name, repo2Name, true), + }, + { + Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, true, false), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), + ), + }, + { + Config: fmt.Sprintf(configWithDS, repo1Name, repo2Name, true, true), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), + ), + }, + }, + }) + }) +} diff --git a/github/provider.go b/github/provider.go index 2d5d6dc33a..0c28fb9d11 100644 --- a/github/provider.go +++ b/github/provider.go @@ -238,63 +238,64 @@ func Provider() *schema.Provider { "github_app_token": dataSourceGithubAppToken(), "github_branch": dataSourceGithubBranch(), "github_branch_protection_rules": dataSourceGithubBranchProtectionRules(), - "github_collaborators": dataSourceGithubCollaborators(), "github_codespaces_organization_public_key": dataSourceGithubCodespacesOrganizationPublicKey(), "github_codespaces_organization_secrets": dataSourceGithubCodespacesOrganizationSecrets(), "github_codespaces_public_key": dataSourceGithubCodespacesPublicKey(), "github_codespaces_secrets": dataSourceGithubCodespacesSecrets(), "github_codespaces_user_public_key": dataSourceGithubCodespacesUserPublicKey(), "github_codespaces_user_secrets": dataSourceGithubCodespacesUserSecrets(), + "github_collaborators": dataSourceGithubCollaborators(), "github_dependabot_organization_public_key": dataSourceGithubDependabotOrganizationPublicKey(), "github_dependabot_organization_secrets": dataSourceGithubDependabotOrganizationSecrets(), "github_dependabot_public_key": dataSourceGithubDependabotPublicKey(), "github_dependabot_secrets": dataSourceGithubDependabotSecrets(), + "github_enterprise": dataSourceGithubEnterprise(), "github_external_groups": dataSourceGithubExternalGroups(), "github_ip_ranges": dataSourceGithubIpRanges(), "github_issue_labels": dataSourceGithubIssueLabels(), "github_membership": dataSourceGithubMembership(), - "github_organization": dataSourceGithubOrganization(), - "github_organization_custom_role": dataSourceGithubOrganizationCustomRole(), + "github_organization_app_installations": dataSourceGithubOrganizationAppInstallations(), "github_organization_custom_properties": dataSourceGithubOrganizationCustomProperties(), + "github_organization_custom_role": dataSourceGithubOrganizationCustomRole(), "github_organization_external_identities": dataSourceGithubOrganizationExternalIdentities(), "github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(), + "github_organization_repositories": dataSourceGithubOrganizationRepositories(), "github_organization_repository_role": dataSourceGithubOrganizationRepositoryRole(), "github_organization_repository_roles": dataSourceGithubOrganizationRepositoryRoles(), - "github_organization_role": dataSourceGithubOrganizationRole(), "github_organization_role_teams": dataSourceGithubOrganizationRoleTeams(), "github_organization_role_users": dataSourceGithubOrganizationRoleUsers(), + "github_organization_role": dataSourceGithubOrganizationRole(), "github_organization_roles": dataSourceGithubOrganizationRoles(), "github_organization_security_managers": dataSourceGithubOrganizationSecurityManagers(), "github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(), "github_organization_teams": dataSourceGithubOrganizationTeams(), "github_organization_webhooks": dataSourceGithubOrganizationWebhooks(), - "github_organization_app_installations": dataSourceGithubOrganizationAppInstallations(), + "github_organization": dataSourceGithubOrganization(), "github_ref": dataSourceGithubRef(), - "github_release": dataSourceGithubRelease(), "github_release_asset": dataSourceGithubReleaseAsset(), + "github_release": dataSourceGithubRelease(), "github_repositories": dataSourceGithubRepositories(), - "github_repository": dataSourceGithubRepository(), "github_repository_autolink_references": dataSourceGithubRepositoryAutolinkReferences(), "github_repository_branches": dataSourceGithubRepositoryBranches(), "github_repository_custom_properties": dataSourceGithubRepositoryCustomProperties(), - "github_repository_environments": dataSourceGithubRepositoryEnvironments(), "github_repository_deploy_keys": dataSourceGithubRepositoryDeployKeys(), "github_repository_deployment_branch_policies": dataSourceGithubRepositoryDeploymentBranchPolicies(), + "github_repository_environment_deployment_policies": dataSourceGithubRepositoryEnvironmentDeploymentPolicies(), + "github_repository_environments": dataSourceGithubRepositoryEnvironments(), "github_repository_file": dataSourceGithubRepositoryFile(), "github_repository_milestone": dataSourceGithubRepositoryMilestone(), "github_repository_pull_request": dataSourceGithubRepositoryPullRequest(), "github_repository_pull_requests": dataSourceGithubRepositoryPullRequests(), "github_repository_teams": dataSourceGithubRepositoryTeams(), "github_repository_webhooks": dataSourceGithubRepositoryWebhooks(), + "github_repository": dataSourceGithubRepository(), "github_rest_api": dataSourceGithubRestApi(), "github_ssh_keys": dataSourceGithubSshKeys(), "github_team": dataSourceGithubTeam(), "github_tree": dataSourceGithubTree(), - "github_user": dataSourceGithubUser(), "github_user_external_identity": dataSourceGithubUserExternalIdentity(), + "github_user": dataSourceGithubUser(), "github_users": dataSourceGithubUsers(), - "github_enterprise": dataSourceGithubEnterprise(), - "github_repository_environment_deployment_policies": dataSourceGithubRepositoryEnvironmentDeploymentPolicies(), }, } diff --git a/website/docs/d/organization_repositories.html.markdown b/website/docs/d/organization_repositories.html.markdown new file mode 100644 index 0000000000..0313c9c484 --- /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: + + * `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.