Skip to content

Commit

Permalink
Make more simple
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 7, 2020
1 parent 93cb8f3 commit d2f27d7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
38 changes: 15 additions & 23 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type CLI struct {
Stderr io.Writer
Option Option

GitHub *App
Config Config
Labeler Labeler
Logger *log.Logger
Config Config
}

type Option struct {
Expand All @@ -31,11 +32,6 @@ type Option struct {
Version bool `long:"version" description:"Show version"`
}

type App struct {
Labeler Labeler
logger *log.Logger
}

func (c *CLI) Run(args []string) error {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
Expand All @@ -52,28 +48,24 @@ func (c *CLI) Run(args []string) error {
if err != nil {
return err
}
c.Config = cfg

gc := &App{
Labeler: githubClientImpl{client},
logger: log.New(os.Stdout, "labeler: ", log.Ldate|log.Ltime),
}
c.Labeler = githubClientImpl{client}
c.Logger = log.New(os.Stdout, "labeler: ", log.Ldate|log.Ltime)

if c.Option.DryRun {
gc.Labeler = githubClientDryRun{client}
gc.logger.SetPrefix("labeler (dry-run): ")
c.Labeler = githubClientDryRun{client}
c.Logger.SetPrefix("labeler (dry-run): ")
}

c.GitHub = gc
c.Config = cfg

if len(c.Config.Repos) == 0 {
return fmt.Errorf("no repos found in %s", c.Option.Config)
}

actual := c.ActualConfig()
if cmp.Equal(actual, c.Config) {
// no need to sync
gc.logger.Printf("Claimed config and actual config is the same")
c.Logger.Printf("Claimed config and actual config is the same")
return nil
}

Expand All @@ -99,21 +91,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.GitHub.GetLabel(owner, repo, label)
ghLabel, err := c.GetLabel(owner, repo, label)
if err != nil {
return c.GitHub.CreateLabel(owner, repo, label)
return c.CreateLabel(owner, repo, label)
}

if ghLabel.Description != label.Description || ghLabel.Color != label.Color {
return c.GitHub.EditLabel(owner, repo, label)
return c.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.GitHub.ListLabels(owner, repo)
labels, err := c.ListLabels(owner, repo)
if err != nil {
return err
}
Expand All @@ -123,7 +115,7 @@ func (c *CLI) deleteLabels(owner, repo string) error {
// no need to delete
continue
}
err := c.GitHub.DeleteLabel(owner, repo, label)
err := c.DeleteLabel(owner, repo, label)
if err != nil {
return err
}
Expand Down Expand Up @@ -159,7 +151,7 @@ func (c *CLI) ActualConfig() Config {
// TODO: handle error
continue
}
labels, err := c.GitHub.ListLabels(slugs[0], slugs[1])
labels, err := c.ListLabels(slugs[0], slugs[1])
if err != nil {
// TODO: handle error
continue
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

// Config represents the YAML file described about labels and repos
type Config struct {
Labels Labels `yaml:"labels"`
Repos Repos `yaml:"repos"`
Labels []Label `yaml:"labels"`
Repos Repos `yaml:"repos"`
}

func loadConfig(path string) (Config, error) {
Expand Down
33 changes: 15 additions & 18 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ type Label struct {
PreviousName string `yaml:"previous_name,omitempty"`
}

// Labels represents a collection of Label
type Labels []Label

// Repo represents GitHub repository
type Repo struct {
Name string `yaml:"name"`
Expand All @@ -26,9 +23,9 @@ type Repo struct {
// Repos represents a collection of Repo
type Repos []Repo

func (a *App) GetLabel(owner, repo string, label Label) (Label, error) {
func (c *CLI) GetLabel(owner, repo string, label Label) (Label, error) {
ctx := context.Background()
ghLabel, _, err := a.Labeler.GetLabel(ctx, owner, repo, label.Name)
ghLabel, _, err := c.Labeler.GetLabel(ctx, owner, repo, label.Name)
if err != nil {
return Label{}, err
}
Expand All @@ -39,41 +36,41 @@ func (a *App) GetLabel(owner, repo string, label Label) (Label, error) {
}, nil
}

func (a *App) CreateLabel(owner, repo string, label Label) error {
func (c *CLI) 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 {
a.logger.Printf("rename %q in %s/%s to %q", label.PreviousName, owner, repo, label.Name)
_, _, err := a.Labeler.EditLabel(ctx, owner, repo, label.PreviousName, ghLabel)
c.Logger.Printf("rename %q in %s/%s to %q", label.PreviousName, owner, repo, label.Name)
_, _, err := c.Labeler.EditLabel(ctx, owner, repo, label.PreviousName, ghLabel)
return err
}
a.logger.Printf("create %q in %s/%s", label.Name, owner, repo)
_, _, err := a.Labeler.CreateLabel(ctx, owner, repo, ghLabel)
c.Logger.Printf("create %q in %s/%s", label.Name, owner, repo)
_, _, err := c.Labeler.CreateLabel(ctx, owner, repo, ghLabel)
return err
}

func (a *App) EditLabel(owner, repo string, label Label) error {
func (c *CLI) 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),
}
a.logger.Printf("edit %q in %s/%s", label.Name, owner, repo)
_, _, err := a.Labeler.EditLabel(ctx, owner, repo, label.Name, ghLabel)
c.Logger.Printf("edit %q in %s/%s", label.Name, owner, repo)
_, _, err := c.Labeler.EditLabel(ctx, owner, repo, label.Name, ghLabel)
return err
}

func (a *App) ListLabels(owner, repo string) ([]Label, error) {
func (c *CLI) ListLabels(owner, repo string) ([]Label, error) {
ctx := context.Background()
opt := &github.ListOptions{PerPage: 10}
var labels []Label
for {
ghLabels, resp, err := a.Labeler.ListLabels(ctx, owner, repo, opt)
ghLabels, resp, err := c.Labeler.ListLabels(ctx, owner, repo, opt)
if err != nil {
return labels, err
}
Expand All @@ -92,9 +89,9 @@ func (a *App) ListLabels(owner, repo string) ([]Label, error) {
return labels, nil
}

func (a *App) DeleteLabel(owner, repo string, label Label) error {
func (c *CLI) DeleteLabel(owner, repo string, label Label) error {
ctx := context.Background()
a.logger.Printf("delete %q in %s/%s", label.Name, owner, repo)
_, err := a.Labeler.DeleteLabel(ctx, owner, repo, label.Name)
c.Logger.Printf("delete %q in %s/%s", label.Name, owner, repo)
_, err := c.Labeler.DeleteLabel(ctx, owner, repo, label.Name)
return err
}

0 comments on commit d2f27d7

Please sign in to comment.