diff --git a/github/resource_github_team_repository.go b/github/resource_github_team_repository.go index e53e4795ca..cf37c10911 100644 --- a/github/resource_github_team_repository.go +++ b/github/resource_github_team_repository.go @@ -36,7 +36,7 @@ func resourceGithubTeamRepository() *schema.Resource { Type: schema.TypeString, Optional: true, Default: "pull", - ValidateFunc: validateValueFunc([]string{"pull", "triage", "push", "maintain", "admin"}), + ValidateFunc: validateTeamRepositoryPermissionFunc, }, "etag": { Type: schema.TypeString, diff --git a/github/util.go b/github/util.go index 2b17a5cc54..2012c93bb9 100644 --- a/github/util.go +++ b/github/util.go @@ -192,3 +192,40 @@ func validateSecretNameFunc(v interface{}, keyName string) (we []string, errs [] return we, errs } + +func validateTeamRepositoryPermissionFunc(v interface{}, keyName string, meta interface{}) (we []string, errors []error) { + roleName, ok := v.(string) + if !ok { + return nil, []error{fmt.Errorf("Expected type of %s to be string", keyName)} + } + + roles := []string{"pull", "triage", "push", "maintain", "admin"} + + client := meta.(*Owner).v3client + orgName := meta.(*Owner).name + ctx := context.Background() + + customRoles, _, err := client.Organizations.ListCustomRepoRoles(ctx, orgName) + + if err != nil { + return nil, []error{fmt.Errorf("Error in response from github while checking for existing custom roles. Error: %s", err)} + } + + for _, role := range customRoles.CustomRepoRoles { + roles = append(roles, *role.Name) + } + + valid := false + for _, role := range roles { + if role == roleName { + valid = true + break + } + } + + if !valid { + return nil, []error{fmt.Errorf("A role with the name %s does not exist in this GitHub organisation", roleName)} + } + + return we, errors +} diff --git a/website/docs/r/team_repository.html.markdown b/website/docs/r/team_repository.html.markdown index e35ebab343..b9af9ac8a5 100644 --- a/website/docs/r/team_repository.html.markdown +++ b/website/docs/r/team_repository.html.markdown @@ -44,7 +44,7 @@ The following arguments are supported: * `team_id` - (Required) The GitHub team id or the GitHub team slug * `repository` - (Required) The repository to add to the team. * `permission` - (Optional) The permissions of team members regarding the repository. - Must be one of `pull`, `triage`, `push`, `maintain`, or `admin`. Defaults to `pull`. + Must be one of `pull`, `triage`, `push`, `maintain`, `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 organisation. Defaults to `pull`. ## Import