Skip to content

Commit

Permalink
Add support for tag-based environment deployment branch policy (#2165)
Browse files Browse the repository at this point in the history
* Add Type field to DeploymentBranchPolicyRequest struct

The Type field is only necessary when creating a deployment branch
policy. When updating a deployment branch policy, the Type field is not
needed and is therefore set to nil.

Resources:
- https://docs.github.com/en/rest/deployments/branch-policies?apiVersion=2022-11-28

* Change name of branch_pattern argument to pattern for github_repository_environment_deployment_policy resource

* Add type argument to github_repository_environment_deployment_policy resource

* Add tag-based test for github_repository_environment_deployment_policy

* Revert "Add tag-based test for github_repository_environment_deployment_policy"

This reverts commit 88b1369.

* Revert "Add type argument to github_repository_environment_deployment_policy resource"

This reverts commit a534219.

* Revert "Change name of branch_pattern argument to pattern for github_repository_environment_deployment_policy resource"

This reverts commit 029960b.

* Revert "Add Type field to DeploymentBranchPolicyRequest struct"

This reverts commit af308a1.

* Add tag_pattern attribute to github_repository_environment_deployment_policy resource

* Correct typo

See #2050 (comment).

* Remove type parameter from deployment policy update

* Add pattern assertions to existing tests

* Fix pattern read to address existing tag policy test

* Fix update to read the configured pattern

* Force new resource when pattern type changes

* Fix tests by ignoring vulnerability_alerts

---------

Co-authored-by: Peter McEvoy <git@mcevoypeter.com>
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
  • Loading branch information
4 people authored Nov 21, 2024
1 parent c644bee commit 84c6bd2
Show file tree
Hide file tree
Showing 4 changed files with 834 additions and 18 deletions.
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

0 comments on commit 84c6bd2

Please sign in to comment.