Skip to content

Commit 681609c

Browse files
authored
fix: Fix org roles implementation (#2968)
Signed-off-by: Steve Hipwell <[email protected]>
1 parent 77035e6 commit 681609c

12 files changed

+257
-156
lines changed

github/data_source_github_organization_repository_role.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ import (
66
"strconv"
77

88
"github.com/google/go-github/v67/github"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
910
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1011
)
1112

1213
func dataSourceGithubOrganizationRepositoryRole() *schema.Resource {
1314
return &schema.Resource{
1415
Description: "Lookup a custom organization repository role.",
1516

16-
Read: dataSourceGithubOrganizationRepositoryRoleRead,
17+
ReadContext: dataSourceGithubOrganizationRepositoryRoleRead,
1718

1819
Schema: map[string]*schema.Schema{
1920
"role_id": {
@@ -46,22 +47,21 @@ func dataSourceGithubOrganizationRepositoryRole() *schema.Resource {
4647
}
4748
}
4849

49-
func dataSourceGithubOrganizationRepositoryRoleRead(d *schema.ResourceData, meta any) error {
50+
func dataSourceGithubOrganizationRepositoryRoleRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5051
client := meta.(*Owner).v3client
51-
ctx := context.Background()
5252
orgName := meta.(*Owner).name
5353

5454
roleId := int64(d.Get("role_id").(int))
5555

56-
// TODO: Use this code when go-github adds the functionality to get a custom repo role
56+
// TODO: Use this code when go-github is at v68+
5757
// role, _, err := client.Organizations.GetCustomRepoRole(ctx, orgName, roleId)
5858
// if err != nil {
59-
// return err
59+
// return diag.FromErr(err)
6060
// }
6161

6262
roles, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName)
6363
if err != nil {
64-
return err
64+
return diag.FromErr(err)
6565
}
6666

6767
var role *github.CustomRepoRoles
@@ -72,7 +72,7 @@ func dataSourceGithubOrganizationRepositoryRoleRead(d *schema.ResourceData, meta
7272
}
7373
}
7474
if role == nil {
75-
return fmt.Errorf("custom organization repo role with ID %d not found", roleId)
75+
return diag.FromErr(fmt.Errorf("custom organization repo role with ID %d not found", roleId))
7676
}
7777

7878
r := map[string]any{
@@ -87,7 +87,7 @@ func dataSourceGithubOrganizationRepositoryRoleRead(d *schema.ResourceData, meta
8787

8888
for k, v := range r {
8989
if err := d.Set(k, v); err != nil {
90-
return err
90+
return diag.FromErr(err)
9191
}
9292
}
9393

github/data_source_github_organization_repository_roles.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
func dataSourceGithubOrganizationRepositoryRoles() *schema.Resource {
1112
return &schema.Resource{
1213
Description: "Lookup all custom repository roles in an organization.",
1314

14-
Read: dataSourceGithubOrganizationRepositoryRolesRead,
15+
ReadContext: dataSourceGithubOrganizationRepositoryRolesRead,
1516

1617
Schema: map[string]*schema.Schema{
1718
"roles": {
@@ -53,14 +54,13 @@ func dataSourceGithubOrganizationRepositoryRoles() *schema.Resource {
5354
}
5455
}
5556

56-
func dataSourceGithubOrganizationRepositoryRolesRead(d *schema.ResourceData, meta any) error {
57+
func dataSourceGithubOrganizationRepositoryRolesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5758
client := meta.(*Owner).v3client
58-
ctx := context.Background()
5959
orgName := meta.(*Owner).name
6060

6161
ret, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName)
6262
if err != nil {
63-
return err
63+
return diag.FromErr(err)
6464
}
6565

6666
allRoles := make([]any, ret.GetTotalCount())
@@ -77,7 +77,7 @@ func dataSourceGithubOrganizationRepositoryRolesRead(d *schema.ResourceData, met
7777

7878
d.SetId(fmt.Sprintf("%s/github-org-repo-roles", orgName))
7979
if err := d.Set("roles", allRoles); err != nil {
80-
return fmt.Errorf("error setting roles: %w", err)
80+
return diag.FromErr(fmt.Errorf("error setting roles: %w", err))
8181
}
8282

8383
return nil

github/data_source_github_organization_role.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"context"
55
"strconv"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
func dataSourceGithubOrganizationRole() *schema.Resource {
1112
return &schema.Resource{
1213
Description: "Lookup a custom organization role.",
1314

14-
Read: dataSourceGithubOrganizationRoleRead,
15+
ReadContext: dataSourceGithubOrganizationRoleRead,
1516

1617
Schema: map[string]*schema.Schema{
1718
"role_id": {
@@ -49,16 +50,15 @@ func dataSourceGithubOrganizationRole() *schema.Resource {
4950
}
5051
}
5152

52-
func dataSourceGithubOrganizationRoleRead(d *schema.ResourceData, meta any) error {
53+
func dataSourceGithubOrganizationRoleRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5354
client := meta.(*Owner).v3client
54-
ctx := context.Background()
5555
orgName := meta.(*Owner).name
5656

5757
roleId := int64(d.Get("role_id").(int))
5858

5959
role, _, err := client.Organizations.GetOrgRole(ctx, orgName, roleId)
6060
if err != nil {
61-
return err
61+
return diag.FromErr(err)
6262
}
6363

6464
r := map[string]any{
@@ -74,7 +74,7 @@ func dataSourceGithubOrganizationRoleRead(d *schema.ResourceData, meta any) erro
7474

7575
for k, v := range r {
7676
if err := d.Set(k, v); err != nil {
77-
return err
77+
return diag.FromErr(err)
7878
}
7979
}
8080

github/data_source_github_organization_role_teams.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"fmt"
66

77
"github.com/google/go-github/v67/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

1112
func dataSourceGithubOrganizationRoleTeams() *schema.Resource {
1213
return &schema.Resource{
1314
Description: "Lookup all teams assigned to a custom organization role.",
1415

15-
Read: dataSourceGithubOrganizationRoleTeamsRead,
16+
ReadContext: dataSourceGithubOrganizationRoleTeamsRead,
1617

1718
Schema: map[string]*schema.Schema{
1819
"role_id": {
@@ -47,7 +48,7 @@ func dataSourceGithubOrganizationRoleTeams() *schema.Resource {
4748
Type: schema.TypeString,
4849
Computed: true,
4950
},
50-
// TODO: Add these fields when go-github adds the functionality to get a custom org
51+
// TODO: Add these fields when go-github is v68+
5152
// See https://github.com/google/go-github/issues/3364
5253
// "assignment": {
5354
// Description: "Determines if the team has a direct, indirect, or mixed relationship to a role.",
@@ -71,9 +72,8 @@ func dataSourceGithubOrganizationRoleTeams() *schema.Resource {
7172
}
7273
}
7374

74-
func dataSourceGithubOrganizationRoleTeamsRead(d *schema.ResourceData, meta any) error {
75+
func dataSourceGithubOrganizationRoleTeamsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
7576
client := meta.(*Owner).v3client
76-
ctx := context.Background()
7777
orgName := meta.(*Owner).name
7878

7979
roleId := int64(d.Get("role_id").(int))
@@ -87,7 +87,7 @@ func dataSourceGithubOrganizationRoleTeamsRead(d *schema.ResourceData, meta any)
8787
for {
8888
teams, resp, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, orgName, roleId, opts)
8989
if err != nil {
90-
return err
90+
return diag.FromErr(err)
9191
}
9292

9393
for _, team := range teams {
@@ -108,7 +108,7 @@ func dataSourceGithubOrganizationRoleTeamsRead(d *schema.ResourceData, meta any)
108108

109109
d.SetId(fmt.Sprintf("%d", roleId))
110110
if err := d.Set("teams", allTeams); err != nil {
111-
return fmt.Errorf("error setting teams: %w", err)
111+
return diag.FromErr(fmt.Errorf("error setting teams: %w", err))
112112
}
113113

114114
return nil

github/data_source_github_organization_role_users.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"fmt"
66

77
"github.com/google/go-github/v67/github"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
910
)
1011

1112
func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
1213
return &schema.Resource{
1314
Description: "Lookup all users assigned to a custom organization role.",
1415

15-
Read: dataSourceGithubOrganizationRoleUsersRead,
16+
ReadContext: dataSourceGithubOrganizationRoleUsersRead,
1617

1718
Schema: map[string]*schema.Schema{
1819
"role_id": {
@@ -37,6 +38,7 @@ func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
3738
Type: schema.TypeString,
3839
Computed: true,
3940
},
41+
// TODO: Add these fields when go-github is v68+
4042
// See https://github.com/google/go-github/issues/3364
4143
// "assignment": {
4244
// Description: "Determines if the team has a direct, indirect, or mixed relationship to a role.",
@@ -60,9 +62,8 @@ func dataSourceGithubOrganizationRoleUsers() *schema.Resource {
6062
}
6163
}
6264

63-
func dataSourceGithubOrganizationRoleUsersRead(d *schema.ResourceData, meta any) error {
65+
func dataSourceGithubOrganizationRoleUsersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6466
client := meta.(*Owner).v3client
65-
ctx := context.Background()
6667
orgName := meta.(*Owner).name
6768

6869
roleId := int64(d.Get("role_id").(int))
@@ -76,7 +77,7 @@ func dataSourceGithubOrganizationRoleUsersRead(d *schema.ResourceData, meta any)
7677
for {
7778
users, resp, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, orgName, roleId, opts)
7879
if err != nil {
79-
return err
80+
return diag.FromErr(err)
8081
}
8182

8283
for _, user := range users {
@@ -95,7 +96,7 @@ func dataSourceGithubOrganizationRoleUsersRead(d *schema.ResourceData, meta any)
9596

9697
d.SetId(fmt.Sprintf("%d", roleId))
9798
if err := d.Set("users", allUsers); err != nil {
98-
return fmt.Errorf("error setting users: %w", err)
99+
return diag.FromErr(fmt.Errorf("error setting users: %w", err))
99100
}
100101

101102
return nil

github/data_source_github_organization_roles.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
func dataSourceGithubOrganizationRoles() *schema.Resource {
1112
return &schema.Resource{
1213
Description: "Lookup all custom roles in an organization.",
1314

14-
Read: dataSourceGithubOrganizationRolesRead,
15+
ReadContext: dataSourceGithubOrganizationRolesRead,
1516

1617
Schema: map[string]*schema.Schema{
1718
"roles": {
@@ -58,14 +59,13 @@ func dataSourceGithubOrganizationRoles() *schema.Resource {
5859
}
5960
}
6061

61-
func dataSourceGithubOrganizationRolesRead(d *schema.ResourceData, meta any) error {
62+
func dataSourceGithubOrganizationRolesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6263
client := meta.(*Owner).v3client
63-
ctx := context.Background()
6464
orgName := meta.(*Owner).name
6565

6666
ret, _, err := client.Organizations.ListRoles(ctx, orgName)
6767
if err != nil {
68-
return err
68+
return diag.FromErr(err)
6969
}
7070

7171
allRoles := make([]any, ret.GetTotalCount())
@@ -83,7 +83,7 @@ func dataSourceGithubOrganizationRolesRead(d *schema.ResourceData, meta any) err
8383

8484
d.SetId(fmt.Sprintf("%s/github-org-roles", orgName))
8585
if err := d.Set("roles", allRoles); err != nil {
86-
return fmt.Errorf("error setting roles: %w", err)
86+
return diag.FromErr(fmt.Errorf("error setting roles: %w", err))
8787
}
8888

8989
return nil

0 commit comments

Comments
 (0)