Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed issue labels adoption #2430

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading