From 754d73f66bd3b5da49d5f3e0714a4186d40d0377 Mon Sep 17 00:00:00 2001 From: Joshua Hancox <67631498+joshua-hancox@users.noreply.github.com> Date: Fri, 15 Jul 2022 22:45:42 +0100 Subject: [PATCH] use getrolename instead of permission mapping func (#1192) Co-authored-by: Keegan Campbell --- github/data_source_github_collaborators.go | 8 +------ ...resource_github_repository_collaborator.go | 16 +++++--------- github/resource_github_team_repository.go | 8 +------ github/util_permissions.go | 22 ------------------- .../r/repository_collaborator.html.markdown | 4 ++-- 5 files changed, 9 insertions(+), 49 deletions(-) diff --git a/github/data_source_github_collaborators.go b/github/data_source_github_collaborators.go index d2825373a3..99b515f46c 100644 --- a/github/data_source_github_collaborators.go +++ b/github/data_source_github_collaborators.go @@ -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) } diff --git a/github/resource_github_repository_collaborator.go b/github/resource_github_repository_collaborator.go index 1ffa1f5523..7dbc998438 100644 --- a/github/resource_github_repository_collaborator.go +++ b/github/resource_github_repository_collaborator.go @@ -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" { @@ -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 } } diff --git a/github/resource_github_team_repository.go b/github/resource_github_team_repository.go index 447ca307cc..dea3210238 100644 --- a/github/resource_github_team_repository.go +++ b/github/resource_github_team_repository.go @@ -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 } diff --git a/github/util_permissions.go b/github/util_permissions.go index 399c693c46..13c7e6d2f4 100644 --- a/github/util_permissions.go +++ b/github/util_permissions.go @@ -1,7 +1,6 @@ package github import ( - "errors" "fmt" "github.com/google/go-github/v45/github" @@ -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", diff --git a/website/docs/r/repository_collaborator.html.markdown b/website/docs/r/repository_collaborator.html.markdown index f3f065d17e..bcede71307 100644 --- a/website/docs/r/repository_collaborator.html.markdown +++ b/website/docs/r/repository_collaborator.html.markdown @@ -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`. @@ -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 -``` \ No newline at end of file +```