Skip to content

feat: Modernise github_repository_file - #3759

Open
stevehipwell wants to merge 1 commit into
mainfrom
stevehipwell/xxwvprryxppp
Open

stevehipwell wants to merge 1 commit into
mainfrom
stevehipwell/xxwvprryxppp

Conversation

@stevehipwell

Copy link
Copy Markdown
Collaborator

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?

  • The github_repository_file resource used the legacy pattern and had numerous behavioural issues

After the change?

  • The github_repository_file resource has been modernised including tests and docs

Pull request checklist

  • Schema migrations have been created if needed (example)
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes
  • No

@stevehipwell stevehipwell added this to the v6.14.0 milestone Nov 27, 2026
@stevehipwell stevehipwell self-assigned this Nov 27, 2026
@stevehipwell stevehipwell added r/repository_file Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR labels Nov 27, 2026
@stevehipwell
stevehipwell force-pushed the stevehipwell/xxwvprryxppp branch 2 times, most recently from e8d8d8d to 69f17b2 Compare November 27, 2026 13:54
@stevehipwell stevehipwell added the acctest For CI / automated testing label Nov 27, 2026
@stevehipwell
stevehipwell requested a balanced review from Copilot November 30, 2026 17:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread github/util_repo.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

@stevehipwell
stevehipwell force-pushed the stevehipwell/xxwvprryxppp branch from 1cebc08 to 1e768d0 Compare November 30, 2026 19:31
@stevehipwell stevehipwell added the needs-github-review Request a review from GitHub label Nov 30, 2026
@team-review-router
team-review-router Bot requested a review from a team November 30, 2026 19:31

@robert-crandall robert-crandall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +326 to 349
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +139 to 153
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")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 91 to +116
"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.",
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

acctest For CI / automated testing needs-github-review Request a review from GitHub r/repository_file Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR

Projects

None yet

3 participants