Skip to content

Commit

Permalink
fix: Fixed issue labels adoption
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Hipwell <[email protected]>
  • Loading branch information
stevehipwell committed Nov 21, 2024
1 parent 84c6bd2 commit d09befb
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 164 deletions.
50 changes: 4 additions & 46 deletions github/data_source_github_issue_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package github

import (
"context"
"fmt"

"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -49,59 +47,19 @@ func dataSourceGithubIssueLabelsRead(d *schema.ResourceData, meta interface{}) e
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repository := d.Get("repository").(string)

ctx := context.Background()
opts := &github.ListOptions{
PerPage: maxPerPage,
}

d.SetId(repository)

allLabels := make([]interface{}, 0)
for {
labels, resp, err := client.Issues.ListLabels(ctx, owner, repository, opts)
if err != nil {
return err
}

result, err := flattenLabels(labels)
if err != nil {
return fmt.Errorf("unable to flatten GitHub Labels (Owner: %q/Repository: %q) : %+v", owner, repository, err)
}

allLabels = append(allLabels, result...)

if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
labels, err := listLabels(client, ctx, owner, repository)
if err != nil {
return err
}

err := d.Set("labels", allLabels)
err = d.Set("labels", flattenLabels(labels))
if err != nil {
return err
}

return nil
}

func flattenLabels(labels []*github.Label) ([]interface{}, error) {
if labels == nil {
return make([]interface{}, 0), nil
}

results := make([]interface{}, 0)

for _, l := range labels {
result := make(map[string]interface{})

result["name"] = l.GetName()
result["color"] = l.GetColor()
result["description"] = l.GetDescription()
result["url"] = l.GetURL()

results = append(results, result)
}

return results, nil
}
155 changes: 56 additions & 99 deletions github/resource_github_issue_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package github

import (
"context"
"fmt"
"log"
"strings"

"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -61,49 +61,23 @@ func resourceGithubIssueLabels() *schema.Resource {

func resourceGithubIssueLabelsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client

owner := meta.(*Owner).name
repository := d.Id()

log.Printf("[DEBUG] Reading GitHub issue labels for %s/%s", owner, repository)

ctx := context.WithValue(context.Background(), ctxId, repository)

options := &github.ListOptions{
PerPage: maxPerPage,
}

labels := make([]map[string]interface{}, 0)

for {
ls, resp, err := client.Issues.ListLabels(ctx, owner, repository, options)
if err != nil {
return err
}
for _, l := range ls {
labels = append(labels, map[string]interface{}{
"name": l.GetName(),
"color": l.GetColor(),
"description": l.GetDescription(),
"url": l.GetURL(),
})
}
log.Printf("[DEBUG] Reading GitHub issue labels for %s/%s", owner, repository)

if resp.NextPage == 0 {
break
}
options.Page = resp.NextPage
labels, err := listLabels(client, ctx, owner, repository)
if err != nil {
return err
}

log.Printf("[DEBUG] Found %d GitHub issue labels for %s/%s", len(labels), owner, repository)
log.Printf("[DEBUG] Labels: %v", labels)

err := d.Set("repository", repository)
err = d.Set("repository", repository)
if err != nil {
return err
}

err = d.Set("label", labels)
err = d.Set("label", flattenLabels(labels))
if err != nil {
return err
}
Expand All @@ -113,97 +87,82 @@ func resourceGithubIssueLabelsRead(d *schema.ResourceData, meta interface{}) err

func resourceGithubIssueLabelsCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client

owner := meta.(*Owner).name
repository := d.Get("repository").(string)
ctx := context.WithValue(context.Background(), ctxId, repository)

o, n := d.GetChange("label")
wantLabels := d.Get("label").(*schema.Set).List()

log.Printf("[DEBUG] Updating GitHub issue labels for %s/%s", owner, repository)
log.Printf("[DEBUG] Old labels: %v", o)
log.Printf("[DEBUG] New labels: %v", n)

oMap := make(map[string]map[string]interface{})
nMap := make(map[string]map[string]interface{})
for _, raw := range o.(*schema.Set).List() {
m := raw.(map[string]interface{})
name := strings.ToLower(m["name"].(string))
oMap[name] = m
wantLabelsMap := make(map[string]interface{}, len(wantLabels))
for _, label := range wantLabels {
name := label.(map[string]interface{})["name"].(string)
if _, found := wantLabelsMap[name]; found {
return fmt.Errorf("duplicate set label: %s", name)
}
wantLabelsMap[name] = label
}
for _, raw := range n.(*schema.Set).List() {
m := raw.(map[string]interface{})
name := strings.ToLower(m["name"].(string))
nMap[name] = m

hasLabels, err := listLabels(client, ctx, owner, repository)
if err != nil {
return err
}

labels := make([]map[string]interface{}, 0)
log.Printf("[DEBUG] Updating GitHub issue labels for %s/%s", owner, repository)

// create
for name, n := range nMap {
if _, ok := oMap[name]; !ok {
log.Printf("[DEBUG] Creating GitHub issue label %s/%s/%s", owner, repository, name)
hasLabelsMap := make(map[string]struct{}, len(hasLabels))
for _, hasLabel := range hasLabels {
name := hasLabel.GetName()
wantLabel, found := wantLabelsMap[name]
if found {
labelData := wantLabel.(map[string]interface{})
description := labelData["description"].(string)
color := labelData["color"].(string)
if hasLabel.GetDescription() != description || hasLabel.GetColor() != color {
log.Printf("[DEBUG] Updating GitHub issue label %s/%s/%s", owner, repository, name)

label, _, err := client.Issues.CreateLabel(ctx, owner, repository, &github.Label{
Name: github.String(n["name"].(string)),
Color: github.String(n["color"].(string)),
Description: github.String(n["description"].(string)),
})
if err != nil {
return err
_, _, err := client.Issues.EditLabel(ctx, owner, repository, name, &github.Label{
Name: github.String(name),
Description: github.String(description),
Color: github.String(color),
})
if err != nil {
return err
}
}

labels = append(labels, map[string]interface{}{
"name": label.GetName(),
"color": label.GetColor(),
"description": label.GetDescription(),
"url": label.GetURL(),
})
}
}

// delete
for name, o := range oMap {
if _, ok := nMap[name]; !ok {
} else {
log.Printf("[DEBUG] Deleting GitHub issue label %s/%s/%s", owner, repository, name)

_, err := client.Issues.DeleteLabel(ctx, owner, repository, o["name"].(string))
_, err := client.Issues.DeleteLabel(ctx, owner, repository, name)
if err != nil {
return err
}
}

hasLabelsMap[name] = struct{}{}
}

// update
for name, n := range nMap {
if o, ok := oMap[name]; ok {
if o["name"] != n["name"] || o["color"] != n["color"] || o["description"] != n["description"] {
log.Printf("[DEBUG] Updating GitHub issue label %s/%s/%s", owner, repository, name)
for _, l := range wantLabels {
labelData := l.(map[string]interface{})
name := labelData["name"].(string)

label, _, err := client.Issues.EditLabel(ctx, owner, repository, name, &github.Label{
Name: github.String(n["name"].(string)),
Color: github.String(n["color"].(string)),
Description: github.String(n["description"].(string)),
})
if err != nil {
return err
}
_, found := hasLabelsMap[name]
if !found {
log.Printf("[DEBUG] Creating GitHub issue label %s/%s/%s", owner, repository, name)

labels = append(labels, map[string]interface{}{
"name": label.GetName(),
"color": label.GetColor(),
"description": label.GetDescription(),
"url": label.GetURL(),
})
} else {
labels = append(labels, o)
_, _, err := client.Issues.CreateLabel(ctx, owner, repository, &github.Label{
Name: github.String(name),
Description: github.String(labelData["description"].(string)),
Color: github.String(labelData["color"].(string)),
})
if err != nil {
return err
}
}
}

d.SetId(repository)

err := d.Set("label", labels)
err = d.Set("label", wantLabels)
if err != nil {
return err
}
Expand All @@ -213,15 +172,13 @@ func resourceGithubIssueLabelsCreateOrUpdate(d *schema.ResourceData, meta interf

func resourceGithubIssueLabelsDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client

owner := meta.(*Owner).name
repository := d.Get("repository").(string)
ctx := context.WithValue(context.Background(), ctxId, repository)

labels := d.Get("label").(*schema.Set).List()

log.Printf("[DEBUG] Deleting GitHub issue labels for %s/%s", owner, repository)
log.Printf("[DEBUG] Labels: %v", labels)

// delete
for _, raw := range labels {
Expand Down
Loading

0 comments on commit d09befb

Please sign in to comment.