From cffa082550b9972fd6beef741032cae158f98310 Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 08:05:21 -0700 Subject: [PATCH 1/7] Create test case for org-level issue --- github/resource_github_issue_label_test.go | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index eebe06b673..d974cf77f9 100644 --- a/github/resource_github_issue_label_test.go +++ b/github/resource_github_issue_label_test.go @@ -2,6 +2,7 @@ package github import ( "fmt" + "os" "strings" "testing" @@ -79,5 +80,56 @@ func TestAccGithubIssueLabel(t *testing.T) { testCase(t, organization) }) }) +} + +func TestAccGithubIssueLabelOrg(t *testing.T) { + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + // run test cases + t.Run("creates and updates labels without error", func(t *testing.T) { + // make a config + description := "test org label" + config := fmt.Sprintf(` + + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_issue_label" "test" { + repository = github_repository.test.id + name = "foo" + color = "000000" + description = "%s" + org = "%s" + } +`, randomID, description, os.Getenv("GITHUB_ORGANIZATION")) + + testCase := func(t *testing.T, mode string) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + // Check: resource.TestCheckResourceAttr("github_issue_label") + }, + }, + }) + } + + 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 eb43fda3ee23bdb4f2cea8560aa45482e9787040 Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 09:34:35 -0700 Subject: [PATCH 2/7] Tweak test case to use org directly --- github/resource_github_issue_label_test.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index d974cf77f9..1fc3e306d1 100644 --- a/github/resource_github_issue_label_test.go +++ b/github/resource_github_issue_label_test.go @@ -84,26 +84,20 @@ func TestAccGithubIssueLabel(t *testing.T) { func TestAccGithubIssueLabelOrg(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + // randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) // run test cases t.Run("creates and updates labels without error", func(t *testing.T) { // make a config description := "test org label" config := fmt.Sprintf(` - - resource "github_repository" "test" { - name = "tf-acc-test-%s" - auto_init = true - } - resource "github_issue_label" "test" { - repository = github_repository.test.id + repository = "%s" name = "foo" color = "000000" description = "%s" org = "%s" } -`, randomID, description, os.Getenv("GITHUB_ORGANIZATION")) +`, os.Getenv("GITHUB_TEMPLATE_REPOSITORY"), description, os.Getenv("GITHUB_ORGANIZATION")) testCase := func(t *testing.T, mode string) { resource.Test(t, resource.TestCase{ From b40fc6c9986feed334bd043dd7b3604fd2447a5e Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 09:34:24 -0700 Subject: [PATCH 3/7] Implement fix --- github/resource_github_issue_label.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/github/resource_github_issue_label.go b/github/resource_github_issue_label.go index 43bd970e6e..d00eabafbc 100644 --- a/github/resource_github_issue_label.go +++ b/github/resource_github_issue_label.go @@ -41,6 +41,11 @@ func resourceGithubIssueLabel() *schema.Resource { Optional: true, Description: "A short description of the label.", }, + "org": { + Type: schema.TypeString, + Optional: true, + Description: "Org ID of an organization for the repository to create this label in, instead of an individual's repository", + }, "url": { Type: schema.TypeString, Computed: true, @@ -66,7 +71,12 @@ func resourceGithubIssueLabel() *schema.Resource { func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { client := meta.(*Owner).v3client - orgName := meta.(*Owner).name + var orgName string + if d.Get("org").(string) == "" { + orgName = meta.(*Owner).name + } else { + orgName = d.Get("org").(string) + } repoName := d.Get("repository").(string) name := d.Get("name").(string) color := d.Get("color").(string) From 35dc26d802da993503f25f45111216a7b94906c0 Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 10:23:55 -0700 Subject: [PATCH 4/7] Add read and delete cases --- github/resource_github_issue_label.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/github/resource_github_issue_label.go b/github/resource_github_issue_label.go index d00eabafbc..a16c49e8d6 100644 --- a/github/resource_github_issue_label.go +++ b/github/resource_github_issue_label.go @@ -44,7 +44,7 @@ func resourceGithubIssueLabel() *schema.Resource { "org": { Type: schema.TypeString, Optional: true, - Description: "Org ID of an organization for the repository to create this label in, instead of an individual's repository", + Description: "Organization for repo to create this label in. Superscedes owner in provider", }, "url": { Type: schema.TypeString, @@ -154,7 +154,12 @@ func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) erro return err } - orgName := meta.(*Owner).name + var orgName string + if d.Get("org").(string) == "" { + orgName = meta.(*Owner).name + } else { + orgName = d.Get("org").(string) + } ctx := context.WithValue(context.Background(), ctxId, d.Id()) if !d.IsNewResource() { ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string)) @@ -190,7 +195,12 @@ func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) erro func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*Owner).v3client - orgName := meta.(*Owner).name + var orgName string + if d.Get("org").(string) == "" { + orgName = meta.(*Owner).name + } else { + orgName = d.Get("org").(string) + } repoName := d.Get("repository").(string) name := d.Get("name").(string) ctx := context.WithValue(context.Background(), ctxId, d.Id()) From 9ed1c0586c0b496ee027da0ffb8d6e7d3860aa1a Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 10:24:02 -0700 Subject: [PATCH 5/7] Add example --- examples/issue_label/main.tf | 14 ++++++++++++++ examples/issue_label/outputs.tf | 0 examples/issue_label/providers.tf | 4 ++++ examples/issue_label/variables.tf | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 examples/issue_label/main.tf create mode 100644 examples/issue_label/outputs.tf create mode 100644 examples/issue_label/providers.tf create mode 100644 examples/issue_label/variables.tf diff --git a/examples/issue_label/main.tf b/examples/issue_label/main.tf new file mode 100644 index 0000000000..1d116c735b --- /dev/null +++ b/examples/issue_label/main.tf @@ -0,0 +1,14 @@ +resource "github_issue_label" "individual_repo_label" { + repository = var.individual_repo + name = "foo" + color = "000000" + description = "Test issue" +} + + resource "github_issue_label" "org_repo_label" { + repository = var.org_repo + name = "bar" + color = "000000" + description = "Test issue" + org = var.org +} \ No newline at end of file diff --git a/examples/issue_label/outputs.tf b/examples/issue_label/outputs.tf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/examples/issue_label/providers.tf b/examples/issue_label/providers.tf new file mode 100644 index 0000000000..07836b6b1a --- /dev/null +++ b/examples/issue_label/providers.tf @@ -0,0 +1,4 @@ +provider "github" { + owner = var.owner + token = var.github_token +} diff --git a/examples/issue_label/variables.tf b/examples/issue_label/variables.tf new file mode 100644 index 0000000000..71b9e65ce8 --- /dev/null +++ b/examples/issue_label/variables.tf @@ -0,0 +1,24 @@ +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 +} + +variable "individual_repo" { + description = "Name of repo owned by an individual to create the issue in" + type = string +} + +variable "org_repo" { + description = "Name of repo owned by an organization to create the issue in" + type = string +} + +variable "org" { + description = "Name of an org that owns the repo specified in org_repo" + type = string +} \ No newline at end of file From 22be86b1d23c988cbf826d2017ed00f418c11bc2 Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 10:34:39 -0700 Subject: [PATCH 6/7] Wrap up test case and add sample repo --- github/resource_github_issue_label_test.go | 53 ++++++++++++++++------ 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index 1fc3e306d1..3da24730be 100644 --- a/github/resource_github_issue_label_test.go +++ b/github/resource_github_issue_label_test.go @@ -84,20 +84,41 @@ func TestAccGithubIssueLabel(t *testing.T) { func TestAccGithubIssueLabelOrg(t *testing.T) { - // randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - // run test cases + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + t.Run("creates and updates labels without error", func(t *testing.T) { + description := "label_description_org" + updatedDescription := "updated_label_description_org" // make a config - description := "test org label" config := fmt.Sprintf(` - resource "github_issue_label" "test" { - repository = "%s" - name = "foo" - color = "000000" - description = "%s" - org = "%s" - } -`, os.Getenv("GITHUB_TEMPLATE_REPOSITORY"), description, os.Getenv("GITHUB_ORGANIZATION")) + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } + + resource "github_issue_label" "test" { + repository = github_repository.test.id + name = "foo" + color = "000000" + description = "%s" + org = "%s" + } + `, randomID, description, os.Getenv("GITHUB_ORGANIZATION")) + + checks := map[string]resource.TestCheckFunc{ + "before": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_issue_label.test", "description", + description, + ), + ), + "after": resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_issue_label.test", "description", + updatedDescription, + ), + ), + } testCase := func(t *testing.T, mode string) { resource.Test(t, resource.TestCase{ @@ -106,7 +127,13 @@ func TestAccGithubIssueLabelOrg(t *testing.T) { Steps: []resource.TestStep{ { Config: config, - // Check: resource.TestCheckResourceAttr("github_issue_label") + Check: checks["before"], + }, + { + Config: strings.Replace(config, + description, + updatedDescription, 1), + Check: checks["after"], }, }, }) @@ -123,7 +150,5 @@ func TestAccGithubIssueLabelOrg(t *testing.T) { t.Run("with an organization account", func(t *testing.T) { testCase(t, organization) }) - }) - } From f65379915c4e50c84063738a6c65fc35a33c4d2d Mon Sep 17 00:00:00 2001 From: ikegentz Date: Sat, 9 Dec 2023 10:41:07 -0700 Subject: [PATCH 7/7] Update website documentation --- website/docs/r/issue_label.html.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/docs/r/issue_label.html.markdown b/website/docs/r/issue_label.html.markdown index d3989e5759..484d3a2865 100644 --- a/website/docs/r/issue_label.html.markdown +++ b/website/docs/r/issue_label.html.markdown @@ -43,6 +43,8 @@ The following arguments are supported: * `description` - (Optional) A short description of the label. +* `org` - (Optional) Organization for repo to create this label in. Superscedes owner in provider + * `url` - (Computed) The URL to the issue label ## Import