Skip to content

Commit

Permalink
Remove intermediate service
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 7, 2020
1 parent 511781a commit 3fa9c61
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
17 changes: 7 additions & 10 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *CLI) Run(args []string) error {
}

gc := &githubClient{
github: client,
Client: client,
dryRun: c.Option.DryRun,
logger: log.New(os.Stdout, "labeler: ", log.Ldate|log.Ltime),
}
Expand All @@ -41,9 +41,6 @@ func (c *CLI) Run(args []string) error {
gc.logger.SetPrefix("labeler (dry-run): ")
}

gc.common.client = gc
gc.Label = (*LabelService)(&gc.common)

c.Client = gc
c.Config = m

Expand Down Expand Up @@ -79,21 +76,21 @@ func (c *CLI) Run(args []string) error {

// applyLabels creates/edits labels described in YAML
func (c *CLI) applyLabels(owner, repo string, label Label) error {
ghLabel, err := c.Client.Label.Get(owner, repo, label)
ghLabel, err := c.Client.GetLabel(owner, repo, label)
if err != nil {
return c.Client.Label.Create(owner, repo, label)
return c.Client.CreateLabel(owner, repo, label)
}

if ghLabel.Description != label.Description || ghLabel.Color != label.Color {
return c.Client.Label.Edit(owner, repo, label)
return c.Client.EditLabel(owner, repo, label)
}

return nil
}

// deleteLabels deletes the label not described in YAML but exists on GitHub
func (c *CLI) deleteLabels(owner, repo string) error {
labels, err := c.Client.Label.List(owner, repo)
labels, err := c.Client.ListLabels(owner, repo)
if err != nil {
return err
}
Expand All @@ -103,7 +100,7 @@ func (c *CLI) deleteLabels(owner, repo string) error {
// no need to delete
continue
}
err := c.Client.Label.Delete(owner, repo, label)
err := c.Client.DeleteLabel(owner, repo, label)
if err != nil {
return err
}
Expand Down Expand Up @@ -139,7 +136,7 @@ func (c *CLI) CurrentLabels() Manifest {
// TODO: handle error
continue
}
labels, err := c.Client.Label.List(e[0], e[1])
labels, err := c.Client.ListLabels(e[0], e[1])
if err != nil {
// TODO: handle error
continue
Expand Down
15 changes: 1 addition & 14 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,10 @@ type Option struct {
}

type githubClient struct {
// github *github.Client
github *github.Client
*github.Client

dryRun bool
logger *log.Logger

common service

Label *LabelService
}

type Labeler interface {
Expand All @@ -75,11 +70,3 @@ type Labeler interface {
ListLabels(ctx context.Context, owner string, repo string, opt *github.ListOptions) ([]*github.Label, *github.Response, error)
DeleteLabel(ctx context.Context, owner string, repo string, name string) (*github.Response, error)
}

// LabelService handles communication with the label related
// methods of GitHub API
type LabelService service

type service struct {
client *githubClient
}
38 changes: 19 additions & 19 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

// Get gets GitHub labels
func (g *LabelService) Get(owner, repo string, label Label) (Label, error) {
func (c *githubClient) GetLabel(owner, repo string, label Label) (Label, error) {
ctx := context.Background()
ghLabel, _, err := g.client.github.Issues.GetLabel(ctx, owner, repo, label.Name)
ghLabel, _, err := c.Issues.GetLabel(ctx, owner, repo, label.Name)
if err != nil {
return Label{}, err
}
Expand All @@ -21,52 +21,52 @@ func (g *LabelService) Get(owner, repo string, label Label) (Label, error) {
}

// Create creates GitHub labels
func (g *LabelService) Create(owner, repo string, label Label) error {
func (c *githubClient) CreateLabel(owner, repo string, label Label) error {
ctx := context.Background()
ghLabel := &github.Label{
Name: github.String(label.Name),
Description: github.String(label.Description),
Color: github.String(label.Color),
}
if len(label.PreviousName) > 0 {
g.client.logger.Printf("rename %q in %s/%s to %q", label.PreviousName, owner, repo, label.Name)
if g.client.dryRun {
c.logger.Printf("rename %q in %s/%s to %q", label.PreviousName, owner, repo, label.Name)
if c.dryRun {
return nil
}
_, _, err := g.client.github.Issues.EditLabel(ctx, owner, repo, label.PreviousName, ghLabel)
_, _, err := c.Issues.EditLabel(ctx, owner, repo, label.PreviousName, ghLabel)
return err
}
g.client.logger.Printf("create %q in %s/%s", label.Name, owner, repo)
if g.client.dryRun {
c.logger.Printf("create %q in %s/%s", label.Name, owner, repo)
if c.dryRun {
return nil
}
_, _, err := g.client.github.Issues.CreateLabel(ctx, owner, repo, ghLabel)
_, _, err := c.Issues.CreateLabel(ctx, owner, repo, ghLabel)
return err
}

// Edit edits GitHub labels
func (g *LabelService) Edit(owner, repo string, label Label) error {
func (c *githubClient) EditLabel(owner, repo string, label Label) error {
ctx := context.Background()
ghLabel := &github.Label{
Name: github.String(label.Name),
Description: github.String(label.Description),
Color: github.String(label.Color),
}
g.client.logger.Printf("edit %q in %s/%s", label.Name, owner, repo)
if g.client.dryRun {
c.logger.Printf("edit %q in %s/%s", label.Name, owner, repo)
if c.dryRun {
return nil
}
_, _, err := g.client.github.Issues.EditLabel(ctx, owner, repo, label.Name, ghLabel)
_, _, err := c.Issues.EditLabel(ctx, owner, repo, label.Name, ghLabel)
return err
}

// List lists GitHub labels
func (g *LabelService) List(owner, repo string) ([]Label, error) {
func (c *githubClient) ListLabels(owner, repo string) ([]Label, error) {
ctx := context.Background()
opt := &github.ListOptions{PerPage: 10}
var labels []Label
for {
ghLabels, resp, err := g.client.github.Issues.ListLabels(ctx, owner, repo, opt)
ghLabels, resp, err := c.Issues.ListLabels(ctx, owner, repo, opt)
if err != nil {
return labels, err
}
Expand All @@ -86,12 +86,12 @@ func (g *LabelService) List(owner, repo string) ([]Label, error) {
}

// Delete deletes GitHub labels
func (g *LabelService) Delete(owner, repo string, label Label) error {
func (c *githubClient) DeleteLabel(owner, repo string, label Label) error {
ctx := context.Background()
g.client.logger.Printf("delete %q in %s/%s", label.Name, owner, repo)
if g.client.dryRun {
c.logger.Printf("delete %q in %s/%s", label.Name, owner, repo)
if c.dryRun {
return nil
}
_, err := g.client.github.Issues.DeleteLabel(ctx, owner, repo, label.Name)
_, err := c.Issues.DeleteLabel(ctx, owner, repo, label.Name)
return err
}

0 comments on commit 3fa9c61

Please sign in to comment.