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

Partial failure if the repository name contains any space #161

Closed
btlogy opened this issue Jul 21, 2023 · 9 comments
Closed

Partial failure if the repository name contains any space #161

btlogy opened this issue Jul 21, 2023 · 9 comments

Comments

@btlogy
Copy link

btlogy commented Jul 21, 2023

The module seems to allow spaces to be used in the name of the repository.

It will apparently converts them into - while building the plan (I've not found yet where this is done).

However, it partially fails to apply the plan: the repository will be created in GitHub but Terraform will fail.

How to reproduce

  1. Create a new repository like this:
module "space_repo" {
  source  = "mineiros-io/repository/github"
  version = "~> 0.18.0"

  name = "space repo"
}
  1. Execute terraform plan - should give something like this:
# module.space_repo.github_repository.repository will be created
+ resource "github_repository" "repository" {
   ...
   + name = "space repo"
   ...
 }
  1. Execute terraform apply - should fail with this error:
module.space_repo.github_repository.repository: Creating...

Error: PATCH https://api.github.com/repos/my-org/space-repo: 422 Validation Failed []

  with module.space_repo.github_repository.repository,
  on .terraform/modules/space_repo/main.tf line 91, in resource "github_repository" "repository":
  91: resource "github_repository" "repository" {
  1. Subsequent terrafrom apply should fail again with this other error:
module.space_repo.github_repository.repository: Destroying... [id=space-repo]
module.space_repo.github_repository.repository: Destruction complete after 0s
module.space_repo.github_repository.repository: Creating...

Error: POST https://api.github.com/orgs/my-org/repos: 422 Repository creation failed. [{Resource:Repository Field:name Code:custom Message:name already exists on this account}]

  with module.space_repo.github_repository.repository,
  on .terraform/modules/space_repo/main.tf line 91, in resource "github_repository" "repository":
  91: resource "github_repository" "repository" {

(Because archive_on_destroy is set to true by default I suppose).

Expected behavior

I suppose the terraform plan command should explicitly fail and report the presence of invalid character(s) in the name.

@mariux
Copy link
Member

mariux commented Jul 21, 2023

Hi @btlogy

thanks for bringing this to our attention. The errors you see are from the provider not from the module. GitHub does not allow spaces in names and converts them to dashes. The provider does not seem to care about validation here.

As we are just shifting this responsibility to the provider and they are shifting it to the API the error you see is expected.

The upstream error can be tracked here: integrations/terraform-provider-github#1215 maybe you wanna upvote it there ;)

The solution here is to not try to create repositories with spaces but use dashes right away as even GitHub UI adds the dashes for you:
Screenshot 2023-07-21 at 18-29-05 New repository

@btlogy
Copy link
Author

btlogy commented Jul 21, 2023

Hi @mariux

Thank you this quick answer.
I did not trace the root cause back to the provider: thank you for pointing to this issue.

Before reading your comment, I was considering to propose a PR using input-variable-validation here to avoid space in the name:

variable "name" {
description = "(Required) The name of the repository."
type = string
}

Should I try to do this upstream instead?

@btlogy
Copy link
Author

btlogy commented Jul 21, 2023

The solution here is to not try to create repositories with spaces...

In our case, we do not have control on the input, so we have rely on the input being validated by either the module and/or the provider.

btlogy added a commit to btlogy/terraform-github-repository that referenced this issue Jul 21, 2023
@btlogy
Copy link
Author

btlogy commented Jul 21, 2023

Something like this would do for us now:

While waiting for you feedback (@mariux), I'll investigate if this could/should be done in the provider instead...

@btlogy
Copy link
Author

btlogy commented Jul 21, 2023

It will likely be harder for me to fix the provider, but I can try:

@btlogy
Copy link
Author

btlogy commented Jul 24, 2023

Hi @mariux

I've submitted my PR above for the provider.
Maybe you can have a look and upvote it if sensible.

@btlogy
Copy link
Author

btlogy commented Jul 25, 2023

Cool, the pull request has been merged in the provider repository: the latest version (v5.32.0) should enforce character validation.
@mariux: how do we get this feature in this module (terraform init -upgrade still gives me integrations/github v4.31.0)?

@btlogy
Copy link
Author

btlogy commented Jul 25, 2023

Cool, the pull request has been merged in the provider repository: the latest version (v5.32.0) should enforce character validation. @mariux: how do we get this feature in this module (terraform init -upgrade still gives me integrations/github v4.31.0)?

Nevermind: I've not seen we are still using v0.8.0 of module github_team which might prevent us to upgrade...

@btlogy
Copy link
Author

btlogy commented Jul 27, 2023

I can confirm here that provider v5.32.0 fixes this issue

@btlogy btlogy closed this as completed Jul 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants