Skip to content

Commit 681de82

Browse files
committed
feat: add data source to list GitHub App installations in an organization
This commit introduces a new data source, 'github_organization_app_installations', to enable listing all installed GitHub Apps within an organization. References: - API Documentation: https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-app-installations-for-an-organization - Related Issue: #2570 Signed-off-by: atilsensalduz <[email protected]>
1 parent 1ca7092 commit 681de82

3 files changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/go-github/v66/github"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceGithubOrganizationAppInstallations() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubOrganizationAppInstallationsRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"installations": {
16+
Type: schema.TypeList,
17+
Computed: true,
18+
Elem: &schema.Resource{
19+
Schema: map[string]*schema.Schema{
20+
"slug": {
21+
Type: schema.TypeString,
22+
Computed: true,
23+
},
24+
"node_id": {
25+
Type: schema.TypeInt,
26+
Computed: true,
27+
},
28+
"app_id": {
29+
Type: schema.TypeInt,
30+
Computed: true,
31+
},
32+
},
33+
},
34+
},
35+
},
36+
}
37+
}
38+
39+
func dataSourceGithubOrganizationAppInstallationsRead(d *schema.ResourceData, meta interface{}) error {
40+
owner := meta.(*Owner).name
41+
42+
client := meta.(*Owner).v3client
43+
ctx := context.Background()
44+
45+
options := &github.ListOptions{
46+
PerPage: 100,
47+
}
48+
49+
results := make([]map[string]interface{}, 0)
50+
for {
51+
appInstallations, resp, err := client.Organizations.ListInstallations(ctx, owner, options)
52+
if err != nil {
53+
return err
54+
}
55+
56+
results = append(results, flattenGitHubAppInstallations(appInstallations.Installations)...)
57+
if resp.NextPage == 0 {
58+
break
59+
}
60+
61+
options.Page = resp.NextPage
62+
}
63+
64+
d.SetId(owner)
65+
err := d.Set("installations", results)
66+
if err != nil {
67+
return err
68+
}
69+
70+
return nil
71+
}
72+
73+
func flattenGitHubAppInstallations(orgAppInstallations []*github.Installation) []map[string]interface{} {
74+
results := make([]map[string]interface{}, 0)
75+
76+
if orgAppInstallations == nil {
77+
return results
78+
}
79+
80+
for _, appInstallation := range orgAppInstallations {
81+
result := make(map[string]interface{})
82+
83+
result["slug"] = appInstallation.AppSlug
84+
result["node_id"] = appInstallation.NodeID
85+
result["app_id"] = appInstallation.AppID
86+
87+
results = append(results, result)
88+
}
89+
90+
return results
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccGithubOrganizationAppInstallations(t *testing.T) {
10+
t.Run("queries without error", func(t *testing.T) {
11+
config := `data "github_organization_app_installations" "test" {}`
12+
13+
check := resource.ComposeAggregateTestCheckFunc(
14+
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.slug"),
15+
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.node_id"),
16+
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.app_id"),
17+
)
18+
19+
testCase := func(t *testing.T, mode string) {
20+
resource.Test(t, resource.TestCase{
21+
Providers: testAccProviders,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: config,
25+
Check: check,
26+
},
27+
},
28+
})
29+
}
30+
31+
t.Run("with an anonymous account", func(t *testing.T) {
32+
t.Skip("anonymous account not supported for this operation")
33+
})
34+
35+
t.Run("with an individual account", func(t *testing.T) {
36+
t.Skip("individual account not supported for this operation")
37+
})
38+
39+
t.Run("with an organization account", func(t *testing.T) {
40+
testCase(t, organization)
41+
})
42+
})
43+
}

github/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ func Provider() *schema.Provider {
236236
"github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(),
237237
"github_organization_teams": dataSourceGithubOrganizationTeams(),
238238
"github_organization_webhooks": dataSourceGithubOrganizationWebhooks(),
239+
"github_organization_app_installations": dataSourceGithubOrganizationAppInstallations(),
239240
"github_ref": dataSourceGithubRef(),
240241
"github_release": dataSourceGithubRelease(),
241242
"github_repositories": dataSourceGithubRepositories(),

0 commit comments

Comments
 (0)