@@ -2,7 +2,6 @@ package provider
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"log"
8
7
"strconv"
@@ -13,9 +12,6 @@ import (
13
12
gitlab "github.com/xanzy/go-gitlab"
14
13
)
15
14
16
- // https://docs.gitlab.com/ee/api/merge_request_approvals.html#create-project-level-rule
17
- var errApprovalRuleNotFound = errors .New ("approval rule not found" )
18
-
19
15
var _ = registerResource ("gitlab_project_approval_rule" , func () * schema.Resource {
20
16
var validRuleTypeValues = []string {
21
17
"regular" ,
@@ -119,21 +115,28 @@ func resourceGitlabProjectApprovalRuleCreate(ctx context.Context, d *schema.Reso
119
115
func resourceGitlabProjectApprovalRuleRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
120
116
log .Printf ("[DEBUG] read gitlab project-level rule %s" , d .Id ())
121
117
122
- projectID , _ , err := parseTwoPartID (d .Id ())
118
+ projectID , parsedRuleID , err := parseTwoPartID (d .Id ())
123
119
if err != nil {
124
120
return diag .FromErr (err )
125
121
}
126
- d .Set ("project" , projectID )
122
+ ruleID , err := strconv .Atoi (parsedRuleID )
123
+ if err != nil {
124
+ return diag .FromErr (err )
125
+ }
126
+
127
+ client := meta .(* gitlab.Client )
127
128
128
- rule , err := getApprovalRuleByID ( ctx , meta .( * gitlab. Client ), d . Id ( ))
129
+ rule , _ , err := client . Projects . GetProjectApprovalRule ( projectID , ruleID , gitlab . WithContext ( ctx ))
129
130
if err != nil {
130
- if errors .Is (err , errApprovalRuleNotFound ) {
131
+ if is404 (err ) {
132
+ log .Printf ("[DEBUG] no project-level rule %s found, removing from state" , d .Id ())
131
133
d .SetId ("" )
132
134
return nil
133
135
}
134
136
return diag .FromErr (err )
135
137
}
136
138
139
+ d .Set ("project" , projectID )
137
140
d .Set ("name" , rule .Name )
138
141
d .Set ("approvals_required" , rule .ApprovalsRequired )
139
142
d .Set ("rule_type" , rule .RuleType )
@@ -207,35 +210,6 @@ func resourceGitlabProjectApprovalRuleDelete(ctx context.Context, d *schema.Reso
207
210
return nil
208
211
}
209
212
210
- // getApprovalRuleByID checks the list of rules and finds the one that matches our rule ID.
211
- func getApprovalRuleByID (ctx context.Context , client * gitlab.Client , id string ) (* gitlab.ProjectApprovalRule , error ) {
212
- projectID , ruleID , err := parseTwoPartID (id )
213
- if err != nil {
214
- return nil , err
215
- }
216
-
217
- ruleIDInt , err := strconv .Atoi (ruleID )
218
- if err != nil {
219
- return nil , err
220
- }
221
-
222
- log .Printf ("[DEBUG] read approval rules for project %s" , projectID )
223
-
224
- rules , _ , err := client .Projects .GetProjectApprovalRules (projectID , gitlab .WithContext (ctx ))
225
- if err != nil {
226
- return nil , err
227
- }
228
-
229
- for _ , r := range rules {
230
- if r .ID == ruleIDInt {
231
- log .Printf ("[DEBUG] found project-level rule %+v" , r )
232
- return r , nil
233
- }
234
- }
235
-
236
- return nil , errApprovalRuleNotFound
237
- }
238
-
239
213
// flattenApprovalRuleUserIDs flattens a list of approval user ids into a list
240
214
// of ints for storage in state.
241
215
func flattenApprovalRuleUserIDs (users []* gitlab.BasicUser ) []int {
0 commit comments