Skip to content
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

Support "maintain" and "triage" for repository collaborators #457

Merged
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
56 changes: 44 additions & 12 deletions github/resource_github_repository_collaborator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

const expectedPermission string = "admin"

func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
if testCollaborator == "" {
t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set")
Expand All @@ -24,17 +22,49 @@ func TestAccGithubRepositoryCollaborator_basic(t *testing.T) {
rn := "github_repository_collaborator.test_repo_collaborator"
repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5))

permissionAdmin := "admin"
permissionTriage := "triage"
permissionPush := "push"
permissionPull := "pull"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator),
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator, permissionPull),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryCollaboratorExists(rn),
testAccCheckGithubRepositoryCollaboratorPermission(rn),
resource.TestCheckResourceAttr(rn, "permission", expectedPermission),
testAccCheckGithubRepositoryCollaboratorPermission(rn, permissionPull),
resource.TestCheckResourceAttr(rn, "permission", permissionPull),
resource.TestMatchResourceAttr(rn, "invitation_id", regexp.MustCompile(`^[0-9]+$`)),
),
},
{
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator, permissionPush),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryCollaboratorExists(rn),
testAccCheckGithubRepositoryCollaboratorPermission(rn, permissionPush),
resource.TestCheckResourceAttr(rn, "permission", permissionPush),
resource.TestMatchResourceAttr(rn, "invitation_id", regexp.MustCompile(`^[0-9]+$`)),
),
},
{
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator, permissionAdmin),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryCollaboratorExists(rn),
testAccCheckGithubRepositoryCollaboratorPermission(rn, permissionAdmin),
resource.TestCheckResourceAttr(rn, "permission", permissionAdmin),
resource.TestMatchResourceAttr(rn, "invitation_id", regexp.MustCompile(`^[0-9]+$`)),
),
},
{
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator, permissionTriage),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryCollaboratorExists(rn),
testAccCheckGithubRepositoryCollaboratorPermission(rn, permissionTriage),
resource.TestCheckResourceAttr(rn, "permission", permissionTriage),
resource.TestMatchResourceAttr(rn, "invitation_id", regexp.MustCompile(`^[0-9]+$`)),
),
},
Expand All @@ -58,6 +88,8 @@ func TestAccGithubRepositoryCollaborator_caseInsensitive(t *testing.T) {
var origInvitation github.RepositoryInvitation
var otherInvitation github.RepositoryInvitation

expectedPermission := "maintain"

otherCase := flipUsernameCase(testCollaborator)

if testCollaborator == otherCase {
Expand All @@ -70,13 +102,13 @@ func TestAccGithubRepositoryCollaborator_caseInsensitive(t *testing.T) {
CheckDestroy: testAccCheckGithubRepositoryCollaboratorDestroy,
Steps: []resource.TestStep{
{
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator),
Config: testAccGithubRepositoryCollaboratorConfig(repoName, testCollaborator, expectedPermission),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryCollaboratorInvited(repoName, testCollaborator, &origInvitation),
),
},
{
Config: testAccGithubRepositoryCollaboratorConfig(repoName, otherCase),
Config: testAccGithubRepositoryCollaboratorConfig(repoName, otherCase, expectedPermission),
Check: resource.ComposeTestCheckFunc(
testAccCheckGithubRepositoryCollaboratorInvited(repoName, otherCase, &otherInvitation),
resource.TestCheckResourceAttr(rn, "username", testCollaborator),
Expand Down Expand Up @@ -162,7 +194,7 @@ func testAccCheckGithubRepositoryCollaboratorExists(n string) resource.TestCheck
}
}

func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestCheckFunc {
func testAccCheckGithubRepositoryCollaboratorPermission(n, permission string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand Down Expand Up @@ -194,8 +226,8 @@ func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestC
return err
}

if permName != expectedPermission {
return fmt.Errorf("Expected permission %s on repository collaborator, actual permission %s", expectedPermission, permName)
if permName != permission {
return fmt.Errorf("Expected permission %s on repository collaborator, actual permission %s", permission, permName)
}

return nil
Expand All @@ -206,7 +238,7 @@ func testAccCheckGithubRepositoryCollaboratorPermission(n string) resource.TestC
}
}

func testAccGithubRepositoryCollaboratorConfig(repoName, username string) string {
func testAccGithubRepositoryCollaboratorConfig(repoName, username, permission string) string {
return fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
Expand All @@ -217,7 +249,7 @@ resource "github_repository_collaborator" "test_repo_collaborator" {
username = "%s"
permission = "%s"
}
`, repoName, username, expectedPermission)
`, repoName, username, permission)
}

func testAccCheckGithubRepositoryCollaboratorInvited(repoName, username string, invitation *github.RepositoryInvitation) resource.TestCheckFunc {
Expand Down
4 changes: 4 additions & 0 deletions github/util_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func getInvitationPermission(i *github.RepositoryInvitation) (string, error) {
return pushPermission, nil
} else if *i.Permissions == adminPermission {
return adminPermission, nil
} else if *i.Permissions == maintainPermission {
return maintainPermission, nil
} else if *i.Permissions == triagePermission {
return triagePermission, nil
}

return "", fmt.Errorf("unexpected permission value: %v", *i.Permissions)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/repository_collaborator.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ The following arguments are supported:
* `repository` - (Required) The GitHub repository
* `username` - (Required) The user to add to the repository as a collaborator.
* `permission` - (Optional) The permission of the outside collaborator for the repository.
Must be one of `pull`, `push`, or `admin`. Defaults to `push`.
Must be one of `pull`, `push`, `maintain`, `triage` or `admin`. Defaults to `push`.

## Attribute Reference

Expand Down