Skip to content

Commit

Permalink
Manifest -> Config
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 7, 2020
1 parent 3ac244f commit 90bc98b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: 'github-labeler'
description: 'Declarative way to configure GitHub labels'
inputs:
manifest:
config:
description: 'Path to YAML file which defines GitHub labels'
required: false
default: '.github/labels.yml'
Expand Down
28 changes: 14 additions & 14 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ type CLI struct {
Stderr io.Writer
Option Option

GitHub *app
Config Manifest
GitHub *App
Config Config
}

type Option struct {
DryRun bool `long:"dry-run" description:"Just dry run"`
Config string `short:"c" long:"config" description:"Path to YAML file that labels are defined" default:"labels.yaml"`
Config string `long:"config" description:"Path to YAML file that labels are defined" default:"labels.yaml"`
Import bool `long:"import" description:"Import existing labels if enabled"`
Version bool `long:"version" description:"Show version"`
}

type app struct {
type App struct {
Labeler Labeler
logger *log.Logger
}
Expand All @@ -48,12 +48,12 @@ func (c *CLI) Run(args []string) error {
tc := oauth2.NewClient(oauth2.NoContext, ts)
client := github.NewClient(tc)

m, err := loadManifest(c.Option.Config)
cfg, err := loadConfig(c.Option.Config)
if err != nil {
return err
}

gc := &app{
gc := &App{
Labeler: githubClientImpl{client},
logger: log.New(os.Stdout, "labeler: ", log.Ldate|log.Ltime),
}
Expand All @@ -64,20 +64,20 @@ func (c *CLI) Run(args []string) error {
}

c.GitHub = gc
c.Config = m
c.Config = cfg

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

if c.Option.Import {
m := c.CurrentLabels()
cfg := c.CurrentLabels()
f, err := os.Create(c.Option.Config)
if err != nil {
return err
}
defer f.Close()
return yaml.NewEncoder(f).Encode(&m)
return yaml.NewEncoder(f).Encode(&cfg)
}

if cmp.Equal(c.CurrentLabels(), c.Config) {
Expand Down Expand Up @@ -150,8 +150,8 @@ func (c *CLI) Sync(repo Repo) error {
return c.deleteLabels(slugs[0], slugs[1])
}

func (c *CLI) CurrentLabels() Manifest {
var m Manifest
func (c *CLI) CurrentLabels() Config {
var cfg Config
for _, repo := range c.Config.Repos {
slugs := strings.Split(repo.Name, "/")
if len(slugs) != 2 {
Expand All @@ -168,8 +168,8 @@ func (c *CLI) CurrentLabels() Manifest {
ls = append(ls, label.Name)
}
repo.Labels = ls
m.Repos = append(m.Repos, repo)
m.Labels = append(m.Labels, labels...)
cfg.Repos = append(cfg.Repos, repo)
cfg.Labels = append(cfg.Labels, labels...)
}
return m
return cfg
}
24 changes: 12 additions & 12 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ import (
yaml "gopkg.in/yaml.v2"
)

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

func loadManifest(path string) (Manifest, error) {
var m Manifest
func loadConfig(path string) (Config, error) {
var cfg Config
buf, err := ioutil.ReadFile(path)
if err != nil {
return m, err
return cfg, err
}
err = yaml.Unmarshal(buf, &m)
return m, err
err = yaml.Unmarshal(buf, &cfg)
return cfg, err
}

func (m Manifest) getDefinedLabel(name string) (Label, error) {
for _, label := range m.Labels {
func (cfg Config) getDefinedLabel(name string) (Label, error) {
for _, label := range cfg.Labels {
if label.Name == name {
return label, nil
}
}
return Label{}, fmt.Errorf("%s: no such defined label in manifest YAML", name)
return Label{}, fmt.Errorf("%s: no such defined label in config YAML", name)
}

func (m Manifest) checkIfRepoHasLabel(repoName, labelName string) bool {
func (cfg Config) checkIfRepoHasLabel(repoName, labelName string) bool {
var labels []string
for _, repo := range m.Repos {
for _, repo := range cfg.Repos {
if repo.Name == repoName {
labels = repo.Labels
break
Expand Down
6 changes: 3 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash

manifest=${INPUT_MANIFEST}
config=${INPUT_CONFIG}

import=${INPUT_IMPORT:-false}
if ${import}; then
github-labeler --import --config=${manifest}
github-labeler --import --config=${config}
exit ${?}
fi

github-labeler --config=${manifest}
github-labeler --config=${config}
10 changes: 5 additions & 5 deletions label.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Repo struct {
// Repos represents a collection of Repo
type Repos []Repo

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

func (a *app) CreateLabel(owner, repo string, label Label) error {
func (a *App) CreateLabel(owner, repo string, label Label) error {
ctx := context.Background()
ghLabel := &github.Label{
Name: github.String(label.Name),
Expand All @@ -56,7 +56,7 @@ func (a *app) CreateLabel(owner, repo string, label Label) error {
return err
}

func (a *app) EditLabel(owner, repo string, label Label) error {
func (a *App) EditLabel(owner, repo string, label Label) error {
ctx := context.Background()
ghLabel := &github.Label{
Name: github.String(label.Name),
Expand All @@ -68,7 +68,7 @@ func (a *app) EditLabel(owner, repo string, label Label) error {
return err
}

func (a *app) ListLabels(owner, repo string) ([]Label, error) {
func (a *App) ListLabels(owner, repo string) ([]Label, error) {
ctx := context.Background()
opt := &github.ListOptions{PerPage: 10}
var labels []Label
Expand All @@ -92,7 +92,7 @@ func (a *app) ListLabels(owner, repo string) ([]Label, error) {
return labels, nil
}

func (a *app) DeleteLabel(owner, repo string, label Label) error {
func (a *App) 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)
Expand Down

0 comments on commit 90bc98b

Please sign in to comment.