@@ -57,30 +57,63 @@ func resourceGithubActionsOrganizationWorkflowPermissions() *schema.Resource {
5757 }
5858}
5959
60- func handleEditWorkflowPermissionsError (err error , resp * github.Response ) diag.Diagnostics {
60+ func handleEditWorkflowPermissionsError (ctx context. Context , err error , resp * github.Response ) diag.Diagnostics {
6161 var ghErr * github.ErrorResponse
6262 if errors .As (err , & ghErr ) {
6363 if ghErr .Response .StatusCode == http .StatusConflict {
64+ tflog .Info (ctx , "Detected conflict with workflow permissions" , map [string ]any {
65+ "status_code" : ghErr .Response .StatusCode ,
66+ })
67+
6468 errorResponse := & GithubActionsOrganizationWorkflowPermissionsErrorResponse {}
6569 data , readError := io .ReadAll (resp .Body )
6670 if readError == nil && data != nil {
6771 unmarshalError := json .Unmarshal (data , errorResponse )
6872 if unmarshalError != nil {
73+ tflog .Error (ctx , "Failed to unmarshal error response" , map [string ]any {
74+ "error" : unmarshalError .Error (),
75+ })
6976 return diag .FromErr (unmarshalError )
7077 }
78+
79+ tflog .Debug (ctx , "Parsed workflow permissions conflict error" , map [string ]any {
80+ "message" : errorResponse .Message ,
81+ "errors" : errorResponse .Errors ,
82+ "documentation_url" : errorResponse .DocumentationURL ,
83+ "status" : errorResponse .Status ,
84+ })
7185 }
7286 return diag .FromErr (fmt .Errorf ("you are trying to modify a value restricted by the Enterprise's settings.\n Message: %s\n Errors: %s\n Documentation URL: %s\n Status: %s\n err: %w" , errorResponse .Message , errorResponse .Errors , errorResponse .DocumentationURL , errorResponse .Status , err ))
7387 }
7488 }
89+
90+ tflog .Trace (ctx , "Returning generic error" , map [string ]any {
91+ "error" : err .Error (),
92+ })
93+
7594 return diag .FromErr (err )
7695}
7796
7897func resourceGithubActionsOrganizationWorkflowPermissionsCreateOrUpdate (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
98+ tflog .Trace (ctx , "Entering Create/Update workflow permissions" , map [string ]any {
99+ "organization_slug" : d .Get ("organization_slug" ).(string ),
100+ })
101+
79102 client := meta .(* Owner ).v3client
80103
81104 organizationSlug := d .Get ("organization_slug" ).(string )
82105 d .SetId (organizationSlug )
83106
107+ if d .IsNewResource () {
108+ tflog .Info (ctx , "Creating organization workflow permissions" , map [string ]any {
109+ "organization_slug" : organizationSlug ,
110+ })
111+ } else {
112+ tflog .Info (ctx , "Updating organization workflow permissions" , map [string ]any {
113+ "organization_slug" : organizationSlug ,
114+ })
115+ }
116+
84117 workflowPerms := github.DefaultWorkflowPermissionOrganization {}
85118
86119 if v , ok := d .GetOk ("default_workflow_permissions" ); ok {
@@ -91,25 +124,36 @@ func resourceGithubActionsOrganizationWorkflowPermissionsCreateOrUpdate(ctx cont
91124 workflowPerms .CanApprovePullRequestReviews = github .Ptr (v .(bool ))
92125 }
93126
94- tflog .Debug (ctx , "Updating workflow permissions for Organization " , map [string ]any {
127+ tflog .Debug (ctx , "Calling GitHub API to update workflow permissions " , map [string ]any {
95128 "organization_slug" : organizationSlug ,
96129 "default_workflow_permissions" : workflowPerms .DefaultWorkflowPermissions ,
97130 "can_approve_pull_request_reviews" : workflowPerms .CanApprovePullRequestReviews ,
98131 })
99132 _ , resp , err := client .Actions .UpdateDefaultWorkflowPermissionsInOrganization (ctx , organizationSlug , workflowPerms )
100133 if err != nil {
101- return handleEditWorkflowPermissionsError (err , resp )
134+ return handleEditWorkflowPermissionsError (ctx , err , resp )
102135 }
103136
137+ tflog .Trace (ctx , "GitHub API call completed successfully" , map [string ]any {
138+ "organization_slug" : organizationSlug ,
139+ })
140+
104141 // Calling read is necessary as the Update API returns 204 with Empty Body on success
142+ tflog .Trace (ctx , "Exiting Create/Update workflow permissions successfully" , map [string ]any {
143+ "organization_slug" : organizationSlug ,
144+ })
105145 return resourceGithubActionsOrganizationWorkflowPermissionsRead (ctx , d , meta )
106146}
107147
108148func resourceGithubActionsOrganizationWorkflowPermissionsRead (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
149+ tflog .Trace (ctx , "Entering Read workflow permissions" , map [string ]any {
150+ "organization_slug" : d .Id (),
151+ })
152+
109153 client := meta .(* Owner ).v3client
110154
111155 organizationSlug := d .Id ()
112- tflog .Debug (ctx , "Reading workflow permissions for Organization " , map [string ]any {
156+ tflog .Debug (ctx , "Calling GitHub API to read workflow permissions " , map [string ]any {
113157 "organization_slug" : organizationSlug ,
114158 })
115159
@@ -118,6 +162,18 @@ func resourceGithubActionsOrganizationWorkflowPermissionsRead(ctx context.Contex
118162 return diag .FromErr (err )
119163 }
120164
165+ tflog .Debug (ctx , "Retrieved workflow permissions from API" , map [string ]any {
166+ "organization_slug" : organizationSlug ,
167+ "default_workflow_permissions" : workflowPerms .DefaultWorkflowPermissions ,
168+ "can_approve_pull_request_reviews" : workflowPerms .CanApprovePullRequestReviews ,
169+ })
170+
171+ tflog .Trace (ctx , "Setting state values" , map [string ]any {
172+ "organization_slug" : organizationSlug ,
173+ "default_workflow_permissions" : workflowPerms .DefaultWorkflowPermissions ,
174+ "can_approve_pull_request_reviews" : workflowPerms .CanApprovePullRequestReviews ,
175+ })
176+
121177 if err := d .Set ("organization_slug" , organizationSlug ); err != nil {
122178 return diag .FromErr (err )
123179 }
@@ -128,14 +184,22 @@ func resourceGithubActionsOrganizationWorkflowPermissionsRead(ctx context.Contex
128184 return diag .FromErr (err )
129185 }
130186
187+ tflog .Trace (ctx , "Exiting Read workflow permissions successfully" , map [string ]any {
188+ "organization_slug" : organizationSlug ,
189+ })
190+
131191 return nil
132192}
133193
134194func resourceGithubActionsOrganizationWorkflowPermissionsDelete (ctx context.Context , d * schema.ResourceData , meta any ) diag.Diagnostics {
195+ tflog .Trace (ctx , "Entering Delete workflow permissions" , map [string ]any {
196+ "organization_slug" : d .Id (),
197+ })
198+
135199 client := meta .(* Owner ).v3client
136200
137201 organizationSlug := d .Id ()
138- tflog .Debug (ctx , "Resetting workflow permissions to defaults for Organization " , map [string ]any {
202+ tflog .Info (ctx , "Deleting organization workflow permissions (resetting to defaults) " , map [string ]any {
139203 "organization_slug" : organizationSlug ,
140204 })
141205
@@ -145,10 +209,27 @@ func resourceGithubActionsOrganizationWorkflowPermissionsDelete(ctx context.Cont
145209 CanApprovePullRequestReviews : github .Ptr (false ),
146210 }
147211
212+ tflog .Debug (ctx , "Using safe default values" , map [string ]any {
213+ "default_workflow_permissions" : "read" ,
214+ "can_approve_pull_request_reviews" : false ,
215+ })
216+
217+ tflog .Debug (ctx , "Calling GitHub API to reset workflow permissions" , map [string ]any {
218+ "organization_slug" : organizationSlug ,
219+ })
220+
148221 _ , resp , err := client .Actions .UpdateDefaultWorkflowPermissionsInOrganization (ctx , organizationSlug , workflowPerms )
149222 if err != nil {
150- return handleEditWorkflowPermissionsError (err , resp )
223+ return handleEditWorkflowPermissionsError (ctx , err , resp )
151224 }
152225
226+ tflog .Trace (ctx , "GitHub API call completed successfully" , map [string ]any {
227+ "organization_slug" : organizationSlug ,
228+ })
229+
230+ tflog .Trace (ctx , "Exiting Delete workflow permissions successfully" , map [string ]any {
231+ "organization_slug" : organizationSlug ,
232+ })
233+
153234 return nil
154235}
0 commit comments