diff --git a/github/data_source_github_organization_app_installations.go b/github/data_source_github_organization_app_installations.go new file mode 100644 index 0000000000..d343a0c00c --- /dev/null +++ b/github/data_source_github_organization_app_installations.go @@ -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, + + 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, + }, + }, + }, + }, + }, + } +} + +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 +} diff --git a/github/data_source_github_organization_app_installations_test.go b/github/data_source_github_organization_app_installations_test.go new file mode 100644 index 0000000000..40a27672fa --- /dev/null +++ b/github/data_source_github_organization_app_installations_test.go @@ -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) + }) + }) +} diff --git a/github/provider.go b/github/provider.go index 8f44c95098..5a9ebe04cf 100644 --- a/github/provider.go +++ b/github/provider.go @@ -236,6 +236,7 @@ func Provider() *schema.Provider { "github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(), "github_organization_teams": dataSourceGithubOrganizationTeams(), "github_organization_webhooks": dataSourceGithubOrganizationWebhooks(), + "github_organization_app_installations": dataSourceGithubOrganizationAppInstallations(), "github_ref": dataSourceGithubRef(), "github_release": dataSourceGithubRelease(), "github_repositories": dataSourceGithubRepositories(), diff --git a/website/docs/d/organization_app_installations.html.markdown b/website/docs/d/organization_app_installations.html.markdown new file mode 100644 index 0000000000..efeb7d1de9 --- /dev/null +++ b/website/docs/d/organization_app_installations.html.markdown @@ -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.