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 #2050

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion github/resource_github_issue_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"strings"

"github.com/google/go-github/v52/github"
"github.com/google/go-github/v55/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

Expand Down
4 changes: 2 additions & 2 deletions github/resource_github_repository_deployment_branch_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
33 changes: 26 additions & 7 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 @@ -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.",
},
},
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/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(`

Expand Down Expand Up @@ -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)
})

})
}