Skip to content

Commit f068507

Browse files
committed
chore: extract flattenInstallationPermissions helper and update test to use ProviderFactories pattern
Signed-off-by: atilsensalduz <[email protected]>
1 parent cec6858 commit f068507

2 files changed

Lines changed: 119 additions & 128 deletions

File tree

github/data_source_github_organization_app_installations.go

Lines changed: 104 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
func dataSourceGithubOrganizationAppInstallations() *schema.Resource {
1212
return &schema.Resource{
1313
ReadContext: dataSourceGithubOrganizationAppInstallationsRead,
14+
Description: "Use this data source to retrieve all GitHub App installations of the organization.",
1415

1516
Schema: map[string]*schema.Schema{
1617
"installations": {
@@ -89,7 +90,7 @@ func dataSourceGithubOrganizationAppInstallationsRead(ctx context.Context, d *sc
8990
client := meta.(*Owner).v3client
9091

9192
options := &github.ListOptions{
92-
PerPage: 100,
93+
PerPage: maxPerPage,
9394
}
9495

9596
results := make([]map[string]interface{}, 0)
@@ -134,110 +135,117 @@ func flattenGitHubAppInstallations(orgAppInstallations []*github.Installation) [
134135
result["client_id"] = appInstallation.GetClientID()
135136
result["target_id"] = appInstallation.GetTargetID()
136137
result["target_type"] = appInstallation.GetTargetType()
137-
result["suspended"] = appInstallation.GetSuspendedAt() != nil
138+
result["suspended"] = !appInstallation.GetSuspendedAt().IsZero()
138139
if appInstallation.Events != nil {
139140
result["events"] = appInstallation.Events
140141
} else {
141142
result["events"] = []string{}
142143
}
143144

144-
permissions := make(map[string]string)
145-
if appInstallation.Permissions != nil {
146-
if v := appInstallation.Permissions.Actions; v != nil {
147-
permissions["actions"] = *v
148-
}
149-
if v := appInstallation.Permissions.Administration; v != nil {
150-
permissions["administration"] = *v
151-
}
152-
if v := appInstallation.Permissions.Checks; v != nil {
153-
permissions["checks"] = *v
154-
}
155-
if v := appInstallation.Permissions.Contents; v != nil {
156-
permissions["contents"] = *v
157-
}
158-
if v := appInstallation.Permissions.Deployments; v != nil {
159-
permissions["deployments"] = *v
160-
}
161-
if v := appInstallation.Permissions.Environments; v != nil {
162-
permissions["environments"] = *v
163-
}
164-
if v := appInstallation.Permissions.Issues; v != nil {
165-
permissions["issues"] = *v
166-
}
167-
if v := appInstallation.Permissions.Metadata; v != nil {
168-
permissions["metadata"] = *v
169-
}
170-
if v := appInstallation.Permissions.Members; v != nil {
171-
permissions["members"] = *v
172-
}
173-
if v := appInstallation.Permissions.OrganizationAdministration; v != nil {
174-
permissions["organization_administration"] = *v
175-
}
176-
if v := appInstallation.Permissions.OrganizationHooks; v != nil {
177-
permissions["organization_hooks"] = *v
178-
}
179-
if v := appInstallation.Permissions.OrganizationPlan; v != nil {
180-
permissions["organization_plan"] = *v
181-
}
182-
if v := appInstallation.Permissions.OrganizationProjects; v != nil {
183-
permissions["organization_projects"] = *v
184-
}
185-
if v := appInstallation.Permissions.OrganizationSecrets; v != nil {
186-
permissions["organization_secrets"] = *v
187-
}
188-
if v := appInstallation.Permissions.OrganizationSelfHostedRunners; v != nil {
189-
permissions["organization_self_hosted_runners"] = *v
190-
}
191-
if v := appInstallation.Permissions.OrganizationUserBlocking; v != nil {
192-
permissions["organization_user_blocking"] = *v
193-
}
194-
if v := appInstallation.Permissions.Packages; v != nil {
195-
permissions["packages"] = *v
196-
}
197-
if v := appInstallation.Permissions.Pages; v != nil {
198-
permissions["pages"] = *v
199-
}
200-
if v := appInstallation.Permissions.PullRequests; v != nil {
201-
permissions["pull_requests"] = *v
202-
}
203-
if v := appInstallation.Permissions.RepositoryHooks; v != nil {
204-
permissions["repository_hooks"] = *v
205-
}
206-
if v := appInstallation.Permissions.RepositoryProjects; v != nil {
207-
permissions["repository_projects"] = *v
208-
}
209-
if v := appInstallation.Permissions.RepositoryPreReceiveHooks; v != nil {
210-
permissions["repository_pre_receive_hooks"] = *v
211-
}
212-
if v := appInstallation.Permissions.Secrets; v != nil {
213-
permissions["secrets"] = *v
214-
}
215-
if v := appInstallation.Permissions.SecretScanningAlerts; v != nil {
216-
permissions["secret_scanning_alerts"] = *v
217-
}
218-
if v := appInstallation.Permissions.SecurityEvents; v != nil {
219-
permissions["security_events"] = *v
220-
}
221-
if v := appInstallation.Permissions.SingleFile; v != nil {
222-
permissions["single_file"] = *v
223-
}
224-
if v := appInstallation.Permissions.Statuses; v != nil {
225-
permissions["statuses"] = *v
226-
}
227-
if v := appInstallation.Permissions.TeamDiscussions; v != nil {
228-
permissions["team_discussions"] = *v
229-
}
230-
if v := appInstallation.Permissions.VulnerabilityAlerts; v != nil {
231-
permissions["vulnerability_alerts"] = *v
232-
}
233-
if v := appInstallation.Permissions.Workflows; v != nil {
234-
permissions["workflows"] = *v
235-
}
236-
}
237-
result["permissions"] = permissions
145+
result["permissions"] = flattenInstallationPermissions(appInstallation.Permissions)
238146

239147
results = append(results, result)
240148
}
241149

242150
return results
243151
}
152+
153+
func flattenInstallationPermissions(perms *github.InstallationPermissions) map[string]string {
154+
permissions := make(map[string]string)
155+
if perms == nil {
156+
return permissions
157+
}
158+
159+
if v := perms.GetActions(); v != "" {
160+
permissions["actions"] = v
161+
}
162+
if v := perms.GetAdministration(); v != "" {
163+
permissions["administration"] = v
164+
}
165+
if v := perms.GetChecks(); v != "" {
166+
permissions["checks"] = v
167+
}
168+
if v := perms.GetContents(); v != "" {
169+
permissions["contents"] = v
170+
}
171+
if v := perms.GetDeployments(); v != "" {
172+
permissions["deployments"] = v
173+
}
174+
if v := perms.GetEnvironments(); v != "" {
175+
permissions["environments"] = v
176+
}
177+
if v := perms.GetIssues(); v != "" {
178+
permissions["issues"] = v
179+
}
180+
if v := perms.GetMetadata(); v != "" {
181+
permissions["metadata"] = v
182+
}
183+
if v := perms.GetMembers(); v != "" {
184+
permissions["members"] = v
185+
}
186+
if v := perms.GetOrganizationAdministration(); v != "" {
187+
permissions["organization_administration"] = v
188+
}
189+
if v := perms.GetOrganizationHooks(); v != "" {
190+
permissions["organization_hooks"] = v
191+
}
192+
if v := perms.GetOrganizationPlan(); v != "" {
193+
permissions["organization_plan"] = v
194+
}
195+
if v := perms.GetOrganizationProjects(); v != "" {
196+
permissions["organization_projects"] = v
197+
}
198+
if v := perms.GetOrganizationSecrets(); v != "" {
199+
permissions["organization_secrets"] = v
200+
}
201+
if v := perms.GetOrganizationSelfHostedRunners(); v != "" {
202+
permissions["organization_self_hosted_runners"] = v
203+
}
204+
if v := perms.GetOrganizationUserBlocking(); v != "" {
205+
permissions["organization_user_blocking"] = v
206+
}
207+
if v := perms.GetPackages(); v != "" {
208+
permissions["packages"] = v
209+
}
210+
if v := perms.GetPages(); v != "" {
211+
permissions["pages"] = v
212+
}
213+
if v := perms.GetPullRequests(); v != "" {
214+
permissions["pull_requests"] = v
215+
}
216+
if v := perms.GetRepositoryHooks(); v != "" {
217+
permissions["repository_hooks"] = v
218+
}
219+
if v := perms.GetRepositoryProjects(); v != "" {
220+
permissions["repository_projects"] = v
221+
}
222+
if v := perms.GetRepositoryPreReceiveHooks(); v != "" {
223+
permissions["repository_pre_receive_hooks"] = v
224+
}
225+
if v := perms.GetSecrets(); v != "" {
226+
permissions["secrets"] = v
227+
}
228+
if v := perms.GetSecretScanningAlerts(); v != "" {
229+
permissions["secret_scanning_alerts"] = v
230+
}
231+
if v := perms.GetSecurityEvents(); v != "" {
232+
permissions["security_events"] = v
233+
}
234+
if v := perms.GetSingleFile(); v != "" {
235+
permissions["single_file"] = v
236+
}
237+
if v := perms.GetStatuses(); v != "" {
238+
permissions["statuses"] = v
239+
}
240+
if v := perms.GetTeamDiscussions(); v != "" {
241+
permissions["team_discussions"] = v
242+
}
243+
if v := perms.GetVulnerabilityAlerts(); v != "" {
244+
permissions["vulnerability_alerts"] = v
245+
}
246+
if v := perms.GetWorkflows(); v != "" {
247+
permissions["workflows"] = v
248+
}
249+
250+
return permissions
251+
}

github/data_source_github_organization_app_installations_test.go

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,20 @@ import (
77
)
88

99
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.id"),
15-
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.slug"),
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-
})
10+
config := `data "github_organization_app_installations" "test" {}`
11+
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { skipUnlessHasOrgs(t) },
14+
ProviderFactories: providerFactories,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: config,
18+
Check: resource.ComposeAggregateTestCheckFunc(
19+
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.id"),
20+
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.slug"),
21+
resource.TestCheckResourceAttrSet("data.github_organization_app_installations.test", "installations.0.app_id"),
22+
),
23+
},
24+
},
4225
})
4326
}

0 commit comments

Comments
 (0)