-
Notifications
You must be signed in to change notification settings - Fork 954
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
Open
borchero
wants to merge
12
commits into
integrations:main
Choose a base branch
from
borchero:github-organization-repositories
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
55c08ef
feat: Add `github_organization_repositories` data source
borchero 83a5ab5
Adjust test
borchero 5750b84
Merge branch 'main' into github-organization-repositories
borchero 41ab82b
Merge branch 'main' into github-organization-repositories
borchero 7d5f5af
Review
borchero 20cf030
Review
borchero 4df0ecb
Reduce duplication
borchero d20ada8
Update github/data_source_github_organization_repositories_test.go
borchero 7f68ece
Update github/data_source_github_organization_repositories_test.go
borchero 7516055
Review
borchero af5cc7f
Merge branch 'main' into github-organization-repositories
borchero 7d15d1b
Merge branch 'main' into github-organization-repositories
borchero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
64 changes: 64 additions & 0 deletions
64
github/data_source_github_organization_repositories_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.#"), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
|
||
| * `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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.