Skip to content

[MAINT] Improve Import behaviour of github_default_branch#3216

Open
deiga wants to merge 23 commits into
integrations:mainfrom
F-Secure-web:fix-default-branch-import
Open

[MAINT] Improve Import behaviour of github_default_branch#3216
deiga wants to merge 23 commits into
integrations:mainfrom
F-Secure-web:fix-default-branch-import

Conversation

@deiga

@deiga deiga commented Feb 21, 2026

Copy link
Copy Markdown
Collaborator

Resolves #2613


Before the change?

  • Resource didn't use Context-aware functions
  • Resource didn't use tflog for logging
  • Missing tests for Update and Import functionality

After the change?

  • Changes to use Context-aware functions
  • Changes to use tflog for logging
  • Adds support for repository renaming, by add repository_id field and removing ForceNew from repository

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

@github-actions

Copy link
Copy Markdown

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@github-actions github-actions Bot added the Type: Bug Something isn't working as documented label Feb 21, 2026
@deiga deiga added this to the v6.12.0 Release milestone Feb 22, 2026
@deiga deiga force-pushed the fix-default-branch-import branch from 0e63837 to a31fc9f Compare March 8, 2026 21:14
@deiga deiga requested a review from stevehipwell March 8, 2026 21:15
@deiga deiga force-pushed the fix-default-branch-import branch from a31fc9f to cd4dd3d Compare May 11, 2026 02:08
@deiga deiga marked this pull request as draft May 14, 2026 21:10
@deiga deiga removed the request for review from stevehipwell May 17, 2026 13:22
@deiga deiga force-pushed the fix-default-branch-import branch from b44b2a4 to 687ec04 Compare June 3, 2026 17:16
@deiga deiga marked this pull request as ready for review June 3, 2026 17:16
@deiga deiga force-pushed the fix-default-branch-import branch from b417ac4 to 055d00c Compare June 3, 2026 18:25
@deiga deiga requested a review from Copilot June 3, 2026 18:26

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.

Pull request overview

These provider review instructions are being used.

This PR updates the github_branch_default resource to improve import and rename behavior, modernize the resource implementation to SDKv2 context-aware CRUD, and align logging and tests with current provider patterns.

Changes:

  • Refactors github_branch_default to use CreateContext/ReadContext/UpdateContext/DeleteContext, returns diag.Diagnostics, and uses tflog.
  • Adds rename-safe repository handling via repository_id + CustomizeDiff: diffRepository, and updates docs/templates to document the new exported attribute.
  • Expands acceptance coverage to include update and import scenarios using statecheck/plancheck.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
github/resource_github_branch_default.go Refactors resource to context-aware CRUD + tflog, adds repository_id, custom import handler, and rename-safe diffing.
github/resource_github_branch_default_test.go Adds/updates acceptance tests using modern statecheck/plancheck patterns for create/update/import coverage.
docs/resources/branch_default.md Documents exported repository_id attribute and clarifies import ID format.
templates/resources/branch_default.md.tmpl Mirrors docs updates for generated documentation (repository_id + import wording).

Comment thread github/resource_github_branch_default.go
Comment thread github/resource_github_branch_default.go
Comment thread github/resource_github_branch_default.go Outdated
Comment thread github/resource_github_branch_default.go
Comment thread github/resource_github_branch_default_test.go
@deiga deiga requested a review from stevehipwell June 3, 2026 19:50

@stevehipwell stevehipwell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just a quick pre-pass to look at the coding patterns for a re-work. The multi line tflog statements make reviewing harder; as does the type checking but I think we need to keep that (other than for meta in the resource functions).

Comment thread github/resource_github_branch_default.go Outdated
Comment thread github/resource_github_branch_default.go Outdated
Comment thread github/resource_github_branch_default.go Outdated
@deiga deiga force-pushed the fix-default-branch-import branch from 9d51b80 to 85640c2 Compare June 4, 2026 11:18

@stevehipwell stevehipwell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It looks like the code for the repository_id addition is missing (e.g. migration, import, update ID modification). I'd also suggest that the docs be updated to the new pattern as part of this change given the attributes are being modified.

Comment thread github/resource_github_branch_default.go Outdated
Comment thread github/resource_github_branch_default.go Outdated
deiga added 10 commits June 5, 2026 21:51
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
deiga added 8 commits June 5, 2026 21:51
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
@deiga

deiga commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator Author

It looks like the code for the repository_id addition is missing (e.g. migration, import, update ID modification). I'd also suggest that the docs be updated to the new pattern as part of this change given the attributes are being modified.

🤦 Doh!
Good catch, adding that now

deiga added 2 commits June 5, 2026 22:42
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
@deiga deiga force-pushed the fix-default-branch-import branch from efdac3c to 4137ac7 Compare June 5, 2026 20:06
@deiga deiga requested review from Copilot and stevehipwell June 5, 2026 20:07

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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comment thread github/resource_github_branch_default.go
deiga added 2 commits June 6, 2026 08:34
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Comment thread templates/resources/branch_default.md.tmpl Outdated
Comment thread templates/resources/branch_default.md.tmpl Outdated
Comment thread github/resource_github_branch_default.go Outdated
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, *repository.DefaultBranch, defaultBranch); err != nil {
return err
tflog.Debug(ctx, "Renaming branch to new default")
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, repository.GetDefaultBranch(), defaultBranch); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we clear the etag here to make it explicit that we don't expect it to work on this code path (not that it ever works with non-static tokens)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

What do you mean? It's only used for the reading the repo

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, but if you rename the branch you're explicitly modifying the repo which means the repository read is stale and requires a new etag. By manually clearing it you're making this clear to anyone reading the code.

if repository.GetDefaultBranch() != defaultBranch {
tflog.Debug(ctx, "Renaming branch to new default")
etag = resp.Header.Get("ETag")
if _, _, err := client.Repositories.RenameBranch(ctx, owner, repoName, repository.GetDefaultBranch(), defaultBranch); err != nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same etag question as above.

Signed-off-by: Timo Sand <timo.sand@f-secure.com>
@deiga deiga requested a review from stevehipwell June 12, 2026 06:36

@stevehipwell stevehipwell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you wait for #3476 to be merged you can reuse the repo ID migration logic.

Comment on lines +262 to +269
func resourceGithubBranchDefaultImport(ctx context.Context, d *schema.ResourceData, m any) ([]*schema.ResourceData, error) {
repoName := d.Id()

if err := d.Set("repository", repoName); err != nil {
return nil, err
}

return []*schema.ResourceData{d}, nil

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We need to fetch all of the required fields as part of the import (e.g. repo ID & branch).

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

Labels

Type: Bug Something isn't working as documented

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: github_branch_default attempts to rename to the same name on import

3 participants