1
1
package gitlab
2
2
3
3
import (
4
+ "context"
4
5
"fmt"
5
6
"log"
6
7
8
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8
10
"github.com/mitchellh/hashstructure"
9
11
"github.com/xanzy/go-gitlab"
10
12
)
11
13
12
14
func dataSourceGitlabProjectProtectedBranches () * schema.Resource {
13
15
return & schema.Resource {
14
- Read : dataSourceGitlabProjectProtectedBranchesRead ,
16
+ ReadContext : dataSourceGitlabProjectProtectedBranchesRead ,
15
17
Schema : map [string ]* schema.Schema {
16
18
"project_id" : {
17
19
Type : schema .TypeString ,
@@ -58,26 +60,26 @@ type stateProtectedBranch struct {
58
60
CodeOwnerApprovalRequired bool `json:"code_owner_approval_required,omitempty" mapstructure:"code_owner_approval_required,omitempty"`
59
61
}
60
62
61
- func dataSourceGitlabProjectProtectedBranchesRead (d * schema.ResourceData , meta interface {}) error {
63
+ func dataSourceGitlabProjectProtectedBranchesRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
62
64
client := meta .(* gitlab.Client )
63
65
64
66
log .Printf ("[INFO] Reading Gitlab protected branch" )
65
67
66
68
project := d .Get ("project_id" )
67
69
68
- projectObject , _ , err := client .Projects .GetProject (project , & gitlab.GetProjectOptions {})
70
+ projectObject , _ , err := client .Projects .GetProject (project , & gitlab.GetProjectOptions {}, gitlab . WithContext ( ctx ) )
69
71
if err != nil {
70
- return err
72
+ return diag . FromErr ( err )
71
73
}
72
74
73
75
allProtectedBranches := make ([]stateProtectedBranch , 0 )
74
76
totalPages := - 1
75
77
opts := & gitlab.ListProtectedBranchesOptions {}
76
78
for opts .Page = 0 ; opts .Page != totalPages ; opts .Page ++ {
77
79
// Get protected branch by project ID/path and branch name
78
- pbs , resp , err := client .ProtectedBranches .ListProtectedBranches (project , opts )
80
+ pbs , resp , err := client .ProtectedBranches .ListProtectedBranches (project , opts , gitlab . WithContext ( ctx ) )
79
81
if err != nil {
80
- return err
82
+ return diag . FromErr ( err )
81
83
}
82
84
totalPages = resp .TotalPages
83
85
for _ , pb := range pbs {
@@ -94,12 +96,12 @@ func dataSourceGitlabProjectProtectedBranchesRead(d *schema.ResourceData, meta i
94
96
95
97
// lintignore:R004 // TODO: Resolve this tfproviderlint issue
96
98
if err := d .Set ("protected_branches" , allProtectedBranches ); err != nil {
97
- return err
99
+ return diag . FromErr ( err )
98
100
}
99
101
100
102
h , err := hashstructure .Hash (* opts , nil )
101
103
if err != nil {
102
- return err
104
+ return diag . FromErr ( err )
103
105
}
104
106
105
107
d .SetId (fmt .Sprintf ("%d-%d" , projectObject .ID , h ))
0 commit comments