/
issue_label.go
62 lines (51 loc) · 1.62 KB
/
issue_label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package issuelabel
import (
"strings"
"github.com/fatih/color"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"github.com/wesleimp/github-terraform/internal/output"
"github.com/wesleimp/github-terraform/internal/tmpl"
"github.com/wesleimp/github-terraform/pkg/context"
"github.com/wesleimp/github-terraform/pkg/templates"
)
// Import issue labels
func Import(ctx *context.Context) error {
if ctx.Config.IssueLabel.Repo == "" || ctx.Config.IssueLabel.Owner == "" {
return errors.New("Repository and owner property should be informed to import issue labels")
}
err := importIssueLabels(ctx, ctx.Config.IssueLabel.Owner, ctx.Config.IssueLabel.Repo)
if err != nil {
return err
}
return nil
}
func importIssueLabels(ctx *context.Context, owner, repo string) error {
color.New(color.Bold).Printf("Importing issue labels for %s/%s", owner, repo)
ll, _, err := ctx.Client.Issues.ListLabels(ctx, owner, repo, &github.ListOptions{
Page: ctx.Config.IssueLabel.Page,
PerPage: ctx.Config.IssueLabel.PerPage,
})
if err != nil {
return errors.Wrapf(err, "Error listing issue labels for %s/%s", owner, repo)
}
for _, l := range ll {
resName := strings.ReplaceAll(l.GetName(), " ", "_")
content, err := tmpl.New().WithFields(tmpl.Fields{
"Repository": repo,
"Name": l.GetName(),
"ResourceName": resName,
"Color": l.GetColor(),
"Description": l.GetDescription(),
"URL": l.GetURL(),
}).Apply(templates.IssueLabel)
if err != nil {
return err
}
err = output.Save(ctx.Config.IssueLabel.Dest, resName, content)
if err != nil {
return err
}
}
return nil
}