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 diff --git a/github/resource_github_issue_label.go b/github/resource_github_issue_label.go index 43bd970e6e..a16c49e8d6 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: "Organization for repo to create this label in. Superscedes owner in provider", + }, "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) @@ -144,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)) @@ -180,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()) diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index eebe06b673..3da24730be 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,75 @@ func TestAccGithubIssueLabel(t *testing.T) { testCase(t, organization) }) }) +} + +func TestAccGithubIssueLabelOrg(t *testing.T) { + + 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 + 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")) + + 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{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], + }, + { + Config: strings.Replace(config, + description, + updatedDescription, 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) + }) + }) } 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