Skip to content

Commit 84c6bd2

Browse files
sumnerwarrenmcevoypeterkfcampbellnickfloyd
authored
Add support for tag-based environment deployment branch policy (#2165)
* 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 <[email protected]> Co-authored-by: Keegan Campbell <[email protected]> Co-authored-by: Nick Floyd <[email protected]>
1 parent c644bee commit 84c6bd2

4 files changed

+834
-18
lines changed

github/resource_github_repository_deployment_branch_policy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func resourceGithubRepositoryDeploymentBranchPolicyCreate(d *schema.ResourceData
8080
environmentName := d.Get("environment_name").(string)
8181
name := d.Get("name").(string)
8282

83-
policy, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, &github.DeploymentBranchPolicyRequest{Name: &name})
83+
policy, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, &github.DeploymentBranchPolicyRequest{Name: &name, Type: github.String("branch")})
8484
if err != nil {
8585
return err
8686
}

github/resource_github_repository_environment_deployment_policy.go

+68-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package github
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"net/http"
78
"net/url"
@@ -34,12 +35,21 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource {
3435
Description: "The name of the environment.",
3536
},
3637
"branch_pattern": {
37-
Type: schema.TypeString,
38-
Required: true,
39-
ForceNew: false,
40-
Description: "The name pattern that branches must match in order to deploy to the environment.",
38+
Type: schema.TypeString,
39+
Optional: true,
40+
ForceNew: false,
41+
ConflictsWith: []string{"tag_pattern"},
42+
Description: "The name pattern that branches must match in order to deploy to the environment.",
43+
},
44+
"tag_pattern": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
ForceNew: false,
48+
ConflictsWith: []string{"branch_pattern"},
49+
Description: "The name pattern that tags must match in order to deploy to the environment.",
4150
},
4251
},
52+
CustomizeDiff: customDeploymentPolicyDiffFunction,
4353
}
4454

4555
}
@@ -51,11 +61,21 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.Resourc
5161
owner := meta.(*Owner).name
5262
repoName := d.Get("repository").(string)
5363
envName := d.Get("environment").(string)
54-
branchPattern := d.Get("branch_pattern").(string)
5564
escapedEnvName := url.PathEscape(envName)
5665

57-
createData := github.DeploymentBranchPolicyRequest{
58-
Name: github.String(branchPattern),
66+
var createData github.DeploymentBranchPolicyRequest
67+
if v, ok := d.GetOk("branch_pattern"); ok {
68+
createData = github.DeploymentBranchPolicyRequest{
69+
Name: github.String(v.(string)),
70+
Type: github.String("branch"),
71+
}
72+
} else if v, ok := d.GetOk("tag_pattern"); ok {
73+
createData = github.DeploymentBranchPolicyRequest{
74+
Name: github.String(v.(string)),
75+
Type: github.String("tag"),
76+
}
77+
} else {
78+
return fmt.Errorf("exactly one of %q and %q must be specified", "branch_pattern", "tag_pattern")
5979
}
6080

6181
resultKey, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, &createData)
@@ -98,7 +118,11 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceD
98118
return err
99119
}
100120

101-
d.Set("branch_pattern", branchPolicy.GetName())
121+
if branchPolicy.GetType() == "branch" {
122+
d.Set("branch_pattern", branchPolicy.GetName())
123+
} else {
124+
d.Set("tag_pattern", branchPolicy.GetName())
125+
}
102126
return nil
103127
}
104128

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

149+
pattern := branchPattern
150+
if branchPattern == "" {
151+
pattern = tagPattern
152+
}
153+
124154
updateData := github.DeploymentBranchPolicyRequest{
125-
Name: github.String(branchPattern),
155+
Name: github.String(pattern),
126156
}
127157

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

156186
return nil
157187
}
188+
189+
func customDeploymentPolicyDiffFunction(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
190+
oldBranchPattern, newBranchPattern := diff.GetChange("branch_pattern")
191+
192+
if oldBranchPattern != "" && newBranchPattern == "" {
193+
if err := diff.ForceNew("branch_pattern"); err != nil {
194+
return err
195+
}
196+
}
197+
if oldBranchPattern == "" && newBranchPattern != "" {
198+
if err := diff.ForceNew("branch_pattern"); err != nil {
199+
return err
200+
}
201+
}
202+
203+
oldTagPattern, newTagPattern := diff.GetChange("tag_pattern")
204+
if oldTagPattern != "" && newTagPattern == "" {
205+
if err := diff.ForceNew("tag_pattern"); err != nil {
206+
return err
207+
}
208+
}
209+
if oldTagPattern == "" && newTagPattern != "" {
210+
if err := diff.ForceNew("tag_pattern"); err != nil {
211+
return err
212+
}
213+
}
214+
215+
return nil
216+
}

0 commit comments

Comments
 (0)