Skip to content

Commit 75ed20e

Browse files
authored
fix: Fix repository environment deployment (#2993)
Signed-off-by: Steve Hipwell <[email protected]>
1 parent dda3f9a commit 75ed20e

5 files changed

Lines changed: 286 additions & 91 deletions

github/data_source_github_repository_environment_deployment_policies.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
89
)
910

1011
func dataSourceGithubRepositoryEnvironmentDeploymentPolicies() *schema.Resource {
1112
return &schema.Resource{
12-
Read: dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead,
13+
ReadContext: dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead,
1314

1415
Schema: map[string]*schema.Schema{
1516
"repository": {
@@ -44,26 +45,31 @@ func dataSourceGithubRepositoryEnvironmentDeploymentPolicies() *schema.Resource
4445
}
4546
}
4647

47-
func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(d *schema.ResourceData, meta any) error {
48+
func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
4849
client := meta.(*Owner).v3client
4950
owner := meta.(*Owner).name
5051
repoName := d.Get("repository").(string)
51-
environmentName := d.Get("environment_name").(string)
52+
environmentName := d.Get("environment").(string)
5253

53-
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(context.Background(), owner, repoName, environmentName)
54+
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, owner, repoName, environmentName)
5455
if err != nil {
55-
return err
56+
return diag.FromErr(err)
5657
}
5758

5859
results := make([]map[string]any, 0)
5960

6061
for _, policy := range policies.BranchPolicies {
6162
policyMap := make(map[string]any)
62-
policyMap["type"] = policy.Type
63+
policyMap["type"] = policy.GetType()
6364
policyMap["pattern"] = policy.GetName()
6465
results = append(results, policyMap)
6566
}
6667

6768
d.SetId(fmt.Sprintf("%s:%s", repoName, environmentName))
68-
return d.Set("policies", results)
69+
err = d.Set("policies", results)
70+
if err != nil {
71+
return diag.FromErr(err)
72+
}
73+
74+
return nil
6975
}

github/data_source_github_repository_environment_deployment_policies_test.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,16 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicies(t *testing.T) {
3636
resource "github_repository_environment_deployment_policy" "tag" {
3737
repository = github_repository.test.name
3838
environment = github_repository_environment.env.environment
39-
tag_pattern = "bar"
39+
tag_pattern = "bar"
4040
}
41-
`, randomID)
4241
43-
config2 := config + `
44-
data "github_repository_environment_deployment_policies" "all" {
42+
data "github_repository_environment_deployment_policies" "test" {
4543
repository = github_repository.test.name
4644
environment = github_repository_environment.env.environment
45+
46+
depends_on = [github_repository_environment_deployment_policy.branch, github_repository_environment_deployment_policy.tag]
4747
}
48-
`
49-
check := resource.ComposeTestCheckFunc(
50-
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.#", "2"),
51-
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.0.type", "branch"),
52-
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.0.name", "foo"),
53-
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.1.type", "tag"),
54-
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.all", "policies.1.name", "bar"),
55-
)
48+
`, randomID)
5649

5750
testCase := func(t *testing.T, mode string) {
5851
resource.Test(t, resource.TestCase{
@@ -63,8 +56,14 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicies(t *testing.T) {
6356
Config: config,
6457
},
6558
{
66-
Config: config2,
67-
Check: check,
59+
Config: config,
60+
Check: resource.ComposeTestCheckFunc(
61+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.test", "policies.#", "2"),
62+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.test", "policies.0.type", "branch"),
63+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.test", "policies.0.pattern", "foo"),
64+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.test", "policies.1.type", "tag"),
65+
resource.TestCheckResourceAttr("data.github_repository_environment_deployment_policies.test", "policies.1.pattern", "bar"),
66+
),
6867
},
6968
},
7069
})

github/resource_github_actions_organization_secret.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func resourceGithubActionsOrganizationSecret() *schema.Resource {
2323
if err := d.Set("secret_name", d.Id()); err != nil {
2424
return nil, err
2525
}
26+
if err := d.Set("destroy_on_drift", true); err != nil {
27+
return nil, err
28+
}
2629
return []*schema.ResourceData{d}, nil
2730
},
2831
},

github/resource_github_repository_environment_deployment_policy.go

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,74 @@ package github
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"log"
87
"net/http"
98
"net/url"
109
"strconv"
1110

1211
"github.com/google/go-github/v67/github"
12+
"github.com/hashicorp/go-cty/cty"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
)
1516

