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

Add Org Option to issue_label #2052

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/issue_label/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
Empty file added examples/issue_label/outputs.tf
Empty file.
4 changes: 4 additions & 0 deletions examples/issue_label/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "github" {
owner = var.owner
token = var.github_token
}
24 changes: 24 additions & 0 deletions examples/issue_label/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 23 additions & 3 deletions github/resource_github_issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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())
Expand Down
71 changes: 71 additions & 0 deletions github/resource_github_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"fmt"
"os"
"strings"
"testing"

Expand Down Expand Up @@ -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)
})
})
}
2 changes: 2 additions & 0 deletions website/docs/r/issue_label.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading