forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_repository_file.go
More file actions
553 lines (479 loc) · 16.6 KB
/
resource_github_repository_file.go
File metadata and controls
553 lines (479 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package github
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/google/go-github/v82/github"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubRepositoryFile() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGithubRepositoryFileCreate,
ReadContext: resourceGithubRepositoryFileRead,
UpdateContext: resourceGithubRepositoryFileUpdate,
DeleteContext: resourceGithubRepositoryFileDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceGithubRepositoryFileImport,
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceGithubRepositoryFileV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceGithubRepositoryFileStateUpgradeV0,
Version: 0,
},
},
Description: "This resource allows you to create and manage files within a GitHub repository.",
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "The repository name",
},
"repository_id": {
Type: schema.TypeInt,
Computed: true,
Description: "The repository ID",
},
"file": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The file path to manage",
},
"content": {
Type: schema.TypeString,
Required: true,
Description: "The file's content",
},
"branch": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The branch name, defaults to the repository's default branch",
},
"ref": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the commit/branch/tag",
ForceNew: true,
},
"commit_sha": {
Type: schema.TypeString,
Computed: true,
Description: "The SHA of the commit that modified the file",
},
"commit_message": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit message when creating, updating or deleting the file",
},
"commit_author": {
Type: schema.TypeString,
Optional: true,
Computed: false,
Description: "The commit author name, defaults to the authenticated user's name. GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App. ",
RequiredWith: []string{"commit_email"},
},
"commit_email": {
Type: schema.TypeString,
Optional: true,
Computed: false,
Description: "The commit author email address, defaults to the authenticated user's email address. GitHub app users may omit author and email information so GitHub can verify commits as the GitHub App.",
RequiredWith: []string{"commit_author"},
},
"sha": {
Type: schema.TypeString,
Computed: true,
Description: "The blob SHA of the file",
},
"overwrite_on_create": {
Type: schema.TypeBool,
Optional: true,
Description: "Enable overwriting existing files, defaults to \"false\"",
Default: false,
},
"autocreate_branch": {
Type: schema.TypeBool,
Optional: true,
Description: "Automatically create the branch if it could not be found. Subsequent reads if the branch is deleted will occur from 'autocreate_branch_source_branch'",
Default: false,
DiffSuppressFunc: autoBranchDiffSuppressFunc,
},
"autocreate_branch_source_branch": {
Type: schema.TypeString,
Default: "main",
Optional: true,
Description: "The branch name to start from, if 'autocreate_branch' is set. Defaults to 'main'.",
RequiredWith: []string{"autocreate_branch"},
DiffSuppressFunc: autoBranchDiffSuppressFunc,
},
"autocreate_branch_source_sha": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The commit hash to start from, if 'autocreate_branch' is set. Defaults to the tip of 'autocreate_branch_source_branch'. If provided, 'autocreate_branch_source_branch' is ignored.",
RequiredWith: []string{"autocreate_branch"},
DiffSuppressFunc: autoBranchDiffSuppressFunc,
},
},
CustomizeDiff: diffRepository,
}
}
func resourceGithubRepositoryFileOptions(d *schema.ResourceData) *github.RepositoryContentFileOptions {
opts := &github.RepositoryContentFileOptions{
Content: []byte(d.Get("content").(string)),
}
if branch, ok := d.GetOk("branch"); ok {
opts.Branch = github.Ptr(branch.(string))
}
if commitMessage, hasCommitMessage := d.GetOk("commit_message"); hasCommitMessage {
opts.Message = github.Ptr(commitMessage.(string))
}
if SHA, hasSHA := d.GetOk("sha"); hasSHA {
opts.SHA = github.Ptr(SHA.(string))
}
commitAuthor, hasCommitAuthor := d.GetOk("commit_author")
commitEmail, hasCommitEmail := d.GetOk("commit_email")
if hasCommitAuthor && hasCommitEmail {
name := github.Ptr(commitAuthor.(string))
mail := github.Ptr(commitEmail.(string))
opts.Author = &github.CommitAuthor{Name: name, Email: mail}
opts.Committer = &github.CommitAuthor{Name: name, Email: mail}
}
return opts
}
func resourceGithubRepositoryFileCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
file := d.Get("file").(string)
ctx = tflog.SetField(ctx, "repository", repo)
ctx = tflog.SetField(ctx, "file", file)
ctx = tflog.SetField(ctx, "owner", owner)
checkOpt := github.RepositoryContentGetOptions{}
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
if err != nil {
return diag.FromErr(err)
}
var branch string
if branchFieldVal, ok := d.GetOk("branch"); ok {
branch = branchFieldVal.(string)
tflog.Debug(ctx, "Using explicitly set branch", map[string]any{
"branch": branch,
})
if err := checkRepositoryBranchExists(ctx, client, owner, repo, branch); err != nil {
if d.Get("autocreate_branch").(bool) {
if err := resourceGithubRepositoryFileCreateBranch(ctx, d, meta); err != nil {
return diag.FromErr(err)
}
} else {
return diag.FromErr(err)
}
}
checkOpt.Ref = branch
} else {
branch = repoInfo.GetDefaultBranch()
}
if err := d.Set("repository_id", int(repoInfo.GetID())); err != nil {
return diag.FromErr(err)
}
opts := resourceGithubRepositoryFileOptions(d)
if opts.Message == nil {
opts.Message = github.Ptr(fmt.Sprintf("Add %s", file))
}
tflog.Debug(ctx, "Checking if overwriting a repository file")
fileContent, _, resp, err := client.Repositories.GetContents(ctx, owner, repo, file, &checkOpt)
if err != nil {
if resp != nil {
if resp.StatusCode != http.StatusNotFound {
// 404 is a valid response if the file does not exist
return diag.FromErr(err)
}
} else {
// Response should be non-nil
return diag.FromErr(err)
}
}
if fileContent != nil {
if d.Get("overwrite_on_create").(bool) {
// Overwrite existing file if requested by configuring the options for
// `client.Repositories.CreateFile` to match the existing file's SHA
opts.SHA = fileContent.SHA
} else {
// Error if overwriting a file is not requested
return diag.Errorf("refusing to overwrite existing file: configure `overwrite_on_create` to `true` to override")
}
}
tflog.Debug(ctx, "Creating or overwriting a repository file")
// Create a new or overwritten file
create, _, err := client.Repositories.CreateFile(ctx, owner, repo, file, opts)
if err != nil {
return diag.FromErr(err)
}
if err := d.Set("branch", branch); err != nil {
return diag.FromErr(err)
}
newResourceID, err := buildID(repo, file, branch)
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "Setting ID", map[string]any{
"id": newResourceID,
})
d.SetId(newResourceID)
if err = d.Set("commit_sha", create.GetSHA()); err != nil {
return diag.FromErr(err)
}
return resourceGithubRepositoryFileRead(ctx, d, meta)
}
func resourceGithubRepositoryFileRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName, ok := d.Get("repository").(string)
if !ok {
return diag.FromErr(fmt.Errorf("repository not found or is not a string"))
}
file, ok := d.Get("file").(string)
if !ok {
return diag.FromErr(fmt.Errorf("file not found or is not a string"))
}
ctx = tflog.SetField(ctx, "repository", repoName)
ctx = tflog.SetField(ctx, "file", file)
ctx = tflog.SetField(ctx, "owner", owner)
opts := &github.RepositoryContentGetOptions{}
branch := d.Get("branch").(string)
if err := checkRepositoryBranchExists(ctx, client, owner, repoName, branch); err != nil {
if d.Get("autocreate_branch").(bool) {
branch = d.Get("autocreate_branch_source_branch").(string)
} else {
tflog.Info(ctx, "Removing repository path from state because the branch no longer exists in GitHub")
d.SetId("")
return nil
}
}
opts.Ref = branch
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repoName, file, opts)
if err != nil {
var errorResponse *github.ErrorResponse
if errors.As(err, &errorResponse) && errorResponse.Response.StatusCode == http.StatusTooManyRequests {
return diag.FromErr(err)
}
}
if fc == nil {
tflog.Info(ctx, "Removing repository path from state because it no longer exists in GitHub")
d.SetId("")
return nil
}
content, err := fc.GetContent()
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("content", content); err != nil {
return diag.FromErr(err)
}
if err = d.Set("sha", fc.GetSHA()); err != nil {
return diag.FromErr(err)
}
var commit *github.RepositoryCommit
parsedUrl, err := url.Parse(fc.GetURL())
if err != nil {
return diag.FromErr(err)
}
parsedQuery, err := url.ParseQuery(parsedUrl.RawQuery)
if err != nil {
return diag.FromErr(err)
}
ref := parsedQuery["ref"][0]
if err = d.Set("ref", ref); err != nil {
return diag.FromErr(err)
}
// Use the SHA to lookup the commit info if we know it, otherwise loop through commits
if sha, ok := d.GetOk("commit_sha"); ok {
tflog.Debug(ctx, "Using known commit SHA", map[string]any{
"commit_sha": sha.(string),
})
commit, _, err = client.Repositories.GetCommit(ctx, owner, repoName, sha.(string), nil)
} else {
tflog.Debug(ctx, "Commit SHA unknown for file, looking for commit")
commit, err = getFileCommit(ctx, client, owner, repoName, file, ref)
}
if err != nil {
return diag.FromErr(err)
}
tflog.Debug(ctx, "Found file in commit", map[string]any{
"commit_sha": commit.GetSHA(),
})
if err = d.Set("commit_sha", commit.GetSHA()); err != nil {
return diag.FromErr(err)
}
commit_author := commit.Commit.GetCommitter().GetName()
commit_email := commit.Commit.GetCommitter().GetEmail()
_, hasCommitAuthor := d.GetOk("commit_author")
_, hasCommitEmail := d.GetOk("commit_email")
// read from state if author+email is set explicitly, and if it was not github signing it for you previously
if commit_author != "GitHub" && commit_email != "[email protected]" && hasCommitAuthor && hasCommitEmail {
if err = d.Set("commit_author", commit_author); err != nil {
return diag.FromErr(err)
}
if err = d.Set("commit_email", commit_email); err != nil {
return diag.FromErr(err)
}
}
if err = d.Set("commit_message", commit.GetCommit().GetMessage()); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceGithubRepositoryFileUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
file := d.Get("file").(string)
ctx = tflog.SetField(ctx, "repository", repo)
ctx = tflog.SetField(ctx, "file", file)
ctx = tflog.SetField(ctx, "owner", owner)
branch := d.Get("branch").(string)
if err := checkRepositoryBranchExists(ctx, client, owner, repo, branch); err != nil {
if d.Get("autocreate_branch").(bool) {
if err := resourceGithubRepositoryFileCreateBranch(ctx, d, meta); err != nil {
return diag.FromErr(err)
}
} else {
return diag.FromErr(err)
}
}
opts := resourceGithubRepositoryFileOptions(d)
if *opts.Message == fmt.Sprintf("Add %s", file) {
opts.Message = github.Ptr(fmt.Sprintf("Update %s", file))
}
create, _, err := client.Repositories.CreateFile(ctx, owner, repo, file, opts)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("commit_sha", create.GetSHA()); err != nil {
return diag.FromErr(err)
}
return resourceGithubRepositoryFileRead(ctx, d, meta)
}
func resourceGithubRepositoryFileDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repo := d.Get("repository").(string)
file := d.Get("file").(string)
opts := resourceGithubRepositoryFileOptions(d)
if *opts.Message == fmt.Sprintf("Add %s", file) {
opts.Message = github.Ptr(fmt.Sprintf("Delete %s", file))
}
branch := d.Get("branch").(string)
if err := checkRepositoryBranchExists(ctx, client, owner, repo, branch); err != nil {
if d.Get("autocreate_branch").(bool) {
if err := resourceGithubRepositoryFileCreateBranch(ctx, d, meta); err != nil {
return diag.FromErr(err)
}
} else {
return diag.FromErr(err)
}
}
opts.Branch = github.Ptr(branch)
_, _, err := client.Repositories.DeleteFile(ctx, owner, repo, file, opts)
return diag.FromErr(handleArchivedRepoDelete(err, "repository file", file, owner, repo))
}
func autoBranchDiffSuppressFunc(k, _, _ string, d *schema.ResourceData) bool {
if !d.Get("autocreate_branch").(bool) {
switch k {
case "autocreate_branch", "autocreate_branch_source_branch", "autocreate_branch_source_sha":
return true
}
}
return false
}
func resourceGithubRepositoryFileImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
repo, filePath, branch, err := parseID3(d.Id())
if err != nil {
return nil, fmt.Errorf("invalid ID specified. Supplied ID must be written as <repository>:<file path>: (when branch is default) or <repository>:<file path>:<branch>. %w", err)
}
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
opts := &github.RepositoryContentGetOptions{}
repoInfo, _, err := client.Repositories.Get(ctx, owner, repo)
if err != nil {
return nil, err
}
if branch != "" {
opts.Ref = branch
if err := d.Set("branch", branch); err != nil {
return nil, err
}
} else {
defaultBranch := repoInfo.GetDefaultBranch()
branch = defaultBranch
if err := d.Set("branch", branch); err != nil {
return nil, err
}
}
if err := d.Set("repository_id", int(repoInfo.GetID())); err != nil {
return nil, err
}
fc, _, _, err := client.Repositories.GetContents(ctx, owner, repo, filePath, opts)
if err != nil {
return nil, err
}
if fc == nil {
return nil, fmt.Errorf("filePath %s is not a file in repository %s/%s or repository is not readable", filePath, owner, repo)
}
if err := d.Set("repository", repo); err != nil {
return nil, err
}
if err := d.Set("file", filePath); err != nil {
return nil, err
}
newResourceID, err := buildID(repo, filePath, branch)
if err != nil {
return nil, err
}
tflog.Debug(ctx, "Setting ID", map[string]any{
"id": newResourceID,
})
d.SetId(newResourceID)
if err = d.Set("overwrite_on_create", false); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}
func resourceGithubRepositoryFileCreateBranch(ctx context.Context, d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
branch := d.Get("branch").(string)
repo := d.Get("repository").(string)
branchRefName := "refs/heads/" + branch
sourceBranchName := d.Get("autocreate_branch_source_branch").(string)
sourceBranchRefName := "refs/heads/" + sourceBranchName
if _, hasSourceSHA := d.GetOk("autocreate_branch_source_sha"); !hasSourceSHA {
ref, _, err := client.Git.GetRef(ctx, owner, repo, sourceBranchRefName)
if err != nil {
return fmt.Errorf("error querying GitHub branch reference %s/%s (%s): %s",
owner, repo, sourceBranchRefName, err.Error())
}
err = d.Set("autocreate_branch_source_sha", *ref.Object.SHA)
if err != nil {
return fmt.Errorf("error setting autocreate_branch_source_sha: %w", err)
}
}
sourceBranchSHA := d.Get("autocreate_branch_source_sha").(string)
branchRef := github.CreateRef{
Ref: branchRefName,
SHA: sourceBranchSHA,
}
if _, _, err := client.Git.CreateRef(ctx, owner, repo, branchRef); err != nil {
return fmt.Errorf("error creating GitHub branch reference %s/%s (%s): %w",
owner, repo, branchRefName, err)
}
return nil
}