-
Notifications
You must be signed in to change notification settings - Fork 741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add GitHub Organization Custom Role Resource and Data Source #1700
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fec51d7
feat: Add GitHub organization custom role resource
cailen abb0ddc
refactor: Update test and resource files for Github Organization Cust…
cailen 338e151
docs: Fix arguments for organization custom role
cailen cfcc271
docs: Update documentation with organization_custom_role
cailen fd068c5
docs: Fix errant parenthesis
cailen 8a895b6
Merge branch 'main' into add-custom-roles
kfcampbell 0c7cc0b
Merge branch 'main' into add-custom-roles
kfcampbell 8d857ae
Fix bad merge
kfcampbell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/go-github/v53/github" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func dataSourceGithubOrganizationCustomRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGithubOrganizationCustomRoleRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"base_role": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"permissions": { | ||
Type: schema.TypeSet, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGithubOrganizationCustomRoleRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
ctx := context.Background() | ||
orgName := meta.(*Owner).name | ||
|
||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// ListCustomRepoRoles returns a list of all custom repository roles for an organization. | ||
// There is an API endpoint for getting a single custom repository role, but is not | ||
// implemented in the go-github library. | ||
roleList, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName) | ||
if err != nil { | ||
return fmt.Errorf("error querying GitHub custom repository roles %s: %s", orgName, err) | ||
} | ||
|
||
var role *github.CustomRepoRoles | ||
for _, r := range roleList.CustomRepoRoles { | ||
if fmt.Sprint(*r.Name) == d.Get("name").(string) { | ||
role = r | ||
break | ||
} | ||
} | ||
|
||
if role == nil { | ||
log.Printf("[WARN] GitHub custom repository role (%s) not found.", d.Get("name").(string)) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.SetId(fmt.Sprint(*role.ID)) | ||
d.Set("name", role.Name) | ||
d.Set("description", role.Description) | ||
d.Set("base_role", role.BaseRole) | ||
d.Set("permissions", role.Permissions) | ||
|
||
return nil | ||
} |
85 changes: 85 additions & 0 deletions
85
github/data_source_github_organization_custom_role_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package github | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccGithubOrganizationCustomRoleDataSource(t *testing.T) { | ||
|
||
t.Run("queries a custom repo role", func(t *testing.T) { | ||
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
|
||
config := fmt.Sprintf(` | ||
resource "github_organization_custom_role" "test" { | ||
name = "tf-acc-test-%s" | ||
description = "Test role description" | ||
base_role = "read" | ||
permissions = [ | ||
"reopen_issue", | ||
"reopen_pull_request", | ||
] | ||
} | ||
`, randomID) | ||
|
||
config2 := config + ` | ||
data "github_organization_custom_role" "test" { | ||
name = github_organization_custom_role.test.name | ||
} | ||
` | ||
|
||
check := resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet( | ||
"data.github_organization_custom_role.test", "name", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.github_organization_custom_role.test", "name", | ||
fmt.Sprintf(`tf-acc-test-%s`, randomID), | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.github_organization_custom_role.test", "description", | ||
"Test role description", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.github_organization_custom_role.test", "base_role", | ||
"read", | ||
), | ||
resource.TestCheckResourceAttr( | ||
"data.github_organization_custom_role.test", "permissions.#", | ||
"2", | ||
), | ||
) | ||
|
||
testCase := func(t *testing.T, mode string) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { skipUnlessMode(t, mode) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc(), | ||
}, | ||
{ | ||
Config: config2, | ||
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) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/google/go-github/v53/github" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceGithubOrganizationCustomRole() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceGithubOrganizationCustomRoleCreate, | ||
Read: resourceGithubOrganizationCustomRoleRead, | ||
Update: resourceGithubOrganizationCustomRoleUpdate, | ||
Delete: resourceGithubOrganizationCustomRoleDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The organization custom repository role to create.", | ||
}, | ||
"base_role": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The base role for the custom repository role.", | ||
ValidateFunc: validateValueFunc([]string{"read", "triage", "write", "maintain"}), | ||
}, | ||
"permissions": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
MinItems: 1, // At least one permission should be passed. | ||
Description: "The permissions for the custom repository role.", | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The description of the custom repository role.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGithubOrganizationCustomRoleCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
orgName := meta.(*Owner).name | ||
ctx := context.Background() | ||
|
||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
permissions := d.Get("permissions").(*schema.Set).List() | ||
permissionsStr := make([]string, len(permissions)) | ||
for i, v := range permissions { | ||
permissionsStr[i] = v.(string) | ||
} | ||
|
||
role, _, err := client.Organizations.CreateCustomRepoRole(ctx, orgName, &github.CreateOrUpdateCustomRoleOptions{ | ||
Name: github.String(d.Get("name").(string)), | ||
Description: github.String(d.Get("description").(string)), | ||
BaseRole: github.String(d.Get("base_role").(string)), | ||
Permissions: permissionsStr, | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating GitHub custom repository role %s (%s): %s", orgName, d.Get("name").(string), err) | ||
} | ||
|
||
d.SetId(fmt.Sprint(*role.ID)) | ||
return resourceGithubOrganizationCustomRoleRead(d, meta) | ||
} | ||
|
||
func resourceGithubOrganizationCustomRoleRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
ctx := context.Background() | ||
orgName := meta.(*Owner).name | ||
|
||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
roleID := d.Id() | ||
|
||
// ListCustomRepoRoles returns a list of all custom repository roles for an organization. | ||
// There is an API endpoint for getting a single custom repository role, but is not | ||
// implemented in the go-github library. | ||
roleList, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName) | ||
if err != nil { | ||
return fmt.Errorf("error querying GitHub custom repository roles %s: %s", orgName, err) | ||
} | ||
|
||
var role *github.CustomRepoRoles | ||
for _, r := range roleList.CustomRepoRoles { | ||
if fmt.Sprint(*r.ID) == roleID { | ||
role = r | ||
break | ||
} | ||
} | ||
|
||
if role == nil { | ||
log.Printf("[WARN] GitHub custom repository role (%s/%s) not found, removing from state", orgName, roleID) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
d.Set("name", role.Name) | ||
d.Set("description", role.Description) | ||
d.Set("base_role", role.BaseRole) | ||
d.Set("permissions", role.Permissions) | ||
|
||
return nil | ||
} | ||
|
||
func resourceGithubOrganizationCustomRoleUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
ctx := context.Background() | ||
orgName := meta.(*Owner).name | ||
|
||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
roleID := d.Id() | ||
permissions := d.Get("permissions").(*schema.Set).List() | ||
permissionsStr := make([]string, len(permissions)) | ||
for i, v := range permissions { | ||
permissionsStr[i] = v.(string) | ||
} | ||
|
||
update := &github.CreateOrUpdateCustomRoleOptions{ | ||
Name: github.String(d.Get("name").(string)), | ||
Description: github.String(d.Get("description").(string)), | ||
BaseRole: github.String(d.Get("base_role").(string)), | ||
Permissions: permissionsStr, | ||
} | ||
|
||
if _, _, err := client.Organizations.UpdateCustomRepoRole(ctx, orgName, roleID, update); err != nil { | ||
return fmt.Errorf("error updating GitHub custom repository role %s (%s): %s", orgName, roleID, err) | ||
} | ||
|
||
return resourceGithubOrganizationCustomRoleRead(d, meta) | ||
} | ||
|
||
func resourceGithubOrganizationCustomRoleDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Owner).v3client | ||
ctx := context.Background() | ||
orgName := meta.(*Owner).name | ||
|
||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
roleID := d.Id() | ||
|
||
_, err = client.Organizations.DeleteCustomRepoRole(ctx, orgName, roleID) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting GitHub custom repository role %s (%s): %s", orgName, roleID, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to comment on an old/merged PR, but is this a necessary constraint? The API permits creation of a repository role without additional permissions, and it makes perfect sense if the role's only purpose is to designate who can bypass a repository ruleset, for example:
If you can confirm, shall I open another issue (or PR) to correct this? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you may be right about this. I was however unable to get it to post to the API at the time, so I was thinking it did require something, but I may have just passed in the wrong blank value.