Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions github/data_source_github_organization_app_installations.go
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"
Copy link
Contributor

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?

Suggested change
"github.com/google/go-github/v66/github"
"github.com/google/go-github/v77/github"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGithubOrganizationAppInstallations() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubOrganizationAppInstallationsRead,
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Could you refactor this to use ReadContext?


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,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Could you add the following fields to this:

  • repository_selection
  • permissions
  • events
  • html_url
  • client_id
  • target_id
  • target_type
  • suspended

},
},
},
},
}
}
Comment on lines +11 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: Could you please add Description fields to the Schema and all fields?


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
}
43 changes: 43 additions & 0 deletions github/data_source_github_organization_app_installations_test.go
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)
})
})
}
1 change: 1 addition & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
29 changes: 29 additions & 0 deletions website/docs/d/organization_app_installations.html.markdown
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.