Repository navigation
feat: Modernise github_repository_file - #3759
stevehipwell wants to merge 1 commit into
Conversation
e8d8d8d to
69f17b2
Compare
There was a problem hiding this comment.
🟡 Changes recommended
A compilation-blocking CommitsListOptions literal remains, alongside stale commit_sha values after history rewrites and outdated composite IDs after repository renames.
3 open issues
What changed in this PR
These provider review instructions are being used.
This PR modernises github_repository_file to address apply inconsistencies and missing-history refresh failures described in #2655 and #3586.
Changes:
- Uses write responses to populate state and revises commit metadata handling.
- Adds acceptance coverage for external file changes and commit-message-only changes.
- Updates generated schema documentation, examples, and import guidance.
| File | Description |
|---|---|
templates/resources/repository_file.md.tmpl |
Uses generated schema, example, and import sections. |
RESOURCES.md |
Marks the resource’s modernisation checklist complete. |
github/util_repo.go |
Adds a latest-file-commit lookup helper. |
github/resource_github_repository_file.go |
Revises lifecycle, state, and commit-message handling. |
github/resource_github_repository_file_test.go |
Adds refresh and commit-message tests; adjusts import checks. |
github/acc_helpers_repository_test.go |
Adds a helper for external file updates. |
examples/resources/github_repository_file/resource_2.tf |
Adds an auto-created-branch example heading comment. |
examples/resources/github_repository_file/resource_1.tf |
Adds an existing-branch example heading comment. |
examples/resources/github_repository_file/import.sh |
Adds a CLI import example. |
examples/resources/github_repository_file/import-by-string-id.tf |
Adds an import-block example. |
docs/resources/repository_file.md |
Updates schema documentation and import guidance. |
Review effort: Balanced
69f17b2 to
1cebc08
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The provider cannot compile because PerPage is initialized outside the embedded ListOptions, and regression coverage is missing for unavailable stored commits and the changed API error handling.
1 open issue
3 resolved since last review
Review effort: Balanced
Signed-off-by: Steve Hipwell <[email protected]>
1cebc08 to
1e768d0
Compare
robert-crandall
left a comment
There was a problem hiding this comment.
Thanks so much for modernizing github_repository_file and adding the regression tests for history rewrites and out-of-band deletions! Dropping read-after-write and moving to fileCommit makes this much cleaner. I left a few non-blocking notes inline on edge cases and docs if you want to include any of them before merging.
| opts := resourceGithubRepositoryFileOptions(d, "Update") | ||
|
|
||
| update, _, err := client.Repositories.UpdateFile(ctx, owner, repo, file, opts) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if err = d.Set("commit_sha", update.GetSHA()); err != nil { | ||
| return diag.FromErr(err) | ||
| // Set the computed values from the update response as the file is not read back after being written. | ||
| // The commit message is not stored when it is not configured as it's only used when writing. | ||
| for k, v := range map[string]any{ | ||
| "sha": update.Content.GetSHA(), | ||
| "commit_sha": update.GetSHA(), | ||
| } { | ||
| if err := d.Set(k, v); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| if d.HasChanges("repository", "file", "branch") { | ||
| if d.HasChange("repository") { | ||
| newResourceID, err := buildID(repo, escapeIDPart(file), branch) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| d.SetId(newResourceID) |
There was a problem hiding this comment.
Right now this calls UpdateFile even when content hasn't changed, like on a repository rename, a commit_author/commit_email update, or toggling overwrite_on_create after an import. That writes an empty commit (or fails on a protected branch). If you guard UpdateFile and the sha/commit_sha sets behind if d.HasChange("content"), one caveat: the shared repository diff helper returns nil on a 404 target repo, so a repository-only change would still need to verify the repo exists and matches repository_id before rebuilding the ID.
| func resourceGithubRepositoryFileDiff(ctx context.Context, diff *schema.ResourceDiff, m any) error { | ||
| if err := diffRepository(ctx, diff, m); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if SHA, hasSHA := d.GetOk("sha"); hasSHA { | ||
| opts.SHA = new(SHA.(string)) | ||
| if diff.Id() == "" || !diff.HasChange("commit_message") { | ||
| return nil | ||
| } | ||
|
|
||
| commitAuthor, hasCommitAuthor := d.GetOk("commit_author") | ||
| commitEmail, hasCommitEmail := d.GetOk("commit_email") | ||
|
|
||
| if hasCommitAuthor && hasCommitEmail { | ||
| name := new(commitAuthor.(string)) | ||
| mail := new(commitEmail.(string)) | ||
| opts.Author = &github.CommitAuthor{Name: name, Email: mail} | ||
| opts.Committer = &github.CommitAuthor{Name: name, Email: mail} | ||
| if len(diff.GetChangedKeysPrefix("")) != 1 { | ||
| return nil | ||
| } | ||
|
|
||
| return opts | ||
| return diff.Clear("commit_message") | ||
| } |
There was a problem hiding this comment.
Not blocking, but when content changes, sha and commit_sha still plan with their old values instead of (known after apply). Downstream resources that reference them can get stale plans or hit an inconsistent final plan error. Can you call diff.SetNewComputed("sha") and diff.SetNewComputed("commit_sha") when diff.Id() != "" && (!diff.NewValueKnown("content") || diff.HasChange("content")) (SDKv2 still preserves the prior sha for the UpdateFile call)?
| Computed: true, | ||
| Description: "The commit message when creating, updating or deleting the file", | ||
| Computed: true, // TODO: Remove Computed in next major version. | ||
| Description: "Commit message used when creating, updating or deleting the file; this defaults to `Add <file>`, `Update <file>` or `Delete <file>` respectively. It is only used when the file is written and is not read back from GitHub, so changing it on its own will not update the file.", |
There was a problem hiding this comment.
Small docs note since commit_message is still Optional + Computed: if someone omits commit_message after upgrading from an older state (where Read saved the last commit message) or after removing it from config, Terraform keeps the old state value and reuses it on update or delete unless it's empty or Add <file>. Might be worth mentioning that in the description so users aren't surprised.
| "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, | ||
| Deprecated: "Use `github_branch` resource instead", | ||
| 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`.", | ||
| }, | ||
| "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'.", | ||
| Default: "main", | ||
| RequiredWith: []string{"autocreate_branch"}, | ||
| DiffSuppressFunc: autoBranchDiffSuppressFunc, | ||
| Deprecated: "Use `github_branch` resource instead", | ||
| Description: "The branch name to start from, if `autocreate_branch` is set; this defaults to `main`.", | ||
| }, | ||
| "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, | ||
| Deprecated: "Use `github_branch` resource instead", | ||
| Description: "The commit hash to start from, if `autocreate_branch` is set; this defaults to the tip of `autocreate_branch_source_branch`. If provided, `autocreate_branch_source_branch` is ignored.", | ||
| }, |
There was a problem hiding this comment.
Two quick docs catches on the deprecated autocreate_branch* attributes: tfplugindocs doesn't render Deprecated text, so the generated docs lost the "Use the github_branch resource instead" guidance. Also, autocreate_branch says deleted-branch reads fall back to autocreate_branch_source_branch, which Read doesn't do (it clears the ID on 404). Can you update those descriptions and regenerate the docs?
| @@ -1,3 +1,4 @@ | |||
| # Auto Created Branch Example | |||
There was a problem hiding this comment.
Since this example is being republished into the docs, line 16 hardcodes branch = "does/not/exist" instead of branch = github_branch.bar.branch. Without that reference, Terraform creates the branch and file in parallel and can fail on first apply if the file runs first.


Important
This repository is temporarily using an issue-first contribution model. Pull requests are currently limited to collaborators with write access. Please open an issue instead.
Resolves #2655
Resolves #3586
Before the change?
github_repository_fileresource used the legacy pattern and had numerous behavioural issuesAfter the change?
github_repository_fileresource has been modernised including tests and docsPull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!