From 8ddc44c71b3653590df8a8c828521193ad05b99c 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 | 15 ++++++++++++++- github/resource_github_issue_label_test.go | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/github/resource_github_issue_label.go b/github/resource_github_issue_label.go index 42dfcaa8af..aea5282cc1 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 } diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index 89d5bb0f7c..5ca77e60eb 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