Skip to content
Merged
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
16 changes: 8 additions & 8 deletions github/data_source_github_organization_repository_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"strconv"

"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

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

Read: dataSourceGithubOrganizationRepositoryRoleRead,
ReadContext: dataSourceGithubOrganizationRepositoryRoleRead,

Schema: map[string]*schema.Schema{
"role_id": {
Expand Down Expand Up @@ -46,22 +47,21 @@ func dataSourceGithubOrganizationRepositoryRole() *schema.Resource {
}
}

func dataSourceGithubOrganizationRepositoryRoleRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubOrganizationRepositoryRoleRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

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

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

roles, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName)
if err != nil {
return err
return diag.FromErr(err)
}

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

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

for k, v := range r {
if err := d.Set(k, v); err != nil {
return err
return diag.FromErr(err)
}
}

Expand Down
10 changes: 5 additions & 5 deletions github/data_source_github_organization_repository_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"fmt"

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

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

Read: dataSourceGithubOrganizationRepositoryRolesRead,
ReadContext: dataSourceGithubOrganizationRepositoryRolesRead,

Schema: map[string]*schema.Schema{
"roles": {
Expand Down Expand Up @@ -53,14 +54,13 @@ func dataSourceGithubOrganizationRepositoryRoles() *schema.Resource {
}
}

func dataSourceGithubOrganizationRepositoryRolesRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubOrganizationRepositoryRolesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

ret, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName)
if err != nil {
return err
return diag.FromErr(err)
}

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

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

return nil
Expand Down
10 changes: 5 additions & 5 deletions github/data_source_github_organization_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"strconv"

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

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

Read: dataSourceGithubOrganizationRoleRead,
ReadContext: dataSourceGithubOrganizationRoleRead,

Schema: map[string]*schema.Schema{
"role_id": {
Expand Down Expand Up @@ -49,16 +50,15 @@ func dataSourceGithubOrganizationRole() *schema.Resource {
}
}

func dataSourceGithubOrganizationRoleRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubOrganizationRoleRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

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

role, _, err := client.Organizations.GetOrgRole(ctx, orgName, roleId)
if err != nil {
return err
return diag.FromErr(err)
}

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

for k, v := range r {
if err := d.Set(k, v); err != nil {
return err
return diag.FromErr(err)
}
}

Expand Down
12 changes: 6 additions & 6 deletions github/data_source_github_organization_role_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"

"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

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

Read: dataSourceGithubOrganizationRoleTeamsRead,
ReadContext: dataSourceGithubOrganizationRoleTeamsRead,

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

func dataSourceGithubOrganizationRoleTeamsRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubOrganizationRoleTeamsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

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

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

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

return nil
Expand Down
11 changes: 6 additions & 5 deletions github/data_source_github_organization_role_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"fmt"

"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

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

Read: dataSourceGithubOrganizationRoleUsersRead,
ReadContext: dataSourceGithubOrganizationRoleUsersRead,

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

func dataSourceGithubOrganizationRoleUsersRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubOrganizationRoleUsersRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

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

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

d.SetId(fmt.Sprintf("%d", roleId))
if err := d.Set("users", allUsers); err != nil {
return fmt.Errorf("error setting users: %w", err)
return diag.FromErr(fmt.Errorf("error setting users: %w", err))
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions github/data_source_github_organization_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"fmt"

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

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

Read: dataSourceGithubOrganizationRolesRead,
ReadContext: dataSourceGithubOrganizationRolesRead,

Schema: map[string]*schema.Schema{
"roles": {
Expand Down Expand Up @@ -58,14 +59,13 @@ func dataSourceGithubOrganizationRoles() *schema.Resource {
}
}

func dataSourceGithubOrganizationRolesRead(d *schema.ResourceData, meta any) error {
func dataSourceGithubOrganizationRolesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
ctx := context.Background()
orgName := meta.(*Owner).name

ret, _, err := client.Organizations.ListRoles(ctx, orgName)
if err != nil {
return err
return diag.FromErr(err)
}

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

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

return nil
Expand Down
Loading