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

use GetRoleName() instead of permission mapping func #1192

Merged
merged 2 commits into from
Jul 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 1 addition & 7 deletions github/data_source_github_collaborators.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
```
```