1617
func resourceGithubRepositoryEnvironmentDeploymentPolicy() *schema.Resource {
1718
return &schema.Resource{
18-
Create: resourceGithubRepositoryEnvironmentDeploymentPolicyCreate,
19-
Read: resourceGithubRepositoryEnvironmentDeploymentPolicyRead,
20-
Update: resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate,
21-
Delete: resourceGithubRepositoryEnvironmentDeploymentPolicyDelete,
19+
CreateContext: resourceGithubRepositoryEnvironmentDeploymentPolicyCreate,
20+
ReadContext: resourceGithubRepositoryEnvironmentDeploymentPolicyRead,
21+
UpdateContext: resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate,
22+
DeleteContext: resourceGithubRepositoryEnvironmentDeploymentPolicyDelete,
2223
Importer: &schema.ResourceImporter{
2324
StateContext: schema.ImportStatePassthroughContext,
2425
},
2526
Schema: map[string]*schema.Schema{
2627
"repository": {
28+
Description: "The name of the GitHub repository.",
2729
Type: schema.TypeString,
2830
Required: true,
2931
ForceNew: true,
30-
Description: "The name of the GitHub repository.",
3132
},
3233
"environment": {
34+
Description: "The name of the environment.",
3335
Type: schema.TypeString,
3436
Required: true,
3537
ForceNew: true,
36-
Description: "The name of the environment.",
3738
},
3839
"branch_pattern": {
39-
Type: schema.TypeString,
40-
Optional: true,
41-
ForceNew: false,
42-
ConflictsWith: []string{"tag_pattern"},
43-
Description: "The name pattern that branches must match in order to deploy to the environment.",
40+
Description: "The name pattern that branches must match in order to deploy to the environment.",
41+
Type: schema.TypeString,
42+
Optional: true,
43+
ForceNew: false,
44+
ExactlyOneOf: []string{"branch_pattern", "tag_pattern"},
45+
ValidateDiagFunc: func(i any, _ cty.Path) diag.Diagnostics {
46+
str, ok := i.(string)
47+
if ok && len(str) > 0 {
48+
return nil
49+
}
50+
return diag.Errorf("`branch_pattern` must be a valid non-empty string")
51+
},
4452
},
4553
"tag_pattern": {
46-
Type: schema.TypeString,
47-
Optional: true,
48-
ForceNew: false,
49-
ConflictsWith: []string{"branch_pattern"},
50-
Description: "The name pattern that tags must match in order to deploy to the environment.",
54+
Description: "The name pattern that tags must match in order to deploy to the environment.",
55+
Type: schema.TypeString,
56+
Optional: true,
57+
ForceNew: false,
58+
ExactlyOneOf: []string{"branch_pattern", "tag_pattern"},
59+
ValidateDiagFunc: func(i any, _ cty.Path) diag.Diagnostics {
60+
str, ok := i.(string)
61+
if ok && len(str) > 0 {
62+
return nil
63+
}
64+
return diag.Errorf("`tag_pattern` must be a valid non-empty string")
65+
},
5166
},
5267
},
5368
CustomizeDiff: customDeploymentPolicyDiffFunction,
5469
}
5570
}
5671

57-
func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.ResourceData, meta any) error {
72+
func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5873
client := meta.(*Owner).v3client
59-
ctx := context.Background()
6074

6175
owner := meta.(*Owner).name
6276
repoName := d.Get("repository").(string)
@@ -75,31 +89,30 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyCreate(d *schema.Resourc
7589
Type: github.String("tag"),
7690
}
7791
} else {
78-
return fmt.Errorf("exactly one of %q and %q must be specified", "branch_pattern", "tag_pattern")
92+
return diag.Errorf("only one of 'branch_pattern' or 'tag_pattern' must be specified")
7993
}
8094

8195
resultKey, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, &createData)
8296
if err != nil {
83-
return err
97+
return diag.FromErr(err)
8498
}
8599

86100
d.SetId(buildThreePartID(repoName, escapedEnvName, strconv.FormatInt(resultKey.GetID(), 10)))
87-
return resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d, meta)
101+
return nil
88102
}
89103

