Skip to content

Commit

Permalink
Merge pull request #122 from terraform-providers/cleanup-issue-label
Browse files Browse the repository at this point in the history
Cleanup github_issue_label
  • Loading branch information
radeksimko authored Aug 13, 2018
2 parents 42b14da + 39b4d0f commit 06d54d2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
69 changes: 40 additions & 29 deletions github/resource_github_issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,50 +56,57 @@ func resourceGithubIssueLabel() *schema.Resource {

func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
o := meta.(*Organization).name
r := d.Get("repository").(string)
n := d.Get("name").(string)
c := d.Get("color").(string)
orgName := meta.(*Organization).name
repoName := d.Get("repository").(string)
name := d.Get("name").(string)
color := d.Get("color").(string)

label := &github.Label{
Name: &n,
Color: &c,
Name: github.String(name),
Color: github.String(color),
}

log.Printf("[DEBUG] Querying label existence %s/%s (%s)", o, r, n)
existing, _, _ := client.Issues.GetLabel(context.TODO(), o, r, n)
log.Printf("[DEBUG] Querying label existence %s/%s (%s)",
orgName, repoName, name)
existing, resp, err := client.Issues.GetLabel(context.TODO(),
orgName, repoName, name)
if err != nil && resp.StatusCode != 404 {
return err
}

if existing != nil {
description := d.Get("description").(string)
label.Description = &description
label.Description = github.String(d.Get("description").(string))

log.Printf("[DEBUG] Updating label: %s/%s (%s: %s)", o, r, n, c)
log.Printf("[DEBUG] Updating label: %s/%s (%s: %s)",
orgName, repoName, name, color)

// Pull out the original name. If we already have a resource, this is the
// parsed ID. If not, it's the value given to the resource.
var oname string
var originalName string
if d.Id() == "" {
oname = n
originalName = name
} else {
var err error
_, oname, err = parseTwoPartID(d.Id())
_, originalName, err = parseTwoPartID(d.Id())
if err != nil {
return err
}
}

_, _, err := client.Issues.EditLabel(context.TODO(), o, r, oname, label)
_, _, err := client.Issues.EditLabel(context.TODO(),
orgName, repoName, originalName, label)
if err != nil {
return err
}
} else {
if v, ok := d.GetOk("description"); ok {
description := v.(string)
label.Description = &description
label.Description = github.String(v.(string))
}

log.Printf("[DEBUG] Creating label: %s/%s (%s: %s)", o, r, n, c)
_, resp, err := client.Issues.CreateLabel(context.TODO(), o, r, label)
log.Printf("[DEBUG] Creating label: %s/%s (%s: %s)",
orgName, repoName, name, color)
_, resp, err := client.Issues.CreateLabel(context.TODO(),
orgName, repoName, label)
if resp != nil {
log.Printf("[DEBUG] Response from creating label: %#v", *resp)
}
Expand All @@ -108,27 +115,29 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interfa
}
}

d.SetId(buildTwoPartID(&r, &n))
d.SetId(buildTwoPartID(&repoName, &name))

return resourceGithubIssueLabelRead(d, meta)
}

func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r, n, err := parseTwoPartID(d.Id())
repoName, name, err := parseTwoPartID(d.Id())
if err != nil {
return err
}

log.Printf("[DEBUG] Reading label: %s/%s", r, n)
githubLabel, _, err := client.Issues.GetLabel(context.TODO(), meta.(*Organization).name, r, n)
log.Printf("[DEBUG] Reading label: %s/%s", repoName, name)
githubLabel, _, err := client.Issues.GetLabel(context.TODO(),
meta.(*Organization).name, repoName, name)
if err != nil {
log.Printf("[WARN] GitHub Issue Label (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("repository", r)
d.Set("name", n)
d.Set("repository", repoName)
d.Set("name", name)
d.Set("color", githubLabel.Color)
d.Set("description", githubLabel.Description)
d.Set("url", githubLabel.URL)
Expand All @@ -138,10 +147,12 @@ func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) erro

func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Organization).client
r := d.Get("repository").(string)
n := d.Get("name").(string)
repoName := d.Get("repository").(string)
name := d.Get("name").(string)

log.Printf("[DEBUG] Deleting label: %s/%s", repoName, name)
_, err := client.Issues.DeleteLabel(context.TODO(),
meta.(*Organization).name, repoName, name)

log.Printf("[DEBUG] Deleting label: %s/%s", r, n)
_, err := client.Issues.DeleteLabel(context.TODO(), meta.(*Organization).name, r, n)
return err
}
15 changes: 8 additions & 7 deletions github/resource_github_issue_label_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,14 @@ func testAccCheckGithubIssueLabelExists(n string, label *github.Label) resource.
}

conn := testAccProvider.Meta().(*Organization).client
o := testAccProvider.Meta().(*Organization).name
r, n, err := parseTwoPartID(rs.Primary.ID)
orgName := testAccProvider.Meta().(*Organization).name
repoName, name, err := parseTwoPartID(rs.Primary.ID)
if err != nil {
return err
}

githubLabel, _, err := conn.Issues.GetLabel(context.TODO(), o, r, n)
githubLabel, _, err := conn.Issues.GetLabel(context.TODO(),
orgName, repoName, name)
if err != nil {
return err
}
Expand Down Expand Up @@ -178,14 +179,14 @@ func testAccGithubIssueLabelDestroy(s *terraform.State) error {
continue
}

o := testAccProvider.Meta().(*Organization).name
r, n, err := parseTwoPartID(rs.Primary.ID)
orgName := testAccProvider.Meta().(*Organization).name
repoName, name, err := parseTwoPartID(rs.Primary.ID)
if err != nil {
return err
}

label, res, err := conn.Issues.GetLabel(context.TODO(), o, r, n)

label, res, err := conn.Issues.GetLabel(context.TODO(),
orgName, repoName, name)
if err == nil {
if label != nil &&
buildTwoPartID(label.Name, label.Color) == rs.Primary.ID {
Expand Down

0 comments on commit 06d54d2

Please sign in to comment.