diff --git a/github/resource_github_repository_environment_deployment_policy.go b/github/resource_github_repository_environment_deployment_policy.go index 25db705ea6..5427589cf0 100644 --- a/github/resource_github_repository_environment_deployment_policy.go +++ b/github/resource_github_repository_environment_deployment_policy.go @@ -2,6 +2,7 @@ package github import ( "context" + "fmt" "log" "net/http" "net/url" @@ -37,7 +38,13 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: false, - Description: "The name pattern that branches must match in order to deploy to the environment.", + Description: "The name pattern that branches/tags must match in order to deploy to the environment.", + }, + "type": { + Type: schema.TypeString, + Required: true, + ForceNew: false, + Description: "The type of `pattern`. Either `\"branch\"` or `\"tag\"`.", }, }, } @@ -52,11 +59,16 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.Resourc repoName := d.Get("repository").(string) envName := d.Get("environment").(string) pattern := d.Get("pattern").(string) + patternType := d.Get("type").(string) escapedEnvName := url.PathEscape(envName) + if patternType != "branch" && patternType != "tag" { + return fmt.Errorf("type must be either %q or %q, not %q", "branch", "tag", patternType) + } + createData := github.DeploymentBranchPolicyRequest{ Name: github.String(pattern), - Type: github.String("branch"), + Type: github.String(patternType), } resultKey, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, &createData) @@ -100,6 +112,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceD } d.Set("pattern", branchPolicy.GetName()) + d.Set("type", branchPolicy.GetType()) return nil } @@ -112,6 +125,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.Resourc envName := d.Get("environment").(string) pattern := d.Get("pattern").(string) escapedEnvName := url.PathEscape(envName) + _, _, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId") if err != nil { return err diff --git a/github/resource_github_repository_environment_deployment_policy_test.go b/github/resource_github_repository_environment_deployment_policy_test.go index b3b98d254a..92c4746076 100644 --- a/github/resource_github_repository_environment_deployment_policy_test.go +++ b/github/resource_github_repository_environment_deployment_policy_test.go @@ -41,6 +41,7 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicy(t *testing.T) { repository = github_repository.test.name environment = github_repository_environment.test.environment pattern = "releases/*" + type = "branch" } `, randomID) @@ -58,6 +59,10 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicy(t *testing.T) { "github_repository_environment_deployment_policy.test", "pattern", "releases/*", ), + resource.TestCheckResourceAttr( + "github_repository_environment_deployment_policy.test", "type", + "branch", + ), ) testCase := func(t *testing.T, mode string) { diff --git a/website/docs/r/repository_environment_deployment_policy.html.markdown b/website/docs/r/repository_environment_deployment_policy.html.markdown index 7738eb085c..8af564cbc5 100644 --- a/website/docs/r/repository_environment_deployment_policy.html.markdown +++ b/website/docs/r/repository_environment_deployment_policy.html.markdown @@ -37,6 +37,7 @@ resource "github_repository_environment_deployment_policy" "test" { repository = github_repository.test.name environment = github_repository_environment.test.environment pattern = "releases/*" + type = "branch" } ``` @@ -48,7 +49,9 @@ The following arguments are supported: * `repository` - (Required) The repository of the environment. -* `pattern` - (Required) The name pattern that branches must match in order to deploy to the environment. +* `pattern` - (Required) The name pattern that branches/tags must match in order to deploy to the environment. + +* `type` - (Required) The type of `pattern`. Either `"branch"` or `"tag"`. ## Import