Skip to content
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

Fix Terraform trying to set default_branch to null #343

Merged
merged 6 commits into from
Sep 15, 2020
Merged

Fix Terraform trying to set default_branch to null #343

merged 6 commits into from
Sep 15, 2020

Conversation

abitrolly
Copy link
Contributor

@abitrolly abitrolly commented Jun 22, 2020

Fixes #299 where Terraform tries to reset the default_branch to null for non-empty repositories if not set in .tf

The default_branch defaults to master on GitLab side, but it is only set if the repository is not empty. Otherwise it is null on remote side.


The debug in 24d43d8 showed no trace of null value from remote.

2020/06/19 20:55:55 [DEBUG] gitlab.Project default_branch (string) ""
2020/06/19 20:55:55 [DEBUG] d.Get default_branch (string) ""
2020/06/19 20:55:55 [DEBUG] d.GetOk default_branch (string) "" - false
2020/06/19 20:55:55 [DEBUG] d.HasChange default_branch false
2020/06/19 20:55:55 [DEBUG] d.GetChange default_branch (string) "", (string) ""

But the actual response shows that null was returned in JSON.

2020/06/19 20:55:55 [DEBUG] GitLab API Response Details:
...
 "default_branch": null,

The fix is to skip the change if TF is trying to set null value for remote side. This PR also adds a warning if default_branch is specified in .tf file, but the remote repo is empty. And also removes more specific check for initilize_with_readme towards the generic case above. default_branch should not make assumptions on how the repo was created.

WIP for fixing #299 where Terraform tries to reset the
`default_branch` to null after a repository is added on
GitLab side.

The `default_branch` defaults to `master` on GitLab side,
but it is only set if the repository is not empty.
Otherwise it is `null` on remote side.

The commit generates the following output, which shows
no trace of `null` value from remote.

    2020/06/19 20:55:55 [DEBUG] gitlab.Project default_branch (string) ""
    2020/06/19 20:55:55 [DEBUG] d.Get default_branch (string) ""
    2020/06/19 20:55:55 [DEBUG] d.GetOk default_branch (string) "" - false
    2020/06/19 20:55:55 [DEBUG] d.HasChange default_branch false
    2020/06/19 20:55:55 [DEBUG] d.GetChange default_branch (string) "", (string) ""

But the actual response shows that `null` was returned in
JSON.

    2020/06/19 20:55:55 [DEBUG] GitLab API Response Details:
    ...
     "default_branch": null,
@ghost ghost added the size/XS label Jun 22, 2020
@abitrolly abitrolly marked this pull request as draft June 22, 2020 07:15
@abitrolly
Copy link
Contributor Author

After understanding what SchemaDiffSuppressFunc really does hashicorp/terraform-plugin-sdk#479 it looks like it is possible to suppress the plan if new planned value is empty. What would ignore all changes to default_branch until it is set in Terraform config.

@abitrolly
Copy link
Contributor Author

Setting default branch on an empty repo results in an error.

Error: PUT https://gitlab.com/api/v4/projects/19526446: 400 {message: {base: [Could not change HEAD: branch 'sdfdf' does not exist]}}

@ghost ghost added size/S and removed size/XS labels Jun 22, 2020
@abitrolly abitrolly marked this pull request as ready for review June 22, 2020 18:09
@abitrolly abitrolly changed the title Debug to detect when default_branch is set in .tf file Fix Terraform trying to set default_branch to null Jun 26, 2020
@armsnyder
Copy link
Collaborator

It would be nice to see a regression test for this, since it's difficult to tell just by looking at the change how it will behave.

@horjulf
Copy link

horjulf commented Aug 28, 2020

This looks simple enough, can we get this merged please ?
Currently the provider breaks in all cases where you don't want to track or enforce a default branch in Terraform code.

@nicholasklick

@abitrolly
Copy link
Contributor Author

With the ongoing governmental terror in Belarus, I am emotionally drained, and can not think clearly to write the tests, sorry.

@roidelapluie
Copy link
Collaborator

@armsnyder WDYT about this?

@abitrolly no worries. take care!!!!

@armsnyder
Copy link
Collaborator

@roidelapluie The PR looks like an improvement, so I'm fine with merging. I think it could be improved further at a later time by using the Default or Computed fields of the attribute, and I was also hoping for a test. But on visual inspection it looks ok.

@nicholasklick
Copy link
Collaborator

Merging due to customer request and Maintainer approval

@nicholasklick nicholasklick merged commit 750dd96 into gitlabhq:master Sep 15, 2020
@roidelapluie
Copy link
Collaborator

Merging due to customer request and Maintainer approval

Thanks to* I hope :)

@abitrolly abitrolly deleted the delayed_branch branch November 28, 2020 07:01
@github-actions github-actions bot locked and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
5 participants