[MAINT] Improve Import behaviour of github_default_branch#3216
Conversation
|
👋 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 |
0e63837 to
a31fc9f
Compare
a31fc9f to
cd4dd3d
Compare
b44b2a4 to
687ec04
Compare
b417ac4 to
055d00c
Compare
There was a problem hiding this comment.
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_defaultto useCreateContext/ReadContext/UpdateContext/DeleteContext, returnsdiag.Diagnostics, and usestflog. - 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). |
stevehipwell
left a comment
There was a problem hiding this comment.
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).
9d51b80 to
85640c2
Compare
stevehipwell
left a comment
There was a problem hiding this comment.
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.
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>
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>
🤦 Doh! |
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
efdac3c to
4137ac7
Compare
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
| 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 { |
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
What do you mean? It's only used for the reading the repo
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Same etag question as above.
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
stevehipwell
left a comment
There was a problem hiding this comment.
If you wait for #3476 to be merged you can reuse the repo ID migration logic.
| 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 |
There was a problem hiding this comment.
We need to fetch all of the required fields as part of the import (e.g. repo ID & branch).
Resolves #2613
Before the change?
tflogfor loggingUpdateandImportfunctionalityAfter the change?
tflogfor loggingrepository_idfield and removingForceNewfromrepositoryPull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!