-
Notifications
You must be signed in to change notification settings - Fork 906
feat: add data sources for listing GitHub App installations in an organization #2573
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 all commits
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,91 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/google/go-github/v66/github" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceGithubOrganizationAppInstallations() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Read: dataSourceGithubOrganizationAppInstallationsRead, | ||
|
Contributor
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. suggestion: Could you refactor this to use |
||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "installations": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
| "slug": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "app_id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| }, | ||
|
Contributor
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. suggestion: Could you add the following fields to this:
|
||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
Comment on lines
+11
to
+37
Contributor
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. issue: Could you please add |
||
|
|
||
| func dataSourceGithubOrganizationAppInstallationsRead(d *schema.ResourceData, meta interface{}) error { | ||
| owner := meta.(*Owner).name | ||
|
|
||
| client := meta.(*Owner).v3client | ||
| ctx := context.Background() | ||
|
|
||
| options := &github.ListOptions{ | ||
| PerPage: 100, | ||
| } | ||
|
|
||
| results := make([]map[string]interface{}, 0) | ||
| for { | ||
| appInstallations, resp, err := client.Organizations.ListInstallations(ctx, owner, options) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| results = append(results, flattenGitHubAppInstallations(appInstallations.Installations)...) | ||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
|
|
||
| options.Page = resp.NextPage | ||
| } | ||
|
|
||
| d.SetId(owner) | ||
| err := d.Set("installations", results) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func flattenGitHubAppInstallations(orgAppInstallations []*github.Installation) []map[string]interface{} { | ||
| results := make([]map[string]interface{}, 0) | ||
|
|
||
| if orgAppInstallations == nil { | ||
| return results | ||
| } | ||
|
|
||
| for _, appInstallation := range orgAppInstallations { | ||
| result := make(map[string]interface{}) | ||
|
|
||
| result["id"] = appInstallation.ID | ||
| result["slug"] = appInstallation.AppSlug | ||
| result["app_id"] = appInstallation.AppID | ||
|
|
||
| results = append(results, result) | ||
| } | ||
|
|
||
| return results | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccGithubOrganizationAppInstallations(t *testing.T) { | ||
| t.Run("queries without error", func(t *testing.T) { | ||
| config := `data "github_organization_app_installations" "test" {}` | ||
|
|
||
| check := resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.id"), | ||
| resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.slug"), | ||
| resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.app_id"), | ||
| ) | ||
|
|
||
| testCase := func(t *testing.T, mode string) { | ||
| resource.Test(t, resource.TestCase{ | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| 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) { | ||
| t.Skip("individual account not supported for this operation") | ||
| }) | ||
|
|
||
| t.Run("with an organization account", func(t *testing.T) { | ||
| testCase(t, organization) | ||
| }) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| --- | ||
| layout: "github" | ||
| page_title: "GitHub: github_organization_app_installations" | ||
| description: |- | ||
| Get information on all GitHub App installations of the organization. | ||
| --- | ||
|
|
||
| # github\_organization\_app_installations | ||
|
|
||
| Use this data source to retrieve all GitHub App installations of the organization. | ||
|
|
||
| ## Example Usage | ||
|
|
||
| To retrieve *all* GitHub App installations of the organization: | ||
|
|
||
| ```hcl | ||
| data "github_organization_app_installations" "all" {} | ||
| ``` | ||
|
|
||
| ## Attributes Reference | ||
|
|
||
| * `installations` - An Array of GitHub App installations. Each `installation` block consists of the fields documented below. | ||
| ___ | ||
|
|
||
| The `installation` block consists of: | ||
|
|
||
| * `id` - The GitHub app installation id. | ||
| * `slug` - The URL-friendly name of your GitHub App. | ||
| * `app_id` - This is the ID of the GitHub App. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I doubt we'll be able to land this before #2602
Maybe you could even rebase on top of that branch?