diff --git a/docs/resources/team_repository.md b/docs/resources/team_repository.md index ce43f73bdc..598567475a 100644 --- a/docs/resources/team_repository.md +++ b/docs/resources/team_repository.md @@ -34,7 +34,7 @@ resource "github_repository" "some_repo" { resource "github_team_repository" "some_team_repo" { team_id = github_team.some_team.id repository = github_repository.some_repo.name - permission = "pull" + permission = "read" } ``` @@ -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`, `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`. +- `permission` - (Optional) The permissions of team members regarding the repository. Must be one of `read` (equivalent to `pull`), `triage`, `write` (equivalent to `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 `read`. ## Import diff --git a/examples/resources/team_repository/example_1.tf b/examples/resources/team_repository/example_1.tf index c2276ec830..8b14e9ac53 100644 --- a/examples/resources/team_repository/example_1.tf +++ b/examples/resources/team_repository/example_1.tf @@ -11,5 +11,5 @@ resource "github_repository" "some_repo" { resource "github_team_repository" "some_team_repo" { team_id = github_team.some_team.id repository = github_repository.some_repo.name - permission = "pull" + permission = "read" } diff --git a/github/resource_github_team_repository.go b/github/resource_github_team_repository.go index 4fe4593544..42c788e2a3 100644 --- a/github/resource_github_team_repository.go +++ b/github/resource_github_team_repository.go @@ -55,8 +55,11 @@ func resourceGithubTeamRepository() *schema.Resource { "permission": { Type: schema.TypeString, Optional: true, - Default: "pull", - Description: "The permissions of team members regarding the repository. Must be one of 'pull', 'triage', 'push', 'maintain', 'admin' or the name of an existing custom repository role within the organisation.", + Default: "read", + DiffSuppressFunc: func(_, oldValue, newValue string, _ *schema.ResourceData) bool { + return getPermission(oldValue) == getPermission(newValue) + }, + Description: "The permissions of team members regarding the repository. Must be one of 'read' (equivalent to 'pull'), 'triage', 'write' (equivalent to 'push'), 'maintain', 'admin' or the name of an existing custom repository role within the organisation. Defaults to 'read'.", }, "etag": { Type: schema.TypeString, @@ -94,7 +97,7 @@ func resourceGithubTeamRepositoryCreate(ctx context.Context, d *schema.ResourceD orgName, repoName, &github.TeamAddTeamRepoOptions{ - Permission: permission, + Permission: getPermission(permission), }, ) if err != nil { @@ -197,7 +200,7 @@ func resourceGithubTeamRepositoryUpdate(ctx context.Context, d *schema.ResourceD orgName, repoName, &github.TeamAddTeamRepoOptions{ - Permission: permission, + Permission: getPermission(permission), }, ) if err != nil { diff --git a/github/resource_github_team_repository_test.go b/github/resource_github_team_repository_test.go index a72eb20d11..09457ecd81 100644 --- a/github/resource_github_team_repository_test.go +++ b/github/resource_github_team_repository_test.go @@ -44,6 +44,43 @@ func Test_resourceGithubTeamRepositoryDelete_nilResponseDoesNotPanic(t *testing. } } +func Test_resourceGithubTeamRepositoryPermissionAliases(t *testing.T) { + t.Parallel() + + for _, testCase := range []struct { + permission string + want string + }{ + {permission: "read", want: "pull"}, + {permission: "write", want: "push"}, + {permission: "pull", want: "pull"}, + {permission: "push", want: "push"}, + {permission: "triage", want: "triage"}, + } { + t.Run(testCase.permission, func(t *testing.T) { + t.Parallel() + + if got := getPermission(testCase.permission); got != testCase.want { + t.Errorf("getPermission(%q) = %q, want %q", testCase.permission, got, testCase.want) + } + }) + } + + permissionSchema := resourceGithubTeamRepository().Schema["permission"] + if permissionSchema.Default != "read" { + t.Errorf("permission default = %q, want %q", permissionSchema.Default, "read") + } + if !permissionSchema.DiffSuppressFunc("permission", "pull", "read", nil) { + t.Error("expected read and pull permissions to suppress a diff") + } + if !permissionSchema.DiffSuppressFunc("permission", "push", "write", nil) { + t.Error("expected write and push permissions to suppress a diff") + } + if permissionSchema.DiffSuppressFunc("permission", "pull", "push", nil) { + t.Error("expected read and write permissions to remain distinct") + } +} + func TestAccGithubTeamRepository(t *testing.T) { t.Parallel() @@ -67,7 +104,7 @@ func TestAccGithubTeamRepository(t *testing.T) { resource "github_team_repository" "test" { team_id = "${github_team.test.id}" repository = "${github_repository.test.name}" - permission = "pull" + permission = "read" } `, teamName, repoName) @@ -114,25 +151,37 @@ func TestAccGithubTeamRepository(t *testing.T) { }, { Config: strings.Replace(config, - `permission = "pull"`, + `permission = "read"`, + `permission = "pull"`, 1), + Check: checks["pull"], + }, + { + Config: strings.Replace(config, + `permission = "read"`, `permission = "triage"`, 1), Check: checks["triage"], }, { Config: strings.Replace(config, - `permission = "pull"`, + `permission = "read"`, + `permission = "write"`, 1), + Check: checks["push"], + }, + { + Config: strings.Replace(config, + `permission = "read"`, `permission = "push"`, 1), Check: checks["push"], }, { Config: strings.Replace(config, - `permission = "pull"`, + `permission = "read"`, `permission = "maintain"`, 1), Check: checks["maintain"], }, { Config: strings.Replace(config, - `permission = "pull"`, + `permission = "read"`, `permission = "admin"`, 1), Check: checks["admin"], }, diff --git a/templates/resources/team_repository.md.tmpl b/templates/resources/team_repository.md.tmpl index 4a20a59b2c..7e20b85f42 100644 --- a/templates/resources/team_repository.md.tmpl +++ b/templates/resources/team_repository.md.tmpl @@ -28,7 +28,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`, `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`. +- `permission` - (Optional) The permissions of team members regarding the repository. Must be one of `read` (equivalent to `pull`), `triage`, `write` (equivalent to `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 `read`. ## Import