Skip to content
Open
77 changes: 77 additions & 0 deletions github/data_source_github_organization_repositories.go
Original file line number Diff line number Diff line change
@@ -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,
Comment thread
borchero marked this conversation as resolved.
Outdated
Schema: map[string]*schema.Schema{
"repositories": {
Comment thread
borchero marked this conversation as resolved.
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"repo_id": {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"repo_id": {
"repository_id": {

This would be more consistent, although given that we're using name below maybe this should be id? WDYT @deiga?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use repo_id somewhere else as well.
But I agree that either we use repository_id or just id. The latter is harder to grep but cleaner to use 🤔

I think I'm leaning towards just id

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for 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{}
Comment thread
borchero marked this conversation as resolved.
Outdated
for {
repositories, resp, err := client3.Repositories.ListByOrg(ctx, org, &options)
Comment thread
borchero marked this conversation as resolved.
Outdated
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)
Comment thread
borchero marked this conversation as resolved.
Outdated
}
if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
}

d.SetId(org)
d.Set("repositories", allRepositories)

return nil
}
73 changes: 73 additions & 0 deletions github/data_source_github_organization_repositories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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"
visibility = "private"
}

resource "github_repository" "test2" {
name = "test2"
archived = true
visibility = "public"
depends_on = [github_repository.test1]
}
Comment thread
borchero marked this conversation as resolved.
`

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", "public"),
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)
})
})
}
23 changes: 12 additions & 11 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}

Expand Down
31 changes: 31 additions & 0 deletions website/docs/d/organization_repositories.html.markdown
Original file line number Diff line number Diff line change
@@ -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.