Skip to content

Commit

Permalink
Add type argument to github_repository_environment_deployment_policy …
Browse files Browse the repository at this point in the history
…resource
  • Loading branch information
mcevoypeter committed Dec 6, 2023
1 parent 029960b commit a534219
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
18 changes: 16 additions & 2 deletions github/resource_github_repository_environment_deployment_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"context"
"fmt"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -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\"`.",
},
},
}
Expand All @@ -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)
Expand Down Expand Up @@ -100,6 +112,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceD
}

d.Set("pattern", branchPolicy.GetName())
d.Set("type", branchPolicy.GetType())
return nil
}

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand All @@ -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
Expand Down

0 comments on commit a534219

Please sign in to comment.