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

Support personal repositories not only GitHub organization #45

Closed
jugatsu opened this issue Aug 24, 2017 · 36 comments · Fixed by #465
Closed

Support personal repositories not only GitHub organization #45

jugatsu opened this issue Aug 24, 2017 · 36 comments · Fixed by #465
Assignees
Labels
Type: Feature New feature or request

Comments

@jugatsu
Copy link

jugatsu commented Aug 24, 2017

Terraform Version

v0.9.5

Affected Resource(s)

  • github_repository

Terraform Configuration Files

provider "github" {
  token        = "${var.github_token}"
}

resource "github_repository" "otus-devops-terraform" {
  name        = "otus-devops-terraform"
  description = "Repository for public terraform code using in Otus DevOps course"
}

resource "github_repository" "practice-git-1" {
  name        = "practice-git-1"
  description = "Repository for practicing Git in Otus DevOps course"
}

resource "github_repository" "practice-git-2" {
  name        = "practice-git-2"
  has_issues  = true
  description = "Repository for practicing Git in Otus DevOps course"
}

Debug Output

terraform.tfstate

{
    "version": 3,
    "terraform_version": "0.9.5",
    "serial": 2,
    "lineage": "753781c3-ade7-4b8d-9723-2f7f9bbdb08b",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {},
            "resources": {},
            "depends_on": []
        }
    ]
}

Expected Behavior

It should create repository under personal account and write state in terraform.tfstate

Actual Behavior

terraform asks Github oraganization when running with terraform apply. Also it returns 422 when running twice. So it isn't idempotent.

Steps to Reproduce

terraform apply

@jugatsu
Copy link
Author

jugatsu commented Aug 24, 2017

@radeksimko radeksimko added the Type: Feature New feature or request label Aug 31, 2017
@radeksimko radeksimko changed the title [FEATURE REQUEST] Support personal repositories not only GitHub organization Support personal repositories not only GitHub organization Aug 31, 2017
@TraGicCode
Copy link

Any updates on this?

@paultyng
Copy link
Contributor

Related to #77

@frostebite
Copy link

Any update on this? It would be great!

@kquinsland
Copy link

kquinsland commented Jan 19, 2019

the username is the github_orginization if you're operating under the personal/free account (it appears that in the GET Repos URL, the org argument sent to the provider is set as the owner in the request)

I was able to get tf import github_repo... to work with a personal access token.
I was able to run tf apply after importing and got a correct/successful "no changes needed"
I then tried to change the name of the repo in terraform, and am now getting:
github_repository.test: POST https://api.github.com/orgs/kquinsland/repos: 404 Not Found []

