-
Notifications
You must be signed in to change notification settings - Fork 961
GitHub organization repositories #3243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
55c08ef
83a5ab5
5750b84
41ab82b
7d5f5af
20cf030
4df0ecb
d20ada8
7f68ece
7516055
af5cc7f
7d15d1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| 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{ | ||||||
| "repo_id": { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This would be more consistent, although given that we're using
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we use repo_id somewhere else as well. I think I'm leaning towards just id
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went for |
||||||
| 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, meta any) diag.Diagnostics { | ||||||
| org := meta.(*Owner).name | ||||||
| client3 := meta.(*Owner).v3client | ||||||
|
borchero marked this conversation as resolved.
Outdated
|
||||||
|
|
||||||
| options := github.RepositoryListByOrgOptions{ | ||||||
| ListOptions: github.ListOptions{PerPage: 100}, | ||||||
| } | ||||||
| ignoreArchived := d.Get("ignore_archived_repositories").(bool) | ||||||
| var allRepositories []map[string]interface{} | ||||||
|
borchero marked this conversation as resolved.
Outdated
|
||||||
| for { | ||||||
| repositories, resp, err := client3.Repositories.ListByOrg(ctx, org, &options) | ||||||
|
borchero marked this conversation as resolved.
Outdated
|
||||||
| if err != nil { | ||||||
| return diag.FromErr(err) | ||||||
| } | ||||||
| for _, repository := range repositories { | ||||||
| if ignoreArchived && repository.GetArchived() { | ||||||
| continue | ||||||
| } | ||||||
|
borchero marked this conversation as resolved.
Outdated
|
||||||
| 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) | ||||||
|
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 | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| 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) | ||
|
|
||
| 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" | ||
| } | ||
|
|
||
| resource "github_repository" "test2" { | ||
| name = "%s" | ||
| archived = true | ||
| visibility = "public" | ||
| depends_on = [github_repository.test1] | ||
| } | ||
|
borchero marked this conversation as resolved.
|
||
| `, 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] | ||
| } | ||
| ` | ||
|
borchero marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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: config1, | ||
| }, | ||
| { | ||
| Config: config2, | ||
| }, | ||
| { | ||
| Config: configAll, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttrSet(resourceAll, "repositories.#"), | ||
| ), | ||
| }, | ||
| { | ||
| Config: configSkipArchived, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttrSet(resourceSkipArchived, "repositories.#"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
| } | ||
| 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. |
Uh oh!
There was an error while loading. Please reload this page.