-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow default branch to be set to master/main on update, using a new github_branch_default resource #194
Conversation
@radeksimko Apologies for the ping, but this is ready to be merged and fixes a bug that is hurting us - does it look good to you? |
github/resource_github_repository.go
Outdated
if branch != "master" { | ||
repoReq.DefaultBranch = &branch | ||
} | ||
repoReq.DefaultBranch = &branch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has something changed with the api or behavior from github? Based on my own experiences and the comments the reason its in place is because if you set the default branch and use auto_init = false
[1] this will break. I agree that its probably not the right condition to check but I don't think this is a safe change. I would suggest we avoid setting it when auto_init = false
as this covers the scenario better I think with fewer side effects.
[1]: a common scenario for this is to create an internal fork of a public project where you may need to diverge from the upstream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true, though only on creation. You may want to update a repo that was initially created with auto_init set to false, but that now has a valid ref for the default branch.
I guess the only way to do this safely would be to check if the ref actually exists on the repo and fail early if not.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specific check for master
isn't really correct, because setting the default to any ref that doesn't exist will result in the same error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the check for master is not really correct, that being said we need a condition to make it work for initial setup as well as future setups. I would say that if the ref does not exist and branch is master we set it to null and otherwise we set it to what was passed? I suppose we could also have a third condition of if its not master and ref does not exist we can error out there. I think this should cover both workflows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I expect the suggested changes to be applied anytime soon?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to help here but am unsure if I have understood the requested fix. Does this implementation satisfy the two use cases?
type validDefaultBranchOptions struct {
d *schema.ResourceData
owner string
repo string
repoReq *github.Repository
client *github.Client
}
func validDefaultBranch(options validDefaultBranchOptions) (string, error) {
// A `default_branch` is valid if it is safe for initial repository setup
// as well as future setups
defaultBranch := ""
if v, ok := options.d.GetOk("default_branch"); ok {
defaultBranch = v.(string)
}
// Assert requested default branch exists
ctx := context.WithValue(context.Background(), ctxId, options.d.Id())
_, _, err := options.client.Repositories.GetBranch(ctx, options.owner, options.repo, defaultBranch)
if err != nil {
// TODO: Match a specific error
return defaultBranch, errors.New("default branch does not exist")
}
// Assert `auto_init` is not `false` when a valid default branch is requested
if *options.repoReq.AutoInit == false {
// When creating an internal fork of a public project,
// setting the default branch alongside `auto_init = false` fails
return defaultBranch, errors.New("unsupported repository for default branch request")
}
return defaultBranch, nil
}
@liamg @majormoses - does checking for branch existence and refusing when auto_init
is false
address all concerns without raising new ones?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jcudit thanks for taking a look I am not too familiar with go but it looks right. It's been a long time so I am trying to remember back to the problem so apologies if I get some details wrong. The problem as I understand it is how to support:
- creating a new repository: can fail because it does not wait for the ref to exist.
- importing a repository and modifying it after its existence: With an import it implicitly means that the resource existed so we don't need any special handling we just do what is requested.
- updating a resource in a state: In the cause of created or imported resources that are later updated there would have to be some branch regardless of its name. It could fail if someone tries to set it to a non existing branch, it would have to be a branch and not a generic ref (sha, tag) which we do in your proposed patch.
I believe the reason the check for master was for initial creation as the requested resource did not exist github would error out. At the time terraform did not have the concept of an exposed null
input/variable type (until 0.12+) making that check for "master" something that solved the issue. Perhaps this might be a better to switch this check to null
as its intent will be clearer and its behavior can entirely be controlled by the caller. Setting this to null
would mean that we do not manage this setting until it is changed to a non null
value and in a way is similar to ignore_changes
experience. Defaulting to null
will keep the existing behavior with solving our issues. The workflow for creating a new repo with a non default branch would be:
- create repo (omit or explicitly set to
null
) - push/create new branch
- update terraform to change the default, terraform will ignore any changes to default branch unless you explicitly ask for it
I am not sure if I thought through this but the idea of terraform checking out some other repo feels odd. I view the Terraform provider responsibility in the context of github is to manage github as a service not to actually interact with git directly. It seems harmless enough but I am not sure where blurring those lines could lead to.
ping |
@jcudit @liamg I think I've caught up on all the comments, so here is my $0.02:
My suggestion is to create a new resource, |
Good find, I did not notice the patch vs get here. This actually makes a lot of sense now why I solved those problems within my module by abusing |
@majormoses @jacobisaliveandwell Thanks for the discussion! I've run with the idea above and added a |
This could potentially help with the situation in #513 too |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Looks good! |
@jcudit this is ready for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diff looks great, thanks for putting it together @liamg 🙇
Any idea on timelines for this to be released? This would be extremely useful to restore "master" on new repositories given the recent changes... |
@dwilliams782 thanks for the ping. Lets get this into the next release. @liamg are you around to address the outstanding conflict? |
@jcudit Yep, conflicts resolved :) |
…github_branch_default resource (integrations#194) * Added github_branch_default resource * Fix branch_default example in docs
Closes #193
Best explanation/reasoning for the change is here: https://github.com/terraform-providers/terraform-provider-github/pull/194#issuecomment-675587125