Looking at the API Docs there is a different URL endpoint for creating in an org (POST /orgs/:org/repos) versus creating for a user (POST /user/repos. I'm very new to golang, is there a significant amount of work involved to add a simple account_type value to the provider, where a value of user informs the provider to use the user version of the necessary APIs and a value of org uses the APIs that are currently there?

@p0bailey
Copy link

p0bailey commented Mar 2, 2019

Having the possibilty to create and manage personal repositories from TF would be amazing, especially for demos. Any updates down the line?

@p0bailey
Copy link

Hello there,
Meanwhile waiting for a fix into GitHub Terraform provider. I have just released a Terraform module for anyone interested to set GitHup personal repositories. It's a bit raw, though.

https://github.com/p0bailey/terraform-github-personal-repositories

@binlab
Copy link

binlab commented Jan 20, 2020

Any updates on this?

@p0bailey
Copy link

Any updates on this?

Everything seems blocked.

@ahaffar
Copy link

ahaffar commented Mar 9, 2020

i am using github_provider version 2.4.1 and it works for individual repos

provider "github" {
  version = "> 2.4"
  token = var.token
  individual = false
  organization = var.organization
}

where organization is the username

@mrtazz
Copy link

mrtazz commented Mar 25, 2020

this worked for me. With version 2.4.1 configured for my individual account as above I was at least able to:

  • import state
  • add a collaborator to a repo
  • mark a repo as archived

@cornfeedhobo
Copy link
Contributor

cornfeedhobo commented Mar 29, 2020

I am unable to confirm @mrtazz's success:

Provider

provider "github" {
    version    = "~> 2.4"
    individual = true
}
$ terraform init

Initializing the backend...

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "github" (hashicorp/github) 2.4.1...
...snip...

Applying

resource "github_repository" "test" {
    name = "terraform-github_branch-test"
}
$ terraform apply
...snip...
github_repository.test: Creating...

Error: This resource requires GitHub organization to be set on the provider.

  on main.tf line 5, in resource "github_repository" "test":
   5: resource "github_repository" "test" {

Importing

resource "github_repository" "test" {
    name = "go-travis-testing"
}
$ terraform import github_repository.test cornfeedhobo:go-travis-testing
github_repository.test: Importing from ID "cornfeedhobo:go-travis-testing"...
github_repository.test: Import prepared!
  Prepared github_repository for import
github_repository.test: Refreshing state... [id=cornfeedhobo:terraform-github_branch-test]

Error: This resource requires GitHub organization to be set on the provider.

Fixing this should close #50, #56, and #96.

@shoekstra @jcudit pinging you because I stumbled into this while attempting to test importing with github_branch.

@mrtazz
Copy link

mrtazz commented Mar 30, 2020

@cornfeedhobo you need to set individual = false in the provider config. That's kinda the weird thing here. The provider then treats your individual account as an org, so there will definitely be edge cases that don't work.

@cornfeedhobo
Copy link
Contributor

cornfeedhobo commented Mar 30, 2020

@mrtazz I tried that as well and it fails too.

provider "github" {
    individual   = false
    organization = "cornfeedhobo"
}

resource "github_repository" "test" {
    name = "terraform-github_branch-test"
}
Error: GET https://api.github.com/orgs/cornfeedhobo: 404 Not Found []

Like others point out above, it's impossible because the API's /org endpoint won't respond to usernames.

Edit: same thing if organization = "" :-(

@mrtazz
Copy link

mrtazz commented Mar 30, 2020

hmm that's curious. My provider config looks like this

provider "github" {
  version      = "> 2.4"
  individual   = false
  organization = "mrtazz"
}

And with that I was able to import all of my repos as well as set some things as mentioned in https://github.com/terraform-providers/terraform-provider-github/issues/45#issuecomment-603794145.

@cornfeedhobo
Copy link
Contributor

Strange indeed! Every combination in the provider has failed for me

Error: This resource requires GitHub organization to be set on the provider.
Error: GET https://api.github.com/orgs/cornfeedhobo: 404 Not Found []
Error: If `individual` is true, `organization` cannot be set.

@cornfeedhobo
Copy link
Contributor

@mrtazz are you exporting anything other than GITHUB_TOKEN?

@ozthegreat
Copy link

For Personal repos this works. It only works with 2.4.0 though, 2.4.1 and 2.5.0 break it.

# Configure the GitHub Provider.
# token from GITHUB_TOKEN env var (required by codepipline)
provider "github" {
  version    = "2.4.0"
  individual = false
  organization = "MyPersonalUserName"
}

@cornfeedhobo
Copy link
Contributor

Interesting. As I sit here and finish a PR, I can already see how big of a fix this could end up being. I'll have to think about this a little. Maybe it would be best to fix a one or a few resources at a time. Limit the blast radius of each PR.

@jcudit
Copy link
Contributor

jcudit commented Mar 30, 2020

To add some wider context, a 3.x release of this provider was discussed over in https://github.com/terraform-providers/terraform-provider-github/pull/316 to solve a similar underlying problem for user identifiers. If a 3.x release helps to get this feature shipped, that can be arranged.

I initially thought GraphQL support would be a cause for a major version bump as well. However, there is a plan to incrementally add support. If we are to take a similar approach, comprehensive acceptance testing and state migration coverage will be needed to 🚢 this feature incrementally.

@mrtazz
Copy link

mrtazz commented Mar 31, 2020

@mrtazz are you exporting anything other than GITHUB_TOKEN?

@cornfeedhobo I don't have any other environment variables set.

@ozthegreat oh that's weird. I'm definitely running 2.4.1

Initializing provider plugins...
- Checking for available provider plugins...
- Downloading plugin for provider "github" (hashicorp/github) 2.4.1...

@keyboardarmy
Copy link

this doesn't work in 2.6.0 either

@janaka
Copy link

janaka commented Apr 5, 2020

Doesn't work for me with a personal repo either. Hopefully this information helps get this resolved.

I've tried 2.4 and 2.6. The issue with 2.4 is different to 2.6 it seem and might be caused by the GH API changing.

With provider version 2.4.0
Error: GET https://api.github.com/repos/janaka/aws-app-mesh-example: 404 Not Found []

With provider version 2.6.0
Error: GET https://api.github.com/orgs/janaka: 404 Not Found []

Here are the two relevant fragments of my Terraform

stage {
    name = "Source"

    action {
      name             = "SourceCode"
      category         = "Source"
      owner            = "ThirdParty"
      provider         = "GitHub"
      version          = "1"
      output_artifacts = ["SourceCodeArtifact"]

      configuration = {
        Owner  = "janaka"
        Repo   = "aws-app-mesh-example"
        Branch = "master"
      }
    }
}

provider "github" {
  version = "2.4.0"
  token        = local.webhook_secret
  individual = false
  organization = "janaka"
}

data "github_repository" "repo" {
  full_name = "janaka/aws-app-mesh-example"
}

resource "aws_codepipeline_webhook" "hello-world-api_webhook" {
  name            = "hello-world-api-webhook-github"
  authentication  = "GITHUB_HMAC"
  target_action   = "Source"
  target_pipeline = aws_codepipeline.codepipeline.name

  authentication_configuration {
    secret_token = local.webhook_secret
  }

  filter {
    json_path    = "$.commits[*]"
    match_equals = "hello-world-api/(src/.*|version)"
  }
}

# Wire the CodePipeline webhook into a GitHub repository.
resource "github_repository_webhook" "hello-world-api" {
  repository = data.github_repository.repo.name

  configuration {
    url          = aws_codepipeline_webhook.hello-world-api_webhook.url
    content_type = "json"
    insecure_ssl = false
    secret       = local.webhook_secret
  }

  events = ["push"]
}

@chukaofili
Copy link

@janaka Did you check to make sure your token has the right permissions? you'll need repo admin when create you personal access token. This is using with provider version 2.4.0.

@janaka
Copy link

janaka commented Apr 17, 2020

Pretty sure perms are good. I'll need to double check as it's been some days.

@janaka
Copy link

janaka commented Apr 18, 2020

FYI - I moved the repo from my personal to an Org (the personal account owns) and all is now working. Same token with no perms changes. Provider 2.6.1.

@abitrolly
Copy link

abitrolly commented Jun 22, 2020

@clebio
Copy link

clebio commented Aug 12, 2020

Sorry, what is the fix here for the github_repository data source? Nothing I see described above seems to work. I'm on provider.github v2.9.2 and getting

Error: GET https://api.github.com/orgs/clebio: 404 Not Found []

@cornfeedhobo
Copy link
Contributor

@clebio pretty sure this was reverted

@janaka
Copy link

janaka commented Aug 13, 2020

Given orgs became free around the same time, I just created one and moved my repo there.

@josjaf
Copy link

josjaf commented Aug 23, 2020

I'm now having the issue - @cornfeedhobo - how do you know it was reverted? I cannot easily create an organization because I'm part of a few already.

@cornfeedhobo
Copy link
Contributor

@josjaf
Copy link

josjaf commented Sep 1, 2020

Any idea what the workaround is for personal repositories for now?

@cornfeedhobo
Copy link
Contributor

@josjaf there is none that I know of right now. Sounds like most people have created orgs for the meantime.

@abitrolly
Copy link

@josjaf I moved to GitLab.

mikerochip referenced this issue in mikerochip/swift-sam-app Sep 16, 2020
* Constantly giving the error "error: This resource requires GitHub organization to be set on the provider."
* See https://github.com/terraform-providers/terraform-provider-github/issues/45
@ggsood
Copy link

ggsood commented Dec 25, 2020

This issue is now resolved now as per the provider version v4.1.0, you can create a github repo in your personal profile as

provider "github" {
  owner = "<GITHUB_USERNAME>"
  token = "<GITHUB_TOKEN>"
}

resource "github_repository" "terraformed" {
  name        = "terraformed"
  description = "A repository created by Terraform"
  visibility  = "private"
} 

Thanks Community for fixing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment