@@ -10,17 +10,18 @@ import (
1010 "strings"
1111
1212 "github.com/google/go-github/v81/github"
13+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1314 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415)
1516
1617func resourceGithubRepositoryWebhook () * schema.Resource {
1718 return & schema.Resource {
18- Create : resourceGithubRepositoryWebhookCreate ,
19- Read : resourceGithubRepositoryWebhookRead ,
20- Update : resourceGithubRepositoryWebhookUpdate ,
21- Delete : resourceGithubRepositoryWebhookDelete ,
19+ CreateContext : resourceGithubRepositoryWebhookCreate ,
20+ ReadContext : resourceGithubRepositoryWebhookRead ,
21+ UpdateContext : resourceGithubRepositoryWebhookUpdate ,
22+ DeleteContext : resourceGithubRepositoryWebhookDelete ,
2223 Importer : & schema.ResourceImporter {
23- State : func (d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
24+ StateContext : func (ctx context. Context , d * schema.ResourceData , meta any ) ([]* schema.ResourceData , error ) {
2425 parts := strings .Split (d .Id (), "/" )
2526 if len (parts ) != 2 {
2627 return nil , fmt .Errorf ("invalid ID specified: supplied ID must be written as <repository>/<webhook_id>" )
@@ -104,17 +105,16 @@ func resourceGithubRepositoryWebhookObject(d *schema.ResourceData) *github.Hook
104105 return hook
105106}
106107
107- func resourceGithubRepositoryWebhookCreate (d * schema.ResourceData , meta any ) error {
108+ func resourceGithubRepositoryWebhookCreate (ctx context. Context , d * schema.ResourceData , meta any ) diag. Diagnostics {
108109 client := meta .(* Owner ).v3client
109110
110111 owner := meta .(* Owner ).name
111112 repoName := d .Get ("repository" ).(string )
112113 hk := resourceGithubRepositoryWebhookObject (d )
113- ctx := context .Background ()
114114
115115 hook , _ , err := client .Repositories .CreateHook (ctx , owner , repoName , hk )
116116 if err != nil {
117- return err
117+ return diag . FromErr ( err )
118118 }
119119 d .SetId (strconv .FormatInt (hook .GetID (), 10 ))
120120
@@ -126,22 +126,22 @@ func resourceGithubRepositoryWebhookCreate(d *schema.ResourceData, meta any) err
126126 }
127127
128128 if err = d .Set ("configuration" , interfaceFromWebhookConfig (hook .Config )); err != nil {
129- return err
129+ return diag . FromErr ( err )
130130 }
131131
132- return resourceGithubRepositoryWebhookRead (d , meta )
132+ return resourceGithubRepositoryWebhookRead (ctx , d , meta )
133133}
134134
135- func resourceGithubRepositoryWebhookRead (d * schema.ResourceData , meta any ) error {
135+ func resourceGithubRepositoryWebhookRead (ctx context. Context , d * schema.ResourceData , meta any ) diag. Diagnostics {
136136 client := meta .(* Owner ).v3client
137137
138138 owner := meta .(* Owner ).name
139139 repoName := d .Get ("repository" ).(string )
140140 hookID , err := strconv .ParseInt (d .Id (), 10 , 64 )
141141 if err != nil {
142- return unconvertibleIdErr (d .Id (), err )
142+ return diag . FromErr ( unconvertibleIdErr (d .Id (), err ) )
143143 }
144- ctx : = context .WithValue (context . Background () , ctxId , d .Id ())
144+ ctx = context .WithValue (ctx , ctxId , d .Id ())
145145 if ! d .IsNewResource () {
146146 ctx = context .WithValue (ctx , ctxEtag , d .Get ("etag" ).(string ))
147147 }
@@ -160,16 +160,16 @@ func resourceGithubRepositoryWebhookRead(d *schema.ResourceData, meta any) error
160160 return nil
161161 }
162162 }
163- return err
163+ return diag . FromErr ( err )
164164 }
165165 if err = d .Set ("url" , hook .GetURL ()); err != nil {
166- return err
166+ return diag . FromErr ( err )
167167 }
168168 if err = d .Set ("active" , hook .GetActive ()); err != nil {
169- return err
169+ return diag . FromErr ( err )
170170 }
171171 if err = d .Set ("events" , hook .Events ); err != nil {
172- return err
172+ return diag . FromErr ( err )
173173 }
174174
175175 // GitHub returns the secret as a string of 8 astrisks "********"
@@ -185,43 +185,43 @@ func resourceGithubRepositoryWebhookRead(d *schema.ResourceData, meta any) error
185185 }
186186
187187 if err = d .Set ("configuration" , interfaceFromWebhookConfig (hook .Config )); err != nil {
188- return err
188+ return diag . FromErr ( err )
189189 }
190190
191191 return nil
192192}
193193
194- func resourceGithubRepositoryWebhookUpdate (d * schema.ResourceData , meta any ) error {
194+ func resourceGithubRepositoryWebhookUpdate (ctx context. Context , d * schema.ResourceData , meta any ) diag. Diagnostics {
195195 client := meta .(* Owner ).v3client
196196
197197 owner := meta .(* Owner ).name
198198 repoName := d .Get ("repository" ).(string )
199199 hk := resourceGithubRepositoryWebhookObject (d )
200200 hookID , err := strconv .ParseInt (d .Id (), 10 , 64 )
201201 if err != nil {
202- return unconvertibleIdErr (d .Id (), err )
202+ return diag . FromErr ( unconvertibleIdErr (d .Id (), err ) )
203203 }
204- ctx : = context .WithValue (context . Background () , ctxId , d .Id ())
204+ ctx = context .WithValue (ctx , ctxId , d .Id ())
205205
206206 _ , _ , err = client .Repositories .EditHook (ctx , owner , repoName , hookID , hk )
207207 if err != nil {
208- return err
208+ return diag . FromErr ( err )
209209 }
210210
211- return resourceGithubRepositoryWebhookRead (d , meta )
211+ return resourceGithubRepositoryWebhookRead (ctx , d , meta )
212212}
213213
214- func resourceGithubRepositoryWebhookDelete (d * schema.ResourceData , meta any ) error {
214+ func resourceGithubRepositoryWebhookDelete (ctx context. Context , d * schema.ResourceData , meta any ) diag. Diagnostics {
215215 client := meta .(* Owner ).v3client
216216
217217 owner := meta .(* Owner ).name
218218 repoName := d .Get ("repository" ).(string )
219219 hookID , err := strconv .ParseInt (d .Id (), 10 , 64 )
220220 if err != nil {
221- return unconvertibleIdErr (d .Id (), err )
221+ return diag . FromErr ( unconvertibleIdErr (d .Id (), err ) )
222222 }
223- ctx : = context .WithValue (context . Background () , ctxId , d .Id ())
223+ ctx = context .WithValue (ctx , ctxId , d .Id ())
224224
225225 _ , err = client .Repositories .DeleteHook (ctx , owner , repoName , hookID )
226- return handleArchivedRepoDelete (err , "repository webhook" , d .Id (), owner , repoName )
226+ return diag . FromErr ( handleArchivedRepoDelete (err , "repository webhook" , d .Id (), owner , repoName ) )
227227}
0 commit comments