From 55ba81162811aa83b9c9cdacb68342e5aef95f1a Mon Sep 17 00:00:00 2001 From: Jurgen Weber Date: Fri, 29 Jan 2021 14:33:06 +1100 Subject: [PATCH 1/9] visibility is required on create and can not be updated --- github/resource_github_repository.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 42d851943d..6679bdedb6 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -271,11 +271,6 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er repoReq := resourceGithubRepositoryObject(d) owner := meta.(*Owner).name - // 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. - repoReq.Visibility = nil - repoName := repoReq.GetName() ctx := context.Background() @@ -462,10 +457,9 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er repoReq := resourceGithubRepositoryObject(d) + // The visibility of a repo can not be changed once created // The endpoint will throw an error if trying to PATCH with a visibility value that is the same - if !d.HasChange("visibility") { - repoReq.Visibility = nil - } + 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 { From cbb9b2d7a998d7ddea8768548fa87025bf76fda6 Mon Sep 17 00:00:00 2001 From: Jurgen Weber Date: Fri, 29 Jan 2021 14:33:11 +1100 Subject: [PATCH 2/9] add an example --- examples/repo_org_internal/README.md | 18 ++++++++++++++++++ examples/repo_org_internal/cli.cfg | 10 ++++++++++ examples/repo_org_internal/main.tf | 5 +++++ examples/repo_org_internal/outputs.tf | 4 ++++ examples/repo_org_internal/providers.tf | 4 ++++ examples/repo_org_internal/variables.tf | 9 +++++++++ 6 files changed, 50 insertions(+) create mode 100644 examples/repo_org_internal/README.md create mode 100644 examples/repo_org_internal/cli.cfg create mode 100644 examples/repo_org_internal/main.tf create mode 100644 examples/repo_org_internal/outputs.tf create mode 100644 examples/repo_org_internal/providers.tf create mode 100644 examples/repo_org_internal/variables.tf diff --git a/examples/repo_org_internal/README.md b/examples/repo_org_internal/README.md new file mode 100644 index 0000000000..fd21df18f5 --- /dev/null +++ b/examples/repo_org_internal/README.md @@ -0,0 +1,18 @@ +# Repository Visibility with Org, type internal + +This demos various repository [visibility settings](https://help.github.com/en/github/administering-a-repository/setting-repository-visibility) for repositories. + +This example will create a repositories in the specified `owner` organization. See https://www.terraform.io/docs/providers/github/index.html for details on configuring [`providers.tf`](./providers.tf) accordingly. + +Alternatively, you may use variables passed via command line: + +```console +export GITHUB_OWNER= +export GITHUB_TOKEN= +``` + +```console +terraform apply \ + -var "owner=${GITHUB_OWNER}" \ + -var "github_token=${GITHUB_TOKEN}" +``` diff --git a/examples/repo_org_internal/cli.cfg b/examples/repo_org_internal/cli.cfg new file mode 100644 index 0000000000..87e64e2d8c --- /dev/null +++ b/examples/repo_org_internal/cli.cfg @@ -0,0 +1,10 @@ +provider_installation { + filesystem_mirror { + path = "/Users/jurgen.weber/checkouts/terraform/terraform-provider-github/examples/repo_org_internal/terraform.d/plugins" + include = ["*/*/*"] + } + direct { + exclude = ["*/*/*"] + } +} + diff --git a/examples/repo_org_internal/main.tf b/examples/repo_org_internal/main.tf new file mode 100644 index 0000000000..d1b976d4c1 --- /dev/null +++ b/examples/repo_org_internal/main.tf @@ -0,0 +1,5 @@ +resource github_repository internal { + name = "mtribes-jurgen" + description = "A internal-visible repository created by Terraform" + visibility = "internal" +} diff --git a/examples/repo_org_internal/outputs.tf b/examples/repo_org_internal/outputs.tf new file mode 100644 index 0000000000..cdfa0a8ba9 --- /dev/null +++ b/examples/repo_org_internal/outputs.tf @@ -0,0 +1,4 @@ +output internal_repository { + description = "Example repository JSON blob" + value = github_repository.internal +} diff --git a/examples/repo_org_internal/providers.tf b/examples/repo_org_internal/providers.tf new file mode 100644 index 0000000000..0213bde81b --- /dev/null +++ b/examples/repo_org_internal/providers.tf @@ -0,0 +1,4 @@ +provider github { + owner = var.owner + token = var.github_token +} diff --git a/examples/repo_org_internal/variables.tf b/examples/repo_org_internal/variables.tf new file mode 100644 index 0000000000..36108501a9 --- /dev/null +++ b/examples/repo_org_internal/variables.tf @@ -0,0 +1,9 @@ +variable owner { + description = "GitHub owner used to configure the provider" + type = string +} + +variable github_token { + description = "GitHub access token used to configure the provider" + type = string +} From 6754aed85e432f2319bdf80687d1e06086972120 Mon Sep 17 00:00:00 2001 From: Jurgen Weber Date: Mon, 1 Feb 2021 14:49:16 +1100 Subject: [PATCH 3/9] update logic for testing if there is an adequate change --- github/resource_github_repository.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 6679bdedb6..38ce3a1d26 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -457,9 +457,17 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er repoReq := resourceGithubRepositoryObject(d) - // The visibility of a repo can not be changed once created - // The endpoint will throw an error if trying to PATCH with a visibility value that is the same - repoReq.Visibility = nil + if d.HasChange("visibility") { + // The endpoint will throw an error if this repo is being created and the old value is "" + o, n := d.GetChange("visibility") + log.Printf("[DEBUG] Old Value %v New Value %v", o, n) + if o.(string) == "" { + repoReq.Visibility = nil + } + } else { + // The endpoint will throw an error if trying to PATCH with a visibility value that is the same + 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 { From d08691534c4040f7cba6658052900247be7d7a97 Mon Sep 17 00:00:00 2001 From: Jeremy Udit Date: Sun, 14 Mar 2021 11:09:24 -0400 Subject: [PATCH 4/9] add visibility tests --- github/resource_github_repository_test.go | 203 ++++++++++++++++++++++ 1 file changed, 203 insertions(+) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 0deac806a6..36f02e750e 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "log" + "regexp" "strings" "testing" @@ -669,6 +670,199 @@ func TestAccGithubRepositoryPages(t *testing.T) { } +func TestAccGithubRepositoryVisibility(t *testing.T) { + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + t.Run("updates repos to private visibility", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "public" { + name = "tf-acc-test-visibility-public-%s" + visibility = "public" + } + + resource "github_repository" "internal" { + name = "tf-acc-test-visibility-internal-%[1]s" + visibility = "internal" + } + `, randomID) + + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.public", "visibility", + "public", + ), + resource.TestCheckResourceAttr( + "github_repository.internal", "visibility", + "internal", + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.public", "visibility", + "private", + ), + resource.TestCheckResourceAttr( + "github_repository.internal", "visibility", + "private", + ), + ), + } + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: reconfigureVisibility(config, "private"), + Check: checks["after"], + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) + + // t.Run("updates repos to public visibility", func(t *testing.T) { + + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-prv-vuln-%s" + // visibility = "private" + // } + // `, randomID) + + // checks := map[string]resource.TestCheckFunc{ + // "before": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "false", + // ), + // ), + // "after": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "true", + // ), + // resource.TestCheckResourceAttr( + // "github_repository.test", "visibility", + // "private", + // ), + // ), + // } + + // testCase := func(t *testing.T, mode string) { + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnlessMode(t, mode) }, + // Providers: testAccProviders, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: checks["before"], + // }, + // { + // Config: strings.Replace(config, + // `}`, + // "vulnerability_alerts = true\n}", 1), + // Check: checks["after"], + // }, + // }, + // }) + // } + + // t.Run("with an anonymous account", func(t *testing.T) { + // t.Skip("anonymous account not supported for this operation") + // }) + + // t.Run("with an individual account", func(t *testing.T) { + // testCase(t, individual) + // }) + + // t.Run("with an organization account", func(t *testing.T) { + // testCase(t, organization) + // }) + // }) + + // t.Run("updates repos to internal visibility", func(t *testing.T) { + + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-prv-vuln-%s" + // visibility = "private" + // } + // `, randomID) + + // checks := map[string]resource.TestCheckFunc{ + // "before": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "false", + // ), + // ), + // "after": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "true", + // ), + // resource.TestCheckResourceAttr( + // "github_repository.test", "visibility", + // "private", + // ), + // ), + // } + + // testCase := func(t *testing.T, mode string) { + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnlessMode(t, mode) }, + // Providers: testAccProviders, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: checks["before"], + // }, + // { + // Config: strings.Replace(config, + // `}`, + // "vulnerability_alerts = true\n}", 1), + // Check: checks["after"], + // }, + // }, + // }) + // } + + // t.Run("with an anonymous account", func(t *testing.T) { + // t.Skip("anonymous account not supported for this operation") + // }) + + // t.Run("with an individual account", func(t *testing.T) { + // testCase(t, individual) + // }) + + // t.Run("with an organization account", func(t *testing.T) { + // testCase(t, organization) + // }) + // }) + +} + func testSweepRepositories(region string) error { meta, err := sharedConfigForRegion(region) if err != nil { @@ -701,3 +895,12 @@ func init() { F: testSweepRepositories, }) } + +func reconfigureVisibility(config, visibility string) string { + re := regexp.MustCompile(`visibility = "(.*)"`) + newConfig := re.ReplaceAllString( + config, + fmt.Sprintf(`visibility = "%s"`, visibility), + ) + return newConfig +} From 3dc6603e13f369ce19acfb305ef95fdce94deb57 Mon Sep 17 00:00:00 2001 From: Jeremy Udit Date: Fri, 26 Mar 2021 11:27:29 -0400 Subject: [PATCH 5/9] add testing for creation of private-visibility repos - removes testing of `internal` repositories - we encountered 500s from the API with the following error message: > Only organizations associated with an enterprise can set visibility to internal - a compatible testing environment is needed for automated testing - manual testing may be successful when running a local github instance - removes conflicting use of `private` and `visibility` - added `calculateVisibility` to account for the different combinations of visibility-related configuration that the provider expects - testing revealed different behaviour from the API documentation: > The visibility parameter overrides the private parameter when you use both parameters with the nebula-preview preview header. --- github/resource_github_repository.go | 20 +++++++- github/resource_github_repository_test.go | 56 +++++++++++++++++------ 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 38ce3a1d26..c2ffed0ced 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -237,13 +237,29 @@ func resourceGithubRepository() *schema.Resource { } } +func calculateVisibility(d *schema.ResourceData) string { + + if value, ok := d.GetOk("visibility"); ok { + return value.(string) + } + + if value, ok := d.GetOk("private"); ok { + if value.(bool) { + return "private" + } else { + return "public" + } + } + + return "public" +} + func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository { return &github.Repository{ Name: github.String(d.Get("name").(string)), 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)), + Visibility: github.String(calculateVisibility(d)), HasDownloads: github.Bool(d.Get("has_downloads").(bool)), HasIssues: github.Bool(d.Get("has_issues").(bool)), HasProjects: github.Bool(d.Get("has_projects").(bool)), diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 36f02e750e..3a3d94fba6 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -674,17 +674,55 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + t.Run("creates repos with private visibility", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "private" { + name = "tf-acc-test-visibility-private-%s" + visibility = "private" + } + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.private", "visibility", + "private", + ), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) + t.Run("updates repos to private visibility", func(t *testing.T) { config := fmt.Sprintf(` resource "github_repository" "public" { name = "tf-acc-test-visibility-public-%s" visibility = "public" - } - - resource "github_repository" "internal" { - name = "tf-acc-test-visibility-internal-%[1]s" - visibility = "internal" + vulnerability_alerts = false } `, randomID) @@ -694,20 +732,12 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { "github_repository.public", "visibility", "public", ), - resource.TestCheckResourceAttr( - "github_repository.internal", "visibility", - "internal", - ), ), "after": resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( "github_repository.public", "visibility", "private", ), - resource.TestCheckResourceAttr( - "github_repository.internal", "visibility", - "private", - ), ), } From 8c64351d0035cb95470f4b7cf4ec6265f5741da8 Mon Sep 17 00:00:00 2001 From: Jeremy Udit Date: Fri, 26 Mar 2021 11:27:29 -0400 Subject: [PATCH 6/9] add testing for creation of private-visibility repos - removes testing of `internal` repositories - we encountered 500s from the API with the following error message: > Only organizations associated with an enterprise can set visibility to internal - a compatible testing environment is needed for automated testing - manual testing may be successful when running a local github instance - removes conflicting use of `private` and `visibility` - added `calculateVisibility` to account for the different combinations of visibility-related configuration that the provider expects - testing revealed different behaviour from the API documentation: > The visibility parameter overrides the private parameter when you use both parameters with the nebula-preview preview header. --- github/resource_github_repository.go | 20 +- github/resource_github_repository_test.go | 223 ++++++++++++++++++++++ 2 files changed, 241 insertions(+), 2 deletions(-) diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index 3ef9fdaff0..1eca113eda 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -237,13 +237,29 @@ func resourceGithubRepository() *schema.Resource { } } +func calculateVisibility(d *schema.ResourceData) string { + + if value, ok := d.GetOk("visibility"); ok { + return value.(string) + } + + if value, ok := d.GetOk("private"); ok { + if value.(bool) { + return "private" + } else { + return "public" + } + } + + return "public" +} + func resourceGithubRepositoryObject(d *schema.ResourceData) *github.Repository { return &github.Repository{ Name: github.String(d.Get("name").(string)), 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)), + Visibility: github.String(calculateVisibility(d)), HasDownloads: github.Bool(d.Get("has_downloads").(bool)), HasIssues: github.Bool(d.Get("has_issues").(bool)), HasProjects: github.Bool(d.Get("has_projects").(bool)), diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 26348f2495..56c0f9268d 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -735,6 +735,229 @@ func TestAccGithubRepositoryPages(t *testing.T) { } +func TestAccGithubRepositoryVisibility(t *testing.T) { + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + t.Run("creates repos with private visibility", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "private" { + name = "tf-acc-test-visibility-private-%s" + visibility = "private" + } + `, randomID) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.private", "visibility", + "private", + ), + ) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) + + t.Run("updates repos to private visibility", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "public" { + name = "tf-acc-test-visibility-public-%s" + visibility = "public" + vulnerability_alerts = false + } + `, randomID) + + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.public", "visibility", + "public", + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.public", "visibility", + "private", + ), + ), + } + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: reconfigureVisibility(config, "private"), + Check: checks["after"], + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) + + // t.Run("updates repos to public visibility", func(t *testing.T) { + + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-prv-vuln-%s" + // visibility = "private" + // } + // `, randomID) + + // checks := map[string]resource.TestCheckFunc{ + // "before": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "false", + // ), + // ), + // "after": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "true", + // ), + // resource.TestCheckResourceAttr( + // "github_repository.test", "visibility", + // "private", + // ), + // ), + // } + + // testCase := func(t *testing.T, mode string) { + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnlessMode(t, mode) }, + // Providers: testAccProviders, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: checks["before"], + // }, + // { + // Config: strings.Replace(config, + // `}`, + // "vulnerability_alerts = true\n}", 1), + // Check: checks["after"], + // }, + // }, + // }) + // } + + // t.Run("with an anonymous account", func(t *testing.T) { + // t.Skip("anonymous account not supported for this operation") + // }) + + // t.Run("with an individual account", func(t *testing.T) { + // testCase(t, individual) + // }) + + // t.Run("with an organization account", func(t *testing.T) { + // testCase(t, organization) + // }) + // }) + + // t.Run("updates repos to internal visibility", func(t *testing.T) { + + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-prv-vuln-%s" + // visibility = "private" + // } + // `, randomID) + + // checks := map[string]resource.TestCheckFunc{ + // "before": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "false", + // ), + // ), + // "after": resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "github_repository.test", "vulnerability_alerts", + // "true", + // ), + // resource.TestCheckResourceAttr( + // "github_repository.test", "visibility", + // "private", + // ), + // ), + // } + + // testCase := func(t *testing.T, mode string) { + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnlessMode(t, mode) }, + // Providers: testAccProviders, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: checks["before"], + // }, + // { + // Config: strings.Replace(config, + // `}`, + // "vulnerability_alerts = true\n}", 1), + // Check: checks["after"], + // }, + // }, + // }) + // } + + // t.Run("with an anonymous account", func(t *testing.T) { + // t.Skip("anonymous account not supported for this operation") + // }) + + // t.Run("with an individual account", func(t *testing.T) { + // testCase(t, individual) + // }) + + // t.Run("with an organization account", func(t *testing.T) { + // testCase(t, organization) + // }) + // }) + +} + func testSweepRepositories(region string) error { meta, err := sharedConfigForRegion(region) if err != nil { From 4ce5027926e751add584480dbfb986fa7878f921 Mon Sep 17 00:00:00 2001 From: Keegan Campbell Date: Sat, 3 Apr 2021 08:18:28 -0700 Subject: [PATCH 7/9] Uncomment tests --- github/resource_github_repository_test.go | 238 +++++++++++----------- 1 file changed, 119 insertions(+), 119 deletions(-) diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index 5fc8faa0dc..60caa774a0 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -837,125 +837,125 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { }) }) - // t.Run("updates repos to public visibility", func(t *testing.T) { - - // config := fmt.Sprintf(` - // resource "github_repository" "test" { - // name = "tf-acc-test-prv-vuln-%s" - // visibility = "private" - // } - // `, randomID) - - // checks := map[string]resource.TestCheckFunc{ - // "before": resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr( - // "github_repository.test", "vulnerability_alerts", - // "false", - // ), - // ), - // "after": resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr( - // "github_repository.test", "vulnerability_alerts", - // "true", - // ), - // resource.TestCheckResourceAttr( - // "github_repository.test", "visibility", - // "private", - // ), - // ), - // } - - // testCase := func(t *testing.T, mode string) { - // resource.Test(t, resource.TestCase{ - // PreCheck: func() { skipUnlessMode(t, mode) }, - // Providers: testAccProviders, - // Steps: []resource.TestStep{ - // { - // Config: config, - // Check: checks["before"], - // }, - // { - // Config: strings.Replace(config, - // `}`, - // "vulnerability_alerts = true\n}", 1), - // Check: checks["after"], - // }, - // }, - // }) - // } - - // t.Run("with an anonymous account", func(t *testing.T) { - // t.Skip("anonymous account not supported for this operation") - // }) - - // t.Run("with an individual account", func(t *testing.T) { - // testCase(t, individual) - // }) - - // t.Run("with an organization account", func(t *testing.T) { - // testCase(t, organization) - // }) - // }) - - // t.Run("updates repos to internal visibility", func(t *testing.T) { - - // config := fmt.Sprintf(` - // resource "github_repository" "test" { - // name = "tf-acc-test-prv-vuln-%s" - // visibility = "private" - // } - // `, randomID) - - // checks := map[string]resource.TestCheckFunc{ - // "before": resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr( - // "github_repository.test", "vulnerability_alerts", - // "false", - // ), - // ), - // "after": resource.ComposeTestCheckFunc( - // resource.TestCheckResourceAttr( - // "github_repository.test", "vulnerability_alerts", - // "true", - // ), - // resource.TestCheckResourceAttr( - // "github_repository.test", "visibility", - // "private", - // ), - // ), - // } - - // testCase := func(t *testing.T, mode string) { - // resource.Test(t, resource.TestCase{ - // PreCheck: func() { skipUnlessMode(t, mode) }, - // Providers: testAccProviders, - // Steps: []resource.TestStep{ - // { - // Config: config, - // Check: checks["before"], - // }, - // { - // Config: strings.Replace(config, - // `}`, - // "vulnerability_alerts = true\n}", 1), - // Check: checks["after"], - // }, - // }, - // }) - // } - - // t.Run("with an anonymous account", func(t *testing.T) { - // t.Skip("anonymous account not supported for this operation") - // }) - - // t.Run("with an individual account", func(t *testing.T) { - // testCase(t, individual) - // }) - - // t.Run("with an organization account", func(t *testing.T) { - // testCase(t, organization) - // }) - // }) + t.Run("updates repos to public visibility", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-prv-vuln-%s" + visibility = "private" + } + `, randomID) + + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "vulnerability_alerts", + "false", + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "vulnerability_alerts", + "true", + ), + resource.TestCheckResourceAttr( + "github_repository.test", "visibility", + "private", + ), + ), + } + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: strings.Replace(config, + `}`, + "vulnerability_alerts = true\n}", 1), + Check: checks["after"], + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) + + t.Run("updates repos to internal visibility", func(t *testing.T) { + + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-prv-vuln-%s" + visibility = "private" + } + `, randomID) + + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "vulnerability_alerts", + "false", + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "vulnerability_alerts", + "true", + ), + resource.TestCheckResourceAttr( + "github_repository.test", "visibility", + "private", + ), + ), + } + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, mode) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: strings.Replace(config, + `}`, + "vulnerability_alerts = true\n}", 1), + Check: checks["after"], + }, + }, + }) + } + + t.Run("with an anonymous account", func(t *testing.T) { + t.Skip("anonymous account not supported for this operation") + }) + + t.Run("with an individual account", func(t *testing.T) { + testCase(t, individual) + }) + + t.Run("with an organization account", func(t *testing.T) { + testCase(t, organization) + }) + }) } From b94f595dca2dd56a6601e275d7feef31fbbd9e2e Mon Sep 17 00:00:00 2001 From: Keegan Campbell Date: Tue, 6 Apr 2021 16:20:29 -0700 Subject: [PATCH 8/9] Remove specific configuration and instead link to Building the Provider documentation --- examples/repo_org_internal/README.md | 4 +++- examples/repo_org_internal/cli.cfg | 10 ---------- 2 files changed, 3 insertions(+), 11 deletions(-) delete mode 100644 examples/repo_org_internal/cli.cfg diff --git a/examples/repo_org_internal/README.md b/examples/repo_org_internal/README.md index fd21df18f5..20c5fa91e5 100644 --- a/examples/repo_org_internal/README.md +++ b/examples/repo_org_internal/README.md @@ -2,7 +2,9 @@ This demos various repository [visibility settings](https://help.github.com/en/github/administering-a-repository/setting-repository-visibility) for repositories. -This example will create a repositories in the specified `owner` organization. See https://www.terraform.io/docs/providers/github/index.html for details on configuring [`providers.tf`](./providers.tf) accordingly. +This example will create a repository in the specified `owner` organization. See https://www.terraform.io/docs/providers/github/index.html for details on configuring [`providers.tf`](./providers.tf) accordingly. + +In order to build the provider for use with this example, see [Building the Provider docs](https://github.com/integrations/terraform-provider-github/blob/master/CONTRIBUTING.md#building-the-provider) Alternatively, you may use variables passed via command line: diff --git a/examples/repo_org_internal/cli.cfg b/examples/repo_org_internal/cli.cfg deleted file mode 100644 index 87e64e2d8c..0000000000 --- a/examples/repo_org_internal/cli.cfg +++ /dev/null @@ -1,10 +0,0 @@ -provider_installation { - filesystem_mirror { - path = "/Users/jurgen.weber/checkouts/terraform/terraform-provider-github/examples/repo_org_internal/terraform.d/plugins" - include = ["*/*/*"] - } - direct { - exclude = ["*/*/*"] - } -} - From 303db1f3e317e0bb9b17fd28029d13b3a1c93d00 Mon Sep 17 00:00:00 2001 From: Keegan Campbell Date: Tue, 6 Apr 2021 16:25:34 -0700 Subject: [PATCH 9/9] Renamed example directory to match existing pattern --- .../{repo_org_internal => repository_org_internal}/README.md | 0 examples/{repo_org_internal => repository_org_internal}/main.tf | 2 +- .../{repo_org_internal => repository_org_internal}/outputs.tf | 0 .../{repo_org_internal => repository_org_internal}/providers.tf | 0 .../{repo_org_internal => repository_org_internal}/variables.tf | 0 5 files changed, 1 insertion(+), 1 deletion(-) rename examples/{repo_org_internal => repository_org_internal}/README.md (100%) rename examples/{repo_org_internal => repository_org_internal}/main.tf (78%) rename examples/{repo_org_internal => repository_org_internal}/outputs.tf (100%) rename examples/{repo_org_internal => repository_org_internal}/providers.tf (100%) rename examples/{repo_org_internal => repository_org_internal}/variables.tf (100%) diff --git a/examples/repo_org_internal/README.md b/examples/repository_org_internal/README.md similarity index 100% rename from examples/repo_org_internal/README.md rename to examples/repository_org_internal/README.md diff --git a/examples/repo_org_internal/main.tf b/examples/repository_org_internal/main.tf similarity index 78% rename from examples/repo_org_internal/main.tf rename to examples/repository_org_internal/main.tf index d1b976d4c1..873613f80c 100644 --- a/examples/repo_org_internal/main.tf +++ b/examples/repository_org_internal/main.tf @@ -1,5 +1,5 @@ resource github_repository internal { - name = "mtribes-jurgen" + name = "internal-visibility" description = "A internal-visible repository created by Terraform" visibility = "internal" } diff --git a/examples/repo_org_internal/outputs.tf b/examples/repository_org_internal/outputs.tf similarity index 100% rename from examples/repo_org_internal/outputs.tf rename to examples/repository_org_internal/outputs.tf diff --git a/examples/repo_org_internal/providers.tf b/examples/repository_org_internal/providers.tf similarity index 100% rename from examples/repo_org_internal/providers.tf rename to examples/repository_org_internal/providers.tf diff --git a/examples/repo_org_internal/variables.tf b/examples/repository_org_internal/variables.tf similarity index 100% rename from examples/repo_org_internal/variables.tf rename to examples/repository_org_internal/variables.tf