2016-11-18 12:24:37 +01:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2017-03-12 15:05:18 +01:00
|
|
|
"context"
|
2017-03-17 11:47:43 +01:00
|
|
|
"log"
|
2017-03-12 15:05:18 +01:00
|
|
|
|
2016-11-18 12:24:37 +01:00
|
|
|
"github.com/google/go-github/github"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceGithubIssueLabel() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
2017-03-30 00:26:54 +02:00
|
|
|
Create: resourceGithubIssueLabelCreateOrUpdate,
|
2016-11-18 12:24:37 +01:00
|
|
|
Read: resourceGithubIssueLabelRead,
|
2017-03-30 00:26:54 +02:00
|
|
|
Update: resourceGithubIssueLabelCreateOrUpdate,
|
2016-11-18 12:24:37 +01:00
|
|
|
Delete: resourceGithubIssueLabelDelete,
|
2016-11-28 18:30:24 +01:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-11-18 12:24:37 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-12 15:05:18 +01:00
|
|
|
"repository": {
|
2016-11-18 12:24:37 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"name": {
|
2016-11-18 12:24:37 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"color": {
|
2016-11-18 12:24:37 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-03-12 15:05:18 +01:00
|
|
|
"url": {
|
2016-11-18 12:24:37 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-30 00:26:54 +02:00
|
|
|
// resourceGithubIssueLabelCreateOrUpdate idempotently creates or updates an
|
|
|
|
// issue label. Issue labels are keyed off of their "name", so pre-existing
|
|
|
|
// issue labels result in a 422 HTTP error if they exist outside of Terraform.
|
|
|
|
// Normally this would not be an issue, except new repositories are created with
|
|
|
|
// a "default" set of labels, and those labels easily conflict with custom ones.
|
|
|
|
//
|
|
|
|
// This function will first check if the label exists, and then issue an update,
|
|
|
|
// otherwise it will create. This is also advantageous in that we get to use the
|
|
|
|
// same function for two schema funcs.
|
|
|
|
|
|
|
|
func resourceGithubIssueLabelCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
|
2016-11-18 12:24:37 +01:00
|
|
|
client := meta.(*Organization).client
|
2017-03-30 00:26:54 +02:00
|
|
|
o := meta.(*Organization).name
|
2016-11-18 12:24:37 +01:00
|
|
|
r := d.Get("repository").(string)
|
|
|
|
n := d.Get("name").(string)
|
|
|
|
c := d.Get("color").(string)
|
2017-03-30 00:26:54 +02:00
|
|
|
|
|
|
|
label := &github.Label{
|
2016-11-18 12:24:37 +01:00
|
|
|
Name: &n,
|
|
|
|
Color: &c,
|
2017-03-17 11:47:43 +01:00
|
|
|
}
|
|
|
|
|
2017-03-30 00:26:54 +02:00
|
|
|
log.Printf("[DEBUG] Querying label existence %s/%s (%s)", o, r, n)
|
|
|
|
existing, _, _ := client.Issues.GetLabel(context.TODO(), o, r, n)
|
|
|
|
|
|
|
|
if existing != nil {
|
|
|
|
log.Printf("[DEBUG] Updating label: %s/%s (%s: %s)", o, r, n, c)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
if d.Id() == "" {
|
|
|
|
oname = n
|
|
|
|
} else {
|
|
|
|
_, oname = parseTwoPartID(d.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := client.Issues.EditLabel(context.TODO(), o, r, oname, label)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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] Response from creating label: %s", *resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-18 12:24:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(buildTwoPartID(&r, &n))
|
|
|
|
|
|
|
|
return resourceGithubIssueLabelRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubIssueLabelRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*Organization).client
|
2016-11-28 18:30:24 +01:00
|
|
|
r, n := parseTwoPartID(d.Id())
|
2016-11-18 12:24:37 +01:00
|
|
|
|
2017-03-30 00:26:54 +02:00
|
|
|
log.Printf("[DEBUG] Reading label: %s/%s", r, n)
|
2017-03-12 15:05:18 +01:00
|
|
|
githubLabel, _, err := client.Issues.GetLabel(context.TODO(), meta.(*Organization).name, r, n)
|
2016-11-18 12:24:37 +01:00
|
|
|
if err != nil {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-28 18:30:24 +01:00
|
|
|
d.Set("repository", r)
|
|
|
|
d.Set("name", n)
|
2016-11-18 12:24:37 +01:00
|
|
|
d.Set("color", githubLabel.Color)
|
|
|
|
d.Set("url", githubLabel.URL)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGithubIssueLabelDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*Organization).client
|
|
|
|
r := d.Get("repository").(string)
|
|
|
|
n := d.Get("name").(string)
|
|
|
|
|
2017-03-30 00:26:54 +02:00
|
|
|
log.Printf("[DEBUG] Deleting label: %s/%s", r, n)
|
2017-03-12 15:05:18 +01:00
|
|
|
_, err := client.Issues.DeleteLabel(context.TODO(), meta.(*Organization).name, r, n)
|
2016-11-18 12:24:37 +01:00
|
|
|
return err
|
|
|
|
}
|