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
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
77 changes: 68 additions & 9 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,12 +35,21 @@ 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.",
},
},
CustomizeDiff: customDeploymentPolicyDiffFunction,
}

}
Expand All @@ -51,11 +61,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 Expand Up @@ -98,7 +118,11 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceD
return err
}

d.Set("branch_pattern", branchPolicy.GetName())
if branchPolicy.GetType() == "branch" {
d.Set("branch_pattern", branchPolicy.GetName())
} else {
d.Set("tag_pattern", branchPolicy.GetName())
}
return nil
}

Expand All @@ -110,6 +134,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.Resourc
repoName := d.Get("repository").(string)
envName := d.Get("environment").(string)
branchPattern := d.Get("branch_pattern").(string)
tagPattern := d.Get("tag_pattern").(string)
escapedEnvName := url.PathEscape(envName)
_, _, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
if err != nil {
Expand All @@ -121,8 +146,13 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.Resourc
return err
}

pattern := branchPattern
if branchPattern == "" {
pattern = tagPattern
}

updateData := github.DeploymentBranchPolicyRequest{
Name: github.String(branchPattern),
Name: github.String(pattern),
}

resultKey, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, branchPolicyId, &updateData)
Expand Down Expand Up @@ -155,3 +185,32 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyDelete(d *schema.Resourc

return nil
}

func customDeploymentPolicyDiffFunction(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
oldBranchPattern, newBranchPattern := diff.GetChange("branch_pattern")

if oldBranchPattern != "" && newBranchPattern == "" {
if err := diff.ForceNew("branch_pattern"); err != nil {
return err
}
}
if oldBranchPattern == "" && newBranchPattern != "" {
if err := diff.ForceNew("branch_pattern"); err != nil {
return err
}
}

oldTagPattern, newTagPattern := diff.GetChange("tag_pattern")
if oldTagPattern != "" && newTagPattern == "" {
if err := diff.ForceNew("tag_pattern"); err != nil {
return err
}
}
if oldTagPattern == "" && newTagPattern != "" {
if err := diff.ForceNew("tag_pattern"); err != nil {
return err
}
}

return nil
}
Loading
Loading