Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tag-based environment deployment branch policy #2165

Merged
merged 20 commits into from
Nov 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f459029
Add Type field to DeploymentBranchPolicyRequest struct
mcevoypeter Dec 6, 2023
5b9abc7
Change name of branch_pattern argument to pattern for github_reposito…
mcevoypeter Dec 6, 2023
dace546
Add type argument to github_repository_environment_deployment_policy …
mcevoypeter Dec 6, 2023
7066816
Add tag-based test for github_repository_environment_deployment_policy
mcevoypeter Dec 6, 2023
9b44b63
Revert "Add tag-based test for github_repository_environment_deployme…
mcevoypeter Dec 13, 2023
a0683ed
Revert "Add type argument to github_repository_environment_deployment…
mcevoypeter Dec 13, 2023
f69a727
Revert "Change name of branch_pattern argument to pattern for github_…
mcevoypeter Dec 13, 2023
868cf19
Revert "Add Type field to DeploymentBranchPolicyRequest struct"
mcevoypeter Dec 13, 2023
73701d7
Add tag_pattern attribute to github_repository_environment_deployment…
mcevoypeter Dec 14, 2023
73ca42f
Correct typo
mcevoypeter Dec 14, 2023
8f0ceb5
Remove type parameter from deployment policy update
sumnerwarren Feb 20, 2024
7113782
Add pattern assertions to existing tests
sumnerwarren Feb 20, 2024
c9b0f1d
Fix pattern read to address existing tag policy test
sumnerwarren Feb 20, 2024
b040606
Fix update to read the configured pattern
sumnerwarren Feb 20, 2024
92243c5
Force new resource when pattern type changes
sumnerwarren Feb 20, 2024
82749d6
Merge branch 'main' into feature/deployment-policy-tag-pattern
kfcampbell Mar 4, 2024
3d9beaa
Merge branch 'main' into feature/deployment-policy-tag-pattern
nickfloyd Mar 19, 2024
eb36f7e
Merge branch 'main' into feature/deployment-policy-tag-pattern
kfcampbell Nov 20, 2024
6a477c1
Merge branch 'main' into feature/deployment-policy-tag-pattern
kfcampbell Nov 21, 2024
7999b4d
Fix tests by ignoring vulnerability_alerts
kfcampbell Nov 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tag_pattern attribute to github_repository_environment_deployment…
…_policy resource
mcevoypeter authored and sumnerwarren committed Feb 20, 2024
commit 73701d720fc0f269fe6d7131afc4aa66238208a0
4 changes: 2 additions & 2 deletions github/resource_github_repository_deployment_branch_policy.go
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ func resourceGithubRepositoryDeploymentBranchPolicyUpdate(d *schema.ResourceData
return err
}

_, _, err = client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, int64(id), &github.DeploymentBranchPolicyRequest{Name: &name})
_, _, err = client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, int64(id), &github.DeploymentBranchPolicyRequest{Name: &name, Type: github.String("branch")})
if err != nil {
return err
}
@@ -80,7 +80,7 @@ func resourceGithubRepositoryDeploymentBranchPolicyCreate(d *schema.ResourceData
environmentName := d.Get("environment_name").(string)
name := d.Get("name").(string)

policy, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, &github.DeploymentBranchPolicyRequest{Name: &name})
policy, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, &github.DeploymentBranchPolicyRequest{Name: &name, Type: github.String("branch")})
if err != nil {
return err
}
33 changes: 26 additions & 7 deletions github/resource_github_repository_environment_deployment_policy.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package github

import (
"context"
"fmt"
"log"
"net/http"
"net/url"
@@ -34,10 +35,18 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource {
Description: "The name of the environment.",
},
"branch_pattern": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
Description: "The name pattern that branches must match in order to deploy to the environment.",
Type: schema.TypeString,
Optional: true,
ForceNew: false,
ConflictsWith: []string{"tag_pattern"},
Description: "The name pattern that branches must match in order to deploy to the environment.",
},
"tag_pattern": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
ConflictsWith: []string{"branch_pattern"},
Description: "The name pattern that tags must match in order to deploy to the environment.",
},
},
}
@@ -51,11 +60,21 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.Resourc
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
branchPattern := d.Get("branch_pattern").(string)
escapedEnvName := url.PathEscape(envName)

