Skip to content

Commit

Permalink
use getrolename instead of permission mapping func (#1192)
Browse files Browse the repository at this point in the history
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
joshua-hancox and kfcampbell committed Jul 15, 2022
1 parent eb66ae3 commit 754d73f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 49 deletions.
8 changes: 1 addition & 7 deletions github/data_source_github_collaborators.go
Expand Up @@ -179,13 +179,7 @@ func flattenGitHubCollaborators(collaborators []*github.User) ([]interface{}, er
result["received_events_url"] = c.GetReceivedEventsURL()
result["type"] = c.GetType()
result["site_admin"] = c.GetSiteAdmin()

permissionName, err := getRepoPermission(c.GetPermissions())
if err != nil {
return nil, err
}

result["permission"] = permissionName
result["permission"] = c.GetRoleName()
results = append(results, result)
}

Expand Down
16 changes: 5 additions & 11 deletions github/resource_github_repository_collaborator.go
Expand Up @@ -35,11 +35,10 @@ func resourceGithubRepositoryCollaborator() *schema.Resource {
ForceNew: true,
},
"permission": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "push",
ValidateFunc: validateValueFunc([]string{"pull", "triage", "push", "maintain", "admin"}),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "push",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if d.Get("permission_diff_suppression").(bool) {
if new == "triage" || new == "maintain" {
Expand Down Expand Up @@ -141,14 +140,9 @@ func resourceGithubRepositoryCollaboratorRead(d *schema.ResourceData, meta inter

for _, c := range collaborators {
if strings.EqualFold(c.GetLogin(), username) {
permissionName, err := getRepoPermission(c.GetPermissions())
if err != nil {
return err
}

d.Set("repository", repoName)
d.Set("username", c.GetLogin())
d.Set("permission", permissionName)
d.Set("permission", c.GetRoleName())
return nil
}
}
Expand Down
8 changes: 1 addition & 7 deletions github/resource_github_team_repository.go
Expand Up @@ -131,13 +131,7 @@ func resourceGithubTeamRepositoryRead(d *schema.ResourceData, meta interface{})
d.Set("team_id", teamIdString)
}
d.Set("repository", repo.GetName())

permName, permErr := getRepoPermission(repo.GetPermissions())
if permErr != nil {
return permErr
}

d.Set("permission", permName)
d.Set("permission", repo.GetRoleName())

return nil
}
Expand Down
22 changes: 0 additions & 22 deletions github/util_permissions.go
@@ -1,7 +1,6 @@
package github

import (
"errors"
"fmt"

"github.com/google/go-github/v45/github"
Expand All @@ -17,27 +16,6 @@ const (
readPermission string = "read"
)

func getRepoPermission(p map[string]bool) (string, error) {

// Permissions are returned in this map format such that if you have a certain level
// of permission, all levels below are also true. For example, if a team has push
// permission, the map will be: {"pull": true, "push": true, "admin": false}
if (p)[adminPermission] {
return adminPermission, nil
} else if (p)[maintainPermission] {
return maintainPermission, nil
} else if (p)[pushPermission] {
return pushPermission, nil
} else if (p)[triagePermission] {
return triagePermission, nil
} else {
if (p)[pullPermission] {
return pullPermission, nil
}
return "", errors.New("At least one permission expected from permissions map.")
}
}

func getInvitationPermission(i *github.RepositoryInvitation) (string, error) {
// Permissions for some GitHub API routes are expressed as "read",
// "write", and "admin"; in other places, they are expressed as "pull",
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/repository_collaborator.html.markdown
Expand Up @@ -44,7 +44,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`, `maintain`, `triage` or `admin` for organization-owned repositories.
Must be one of `pull`, `push`, `maintain`, `triage` or `admin` or the name of an existing [custom repository role](https://docs.github.com/en/enterprise-cloud@latest/organizations/managing-peoples-access-to-your-organization-with-roles/managing-custom-repository-roles-for-an-organization) within the organization for organization-owned repositories.
Must be `push` for personal repositories. Defaults to `push`.
* `permission_diff_suppression` - (Optional) Suppress plan diffs for `triage` and `maintain`. Defaults to `false`.

Expand All @@ -60,4 +60,4 @@ GitHub Repository Collaborators can be imported using an ID made up of `reposito

```
$ terraform import github_repository_collaborator.collaborator terraform:someuser
```
```

0 comments on commit 754d73f

Please sign in to comment.