90-
func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceData, meta any) error {
104+
func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
91105
client := meta.(*Owner).v3client
92-
ctx := context.WithValue(context.Background(), ctxId, d.Id())
93106

94107
owner := meta.(*Owner).name
95108
repoName, envName, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
96109
if err != nil {
97-
return err
110+
return diag.FromErr(err)
98111
}
99112

100113
branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64)
101114
if err != nil {
102-
return err
115+
return diag.FromErr(err)
103116
}
104117

105118
branchPolicy, _, err := client.Repositories.GetDeploymentBranchPolicy(ctx, owner, repoName, envName, branchPolicyId)
@@ -116,7 +129,7 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceD
116129
return nil
117130
}
118131
}
119-
return err
132+
return diag.FromErr(err)
120133
}
121134

122135
if branchPolicy.GetType() == "branch" {
@@ -127,9 +140,8 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d *schema.ResourceD
127140
return nil
128141
}
129142

130-
func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.ResourceData, meta any) error {
143+
func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
131144
client := meta.(*Owner).v3client
132-
ctx := context.Background()
133145

134146
owner := meta.(*Owner).name
135147
repoName := d.Get("repository").(string)
@@ -139,12 +151,12 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.Resourc
139151
escapedEnvName := url.PathEscape(envName)
140152
_, _, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
141153
if err != nil {
142-
return err
154+
return diag.FromErr(err)
143155
}
144156

145157
branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64)
146158
if err != nil {
147-
return err
159+
return diag.FromErr(err)
148160
}
149161

150162
pattern := branchPattern
@@ -158,56 +170,39 @@ func resourceGithubRepositoryEnvironmentDeploymentPolicyUpdate(d *schema.Resourc
158170

159171
resultKey, _, err := client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, escapedEnvName, branchPolicyId, &updateData)
160172
if err != nil {
161-
return err
173+
return diag.FromErr(err)
162174
}
163175
d.SetId(buildThreePartID(repoName, escapedEnvName, strconv.FormatInt(resultKey.GetID(), 10)))
164-
return resourceGithubRepositoryEnvironmentDeploymentPolicyRead(d, meta)
176+
return nil
165177
}
166178

167-
func resourceGithubRepositoryEnvironmentDeploymentPolicyDelete(d *schema.ResourceData, meta any) error {
179+
func resourceGithubRepositoryEnvironmentDeploymentPolicyDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
168180
client := meta.(*Owner).v3client
169-
ctx := context.Background()
170181

171182
owner := meta.(*Owner).name
172183
repoName, envName, branchPolicyIdString, err := parseThreePartID(d.Id(), "repository", "environment", "branchPolicyId")
173184
if err != nil {
174-
return err
185+
return diag.FromErr(err)
175186
}
176187

177188
branchPolicyId, err := strconv.ParseInt(branchPolicyIdString, 10, 64)
178189
if err != nil {
179-
return err
190+
return diag.FromErr(err)
180191
}
181192

182193
_, err = client.Repositories.DeleteDeploymentBranchPolicy(ctx, owner, repoName, envName, branchPolicyId)
183194
if err != nil {
184-
return err
195+
return diag.FromErr(err)
185196
}
186197

187198
return nil
188199
}
189200

190201
func customDeploymentPolicyDiffFunction(_ context.Context, diff *schema.ResourceDiff, v any) error {
191-
oldBranchPattern, newBranchPattern := diff.GetChange("branch_pattern")
192-
193-
if oldBranchPattern != "" && newBranchPattern == "" {
202+
if diff.HasChange("branch_pattern") && diff.HasChange("tag_pattern") {
194203
if err := diff.ForceNew("branch_pattern"); err != nil {
195204
return err
196205
}
197-
}
198-
if oldBranchPattern == "" && newBranchPattern != "" {
199-
if err := diff.ForceNew("branch_pattern"); err != nil {
200-
return err
201-
}
202-
}
203-
204-
oldTagPattern, newTagPattern := diff.GetChange("tag_pattern")
205-
if oldTagPattern != "" && newTagPattern == "" {
206-
if err := diff.ForceNew("tag_pattern"); err != nil {
207-
return err
208-
}
209-
}
210-
if oldTagPattern == "" && newTagPattern != "" {
211206
if err := diff.ForceNew("tag_pattern"); err != nil {
212207
return err
213208
}

0 commit comments

Comments
 (0)