@@ -3,60 +3,74 @@ package github
33import (
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
1617func 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
190201func 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