From 4806d30678eac2a6f35312308c8fc8caa47c5246 Mon Sep 17 00:00:00 2001 From: Shouichi Kamiya Date: Mon, 21 Oct 2019 10:35:14 +0900 Subject: [PATCH] Fix not to recreate labels when update Close #173. --- github/resource_github_issue_label.go | 28 +++++++++++----------- github/resource_github_issue_label_test.go | 16 ++++++++++--- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/github/resource_github_issue_label.go b/github/resource_github_issue_label.go index f510ce2524..292f40150a 100644 --- a/github/resource_github_issue_label.go +++ b/github/resource_github_issue_label.go @@ -80,10 +80,23 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interfa ctx = context.WithValue(ctx, ctxId, d.Id()) } + // 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 originalName string + if d.Id() == "" { + originalName = name + } else { + var err error + _, originalName, err = parseTwoPartID(d.Id()) + if err != nil { + return err + } + } + log.Printf("[DEBUG] Querying label existence: %s (%s/%s)", name, orgName, repoName) existing, resp, err := client.Issues.GetLabel(ctx, - orgName, repoName, name) + orgName, repoName, originalName) if err != nil && resp.StatusCode != http.StatusNotFound { return err } @@ -94,19 +107,6 @@ func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interfa log.Printf("[DEBUG] Updating label: %s:%s (%s/%s)", name, color, orgName, repoName) - // 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 originalName string - if d.Id() == "" { - originalName = name - } else { - var err error - _, originalName, err = parseTwoPartID(d.Id()) - if err != nil { - return err - } - } - _, _, err := client.Issues.EditLabel(ctx, orgName, repoName, originalName, label) if err != nil { diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index fc7a206460..bc2382e8a3 100644 --- a/github/resource_github_issue_label_test.go +++ b/github/resource_github_issue_label_test.go @@ -12,7 +12,7 @@ import ( ) func TestAccGithubIssueLabel_basic(t *testing.T) { - var label github.Label + var label, updatedLabel github.Label rn := "github_issue_label.test" rString := acctest.RandString(5) @@ -33,8 +33,9 @@ func TestAccGithubIssueLabel_basic(t *testing.T) { { Config: testAccGithubIssueLabelUpdateConfig(repoName), Check: resource.ComposeTestCheckFunc( - testAccCheckGithubIssueLabelExists(rn, &label), - testAccCheckGithubIssueLabelAttributes(&label, "bar", "FFFFFF"), + testAccCheckGithubIssueLabelExists(rn, &updatedLabel), + testAccCheckGithubIssueLabelAttributes(&updatedLabel, "bar", "FFFFFF"), + testAccCheckGithubIssueLabelIDUnchanged(&label, &updatedLabel), ), }, { @@ -168,6 +169,15 @@ func testAccCheckGithubIssueLabelAttributes(label *github.Label, name, color str } } +func testAccCheckGithubIssueLabelIDUnchanged(label, updatedLabel *github.Label) resource.TestCheckFunc { + return func(_ *terraform.State) error { + if *label.ID != *updatedLabel.ID { + return fmt.Errorf("label was recreated") + } + return nil + } +} + func testAccGithubIssueLabelDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*Organization).client