createData := github.DeploymentBranchPolicyRequest{
Name: github.String(branchPattern),
var createData github.DeploymentBranchPolicyRequest
if v, ok := d.GetOk("branch_pattern"); ok {
createData = github.DeploymentBranchPolicyRequest{
Name: github.String(v.(string)),
Type: github.String("branch"),
}
} else if v, ok := d.GetOk("tag_pattern"); ok {
createData = github.DeploymentBranchPolicyRequest{
Name: github.String(v.(string)),
Type: github.String("tag"),
}
} else {
return fmt.Errorf("exactly one of %q and %q must be specified", "branch_pattern", "tag_pattern")
}

resultKey, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, &createData)
Original file line number Diff line number Diff line change
@@ -8,11 +8,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccGithubRepositoryEnvironmentDeploymentPolicy(t *testing.T) {
func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranch(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("creates a repository environment with deployment policy", func(t *testing.T) {
t.Run("creates a repository environment with branch-based deployment policy", func(t *testing.T) {

config := fmt.Sprintf(`
@@ -87,3 +87,83 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicy(t *testing.T) {

})
}

func TestAccGithubRepositoryEnvironmentDeploymentPolicyTag(t *testing.T) {

randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

t.Run("creates a repository environment with tag-based deployment policy", func(t *testing.T) {

config := fmt.Sprintf(`
data "github_user" "current" {
username = ""
}
resource "github_repository" "test" {
name = "tf-acc-test-%s"
}
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "environment/test"
wait_timer = 10000
reviewers {
users = [data.github_user.current.id]
}
deployment_branch_policy {
protected_branches = false
custom_branch_policies = true
}
}
resource "github_repository_environment_deployment_policy" "test" {
repository = github_repository.test.name
environment = github_repository_environment.test.environment
tag_pattern = "v*"
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_environment_deployment_policy.test", "repository",
fmt.Sprintf("tf-acc-test-%s", randomID),
),
resource.TestCheckResourceAttr(
"github_repository_environment_deployment_policy.test", "environment",
"environment/test",
),
resource.TestCheckResourceAttr(
"github_repository_environment_deployment_policy.test", "tag_pattern",
"v*",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ This resource allows you to create and manage environment deployment branch poli

## Example Usage

Create a branch-based deployment policy:

```hcl
data "github_user" "current" {
username = ""
@@ -21,9 +23,9 @@ resource "github_repository" "test" {
}
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "environment/test"
wait_timer = 10000
repository = github_repository.test.name
environment = "environment/test"
wait_timer = 10000
reviewers {
users = [data.github_user.current.id]
}
@@ -34,12 +36,45 @@ resource "github_repository_environment" "test" {
}
resource "github_repository_environment_deployment_policy" "test" {
repository = github_repository.test.name
environment = github_repository_environment.test.environment
repository = github_repository.test.name
environmen = github_repository_environment.test.environment
branch_pattern = "releases/*"
}
```

Create a tag-based deployment policy:

```hcl
data "github_user" "current" {
username = ""
}
resource "github_repository" "test" {
name = "tf-acc-test-%s"
}
resource "github_repository_environment" "test" {
repository = github_repository.test.name
environment = "environment/test"
wait_timer = 10000
reviewers {
users = [data.github_user.current.id]
}
deployment_branch_policy {
protected_branches = false
custom_branch_policies = true
}
}
resource "github_repository_environment_deployment_policy" "test" {
repository = github_repository.test.name
environment = github_repository_environment.test.environment
tag_pattern = "v*"
}
```


## Argument Reference

The following arguments are supported:
@@ -48,7 +83,9 @@ The following arguments are supported:

* `repository` - (Required) The repository of the environment.

* `branch_pattern` - (Required) The name pattern that branches must match in order to deploy to the environment.
* `branch_pattern` - (Optional) The name pattern that branches must match in order to deploy to the environment. If not specified, `tag_pattern` must be specified.

* `tag_pattern` - (Optional) The name pattern that tags must match in order to deploy to the environment. If not specified, `branch_pattern` must be specified.


## Import