terraform-provider-github: github_repository_collaborator broken since v2.8.0
Since github provider v2.8.0 the github_repository_collaborator resource is broken.
It fails with User <username> is already a collaborator. In v2.7.0 this worked as expected.
Terraform Version
v0.12.25
Affected Resource(s)
- github_repository_collaborator
Terraform Configuration Files
resource "github_repository" "my_repo" {
name = "my_repo"
private = true
description = ""
}
resource "github_repository_collaborator" "user_foobar" {
repository = github_repository.my_repo.name
username = "foobar"
permission = "pull"
}
Panic Output
Expected Behavior
What should have happened?
The collaborator should be added to the repository and the resource should be in the state.
Actual Behavior
What actually happened?
The collaborator gets added to the repository with the correct permissions, but terraform fails with this error and the resource is not in the state.
Error: User foobar is already a collaborator
on test.tf line 7, in resource "github_repository_collaborator" "user_foobar":
7: resource "github_repository_collaborator" "user_foobar" {
Steps to Reproduce
Please list the steps required to reproduce the issue, for example:
terraform apply
Important Factoids
References
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 22
- Comments: 15 (5 by maintainers)
Hello @anGie44 can we release the 2.9.3 version with this bugfix before 3.0.0? Thanks in advance
I also have this issue with repo collaborators that are managed by Terraform via this provider. Applying the plan creates the collaborators on the repo but then causes the errors noted in this issue.
If you then import the resource into the state and rerun the plan the infra is up to date and not changes are detected. Hopefully this gets fixed soon.
Edit: If you want the short version, @jcudit wrote it up here. My take is that the GitHub API documentation is incomplete and 204 should be accepted as an
OKuntil it’s fixed/updated. (Which means a manual check needs to happen before the collaborator is added.)I was able to reproduce the issue with
Terraform 0.12.26 provider.github v2.8.1
Test
Result
Additional test
Additional Result
My understanding is that GitHub reports back with a 204 to every command and that is considered an error in Terraform: https://github.com/terraform-providers/terraform-provider-github/blob/master/github/resource_github_repository_collaborator.go#L77 I think GitHub never really had any internal errors about the request, it’s just that 204 is the default response to any changes.
Troubleshooting details According to the GitHub API description on adding collaborators, there are two possible responses to the call:
What the API documentation fails to mention (at least in my case), is that if a user is a member of my organization, the user will not need an invitation to be added as a collaborator to a repository within the organization.
So, my assumption is that since the user doesn’t need an invite, a 201 response would be inappropriate. The only other defined response is 204 and for whatever reason, GitHub API chooses to send that in each
OKcase. This suspicion is further advanced by the “Remove a repository collaborator” documentation below in the API, which also responds with 204 if the collaborator was successfully removed.In short: the GitHub API documentation fails to mention that 204 is reported in any regular case.
What’s worse, it seems to execute the request properly, before reporting 204, even when the user is really a collaborator on the repo.
Additional note: the call to GitHub AddCollaborator seems fine.
Proposed solution Assume that 204 ==
OK. This seems to be a shortcoming of the GitHub API documentation.Personally, I’d prefer never erring and only giving warnings. If I have a user that has access to the repo already (say through a team), but we want to give this single user (not the whole team) more access, I’d want to be able to do that through the
github_repository_collaboratorstep. So, if the user already has access to the repo but with LESS permissions than the Terraform is trying to add, then the permissions should update to reflect Terraform.@tpatzig by
outsidedoes that also mean the user is not part of a team within the org and the org hasBase Permissionsset toNone? I was also able to reproduce this scenario inv2.8.0and notv2.7.0by adding a completely new userfoobar(outside org and not the user authenticated with the provider) first to a team that lives within an org (org hasBase Permissionsset toNoneso private repos are not visible from the new user’s perspective even while in the team), and then I attempted to add the new user as a collaborator and got the errorError: User foobar is already a collaboratorinv2.8.0