diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 2fc6ef0f4b..0aed0cc489 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -44,6 +44,11 @@ func resourceGithubRepository() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "visibility": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{"public", "private", "internal"}, false), + }, "has_issues": { Type: schema.TypeBool, Optional: true, @@ -179,6 +184,7 @@ func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository { Description: github.String(d.Get("description").(string)), Homepage: github.String(d.Get("homepage_url").(string)), Private: github.Bool(d.Get("private").(bool)), + Visibility: github.String(d.Get("visibility").(string)), HasDownloads: github.Bool(d.Get("has_downloads").(bool)), HasIssues: github.Bool(d.Get("has_issues").(bool)), HasProjects: github.Bool(d.Get("has_projects").(bool)), @@ -209,6 +215,14 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er } repoReq := resourceGithubRepositoryObject(d) + + // Auth issues (403 You need admin access to the organization before adding a repository to it.) + // are encountered when the resources is created with the visibility parameter. As + // resourceGithubRepositoryUpdate is called immediately after, this is subsequently corrected. + if d.IsNewResource() { + repoReq.Visibility = nil + } + orgName := meta.(*Organization).name repoName := repoReq.GetName() ctx := context.Background() @@ -303,6 +317,7 @@ func resourceGithubRepositoryRead(d *schema.ResourceData, meta interface{}) erro d.Set("description", repo.GetDescription()) d.Set("homepage_url", repo.GetHomepage()) d.Set("private", repo.GetPrivate()) + d.Set("visibility", repo.GetVisibility()) d.Set("has_issues", repo.GetHasIssues()) d.Set("has_projects", repo.GetHasProjects()) d.Set("has_wiki", repo.GetHasWiki()) @@ -346,6 +361,12 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er client := meta.(*Organization).v3client repoReq := resourceGithubRepositoryObject(d) + + // The endpoint will throw an error if trying to PATCH with a visibility value that is the same + if !d.IsNewResource() && !d.HasChange("visibility") { + repoReq.Visibility = nil + } + // Can only set `default_branch` on an already created repository with the target branches ref already in-place if v, ok := d.GetOk("default_branch"); ok { branch := v.(string) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 252075d5e0..684bad5c2e 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -562,6 +562,7 @@ type testAccGithubRepositoryExpectedAttributes struct { Description string Homepage string Private bool + Visibility string HasDownloads bool HasIssues bool HasProjects bool @@ -594,6 +595,9 @@ func testAccCheckGithubRepositoryAttributes(repo *github.Repository, want *testA if private := repo.GetPrivate(); private != want.Private { return fmt.Errorf("got private %#v; want %#v", private, want.Private) } + if visibility := repo.GetVisibility(); visibility != want.Visibility { + return fmt.Errorf("got visibility %#v; want %#v", visibility, want.Visibility) + } if hasIssues := repo.GetHasIssues(); hasIssues != want.HasIssues { return fmt.Errorf("got has issues %#v; want %#v", hasIssues, want.HasIssues) } diff --git a/website/docs/r/repository.html.markdown b/website/docs/r/repository.html.markdown index 06f13fd17a..e5a4b250d4 100644 --- a/website/docs/r/repository.html.markdown +++ b/website/docs/r/repository.html.markdown @@ -41,6 +41,10 @@ The following arguments are supported: * `private` - (Optional) Set to `true` to create a private repository. Repositories are created as public (e.g. open source) by default. + +* `visibility` - (Optional) Can be `public` or `private`. If your organization is associated with an enterprise account +using GitHub Enterprise Cloud or GitHub Enterprise Server 2.20+, visibility can also be `internal`. The `visibility` +parameter overrides the `private` parameter. * `has_issues` - (Optional) Set to `true` to enable the GitHub Issues features on the repository.