|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/google/go-github/v81/github" |
| 12 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 16 | +) |
| 17 | + |
| 18 | +type GithubActionsOrganizationWorkflowPermissionsErrorResponse struct { |
| 19 | + Message string `json:"message"` |
| 20 | + Errors string `json:"errors"` |
| 21 | + DocumentationURL string `json:"documentation_url"` |
| 22 | + Status string `json:"status"` |
| 23 | +} |
| 24 | + |
| 25 | +func resourceGithubActionsOrganizationWorkflowPermissions() *schema.Resource { |
| 26 | + return &schema.Resource{ |
| 27 | + Description: "This resource allows you to manage GitHub Actions workflow permissions for a GitHub Organization account. This controls the default permissions granted to the GITHUB_TOKEN when running workflows and whether GitHub Actions can approve pull request reviews.\n\nYou must have organization admin access to use this resource.", |
| 28 | + CreateContext: resourceGithubActionsOrganizationWorkflowPermissionsCreateOrUpdate, |
| 29 | + ReadContext: resourceGithubActionsOrganizationWorkflowPermissionsRead, |
| 30 | + UpdateContext: resourceGithubActionsOrganizationWorkflowPermissionsCreateOrUpdate, |
| 31 | + DeleteContext: resourceGithubActionsOrganizationWorkflowPermissionsDelete, |
| 32 | + Importer: &schema.ResourceImporter{ |
| 33 | + StateContext: schema.ImportStatePassthroughContext, |
| 34 | + }, |
| 35 | + |
| 36 | + Schema: map[string]*schema.Schema{ |
| 37 | + "organization_slug": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + ForceNew: true, |
| 41 | + Description: "The slug of the Organization.", |
| 42 | + }, |
| 43 | + "default_workflow_permissions": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + Default: "read", |
| 47 | + Description: "The default workflow permissions granted to the GITHUB_TOKEN when running workflows in any repository in the organization. Can be 'read' or 'write'.", |
| 48 | + ValidateFunc: validation.StringInSlice([]string{"read", "write"}, false), |
| 49 | + }, |
| 50 | + "can_approve_pull_request_reviews": { |
| 51 | + Type: schema.TypeBool, |
| 52 | + Optional: true, |
| 53 | + Default: false, |
| 54 | + Description: "Whether GitHub Actions can approve pull request reviews in any repository in the organization.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func handleEditWorkflowPermissionsError(ctx context.Context, err error, resp *github.Response) diag.Diagnostics { |
| 61 | + var ghErr *github.ErrorResponse |
| 62 | + if errors.As(err, &ghErr) { |
| 63 | + 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 | + |
| 68 | + errorResponse := &GithubActionsOrganizationWorkflowPermissionsErrorResponse{} |
| 69 | + data, readError := io.ReadAll(resp.Body) |
| 70 | + if readError == nil && data != nil { |
| 71 | + unmarshalError := json.Unmarshal(data, errorResponse) |
| 72 | + if unmarshalError != nil { |
| 73 | + tflog.Error(ctx, "Failed to unmarshal error response", map[string]any{ |
| 74 | + "error": unmarshalError.Error(), |
| 75 | + }) |
| 76 | + return diag.FromErr(unmarshalError) |
| 77 | + } |
| 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 | + }) |
| 85 | + } |
| 86 | + 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\nerr: %w", errorResponse.Message, errorResponse.Errors, errorResponse.DocumentationURL, errorResponse.Status, err)) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + tflog.Trace(ctx, "Returning generic error", map[string]any{ |
| 91 | + "error": err.Error(), |
| 92 | + }) |
| 93 | + |
| 94 | + return diag.FromErr(err) |
| 95 | +} |
| 96 | + |
| 97 | +func 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 | + |
| 102 | + client := meta.(*Owner).v3client |
| 103 | + |
| 104 | + organizationSlug := d.Get("organization_slug").(string) |
| 105 | + d.SetId(organizationSlug) |
| 106 | + |
| 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 | + |
| 117 | + workflowPerms := github.DefaultWorkflowPermissionOrganization{} |
| 118 | + |
| 119 | + if v, ok := d.GetOk("default_workflow_permissions"); ok { |
| 120 | + workflowPerms.DefaultWorkflowPermissions = github.Ptr(v.(string)) |
| 121 | + } |
| 122 | + |
| 123 | + if v, ok := d.GetOk("can_approve_pull_request_reviews"); ok { |
| 124 | + workflowPerms.CanApprovePullRequestReviews = github.Ptr(v.(bool)) |
| 125 | + } |
| 126 | + |
| 127 | + tflog.Debug(ctx, "Calling GitHub API to update workflow permissions", map[string]any{ |
| 128 | + "organization_slug": organizationSlug, |
| 129 | + "default_workflow_permissions": workflowPerms.DefaultWorkflowPermissions, |
| 130 | + "can_approve_pull_request_reviews": workflowPerms.CanApprovePullRequestReviews, |
| 131 | + }) |
| 132 | + _, resp, err := client.Actions.UpdateDefaultWorkflowPermissionsInOrganization(ctx, organizationSlug, workflowPerms) |
| 133 | + if err != nil { |
| 134 | + return handleEditWorkflowPermissionsError(ctx, err, resp) |
| 135 | + } |
| 136 | + |
| 137 | + tflog.Trace(ctx, "Exiting Create/Update workflow permissions successfully", map[string]any{ |
| 138 | + "organization_slug": organizationSlug, |
| 139 | + }) |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func resourceGithubActionsOrganizationWorkflowPermissionsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 144 | + tflog.Trace(ctx, "Entering Read workflow permissions", map[string]any{ |
| 145 | + "organization_slug": d.Id(), |
| 146 | + }) |
| 147 | + |
| 148 | + client := meta.(*Owner).v3client |
| 149 | + |
| 150 | + organizationSlug := d.Id() |
| 151 | + tflog.Debug(ctx, "Calling GitHub API to read workflow permissions", map[string]any{ |
| 152 | + "organization_slug": organizationSlug, |
| 153 | + }) |
| 154 | + |
| 155 | + workflowPerms, _, err := client.Actions.GetDefaultWorkflowPermissionsInOrganization(ctx, organizationSlug) |
| 156 | + if err != nil { |
| 157 | + return diag.FromErr(err) |
| 158 | + } |
| 159 | + |
| 160 | + tflog.Debug(ctx, "Retrieved workflow permissions from API", map[string]any{ |
| 161 | + "organization_slug": organizationSlug, |
| 162 | + "default_workflow_permissions": workflowPerms.DefaultWorkflowPermissions, |
| 163 | + "can_approve_pull_request_reviews": workflowPerms.CanApprovePullRequestReviews, |
| 164 | + }) |
| 165 | + |
| 166 | + if err := d.Set("organization_slug", organizationSlug); err != nil { |
| 167 | + return diag.FromErr(err) |
| 168 | + } |
| 169 | + if err := d.Set("default_workflow_permissions", workflowPerms.DefaultWorkflowPermissions); err != nil { |
| 170 | + return diag.FromErr(err) |
| 171 | + } |
| 172 | + if err := d.Set("can_approve_pull_request_reviews", workflowPerms.CanApprovePullRequestReviews); err != nil { |
| 173 | + return diag.FromErr(err) |
| 174 | + } |
| 175 | + |
| 176 | + tflog.Trace(ctx, "Exiting Read workflow permissions successfully", map[string]any{ |
| 177 | + "organization_slug": organizationSlug, |
| 178 | + }) |
| 179 | + |
| 180 | + return nil |
| 181 | +} |
| 182 | + |
| 183 | +func resourceGithubActionsOrganizationWorkflowPermissionsDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 184 | + tflog.Trace(ctx, "Entering Delete workflow permissions", map[string]any{ |
| 185 | + "organization_slug": d.Id(), |
| 186 | + }) |
| 187 | + |
| 188 | + client := meta.(*Owner).v3client |
| 189 | + |
| 190 | + organizationSlug := d.Id() |
| 191 | + tflog.Info(ctx, "Deleting organization workflow permissions (resetting to defaults)", map[string]any{ |
| 192 | + "organization_slug": organizationSlug, |
| 193 | + }) |
| 194 | + |
| 195 | + // Reset to safe defaults |
| 196 | + workflowPerms := github.DefaultWorkflowPermissionOrganization{ |
| 197 | + DefaultWorkflowPermissions: github.Ptr("read"), |
| 198 | + CanApprovePullRequestReviews: github.Ptr(false), |
| 199 | + } |
| 200 | + |
| 201 | + tflog.Debug(ctx, "Using safe default values", map[string]any{ |
| 202 | + "default_workflow_permissions": "read", |
| 203 | + "can_approve_pull_request_reviews": false, |
| 204 | + }) |
| 205 | + |
| 206 | + tflog.Debug(ctx, "Calling GitHub API to reset workflow permissions", map[string]any{ |
| 207 | + "organization_slug": organizationSlug, |
| 208 | + "workflow_permissions": workflowPerms, |
| 209 | + }) |
| 210 | + |
| 211 | + _, resp, err := client.Actions.UpdateDefaultWorkflowPermissionsInOrganization(ctx, organizationSlug, workflowPerms) |
| 212 | + if err != nil { |
| 213 | + return handleEditWorkflowPermissionsError(ctx, err, resp) |
| 214 | + } |
| 215 | + |
| 216 | + tflog.Trace(ctx, "Exiting Delete workflow permissions successfully", map[string]any{ |
| 217 | + "organization_slug": organizationSlug, |
| 218 | + }) |
| 219 | + |
| 220 | + return nil |
| 221 | +} |
0 commit comments