Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from sue445/feature/golint
Browse files Browse the repository at this point in the history
Apply golint
  • Loading branch information
sue445 committed Oct 11, 2017
2 parents f36583a + 9173dfd commit fa969a1
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 56 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"io/ioutil"
)

// Config represents config file
type Config struct {
Pipelines []ConfigPipeline `yaml:"pipelines"`
}

// ConfigPipeline represents pipelines element of config file
type ConfigPipeline struct {
ApplicationPath string `yaml:"application_path"`
PipelineName string `yaml:"pipeline_name"`
Branch string `yaml:"branch"`
}

// LoadConfigFromData load config from yaml data
func LoadConfigFromData(yamlData string) (Config, error) {
c := Config{}

Expand All @@ -26,6 +29,7 @@ func LoadConfigFromData(yamlData string) (Config, error) {
return c, nil
}

// LoadConfigFromFile load config from yaml file
func LoadConfigFromFile(yamlFile string) (Config, error) {
buf, err := ioutil.ReadFile(yamlFile)

Expand Down
23 changes: 15 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import (
)

const (
DEFAULT_BRANCH = "master"
DEFAULT_PIPELINE_NAME = "build"
// DefaultBranch represents default branch name when branch is undefined in config file
DefaultBranch = "master"

// DefaultPipelineName represents default pipeline name when pipeline_name is undefined in config file
DefaultPipelineName = "build"
)

var (
Version string
Revision string
// Version represents app version (injected from ldflags)
Version string

// Revision represents app revision (injected from ldflags)
Revision string

configFile string
token string
isPrintVersion bool
Expand Down Expand Up @@ -49,7 +56,7 @@ func main() {
run, err := perform(wercker, &configPipeline)

if err == nil {
fmt.Printf("[application_path:%s][pipeline_name:%s][branch:%s] Triggered pipeline: %s\n", configPipeline.ApplicationPath, configPipeline.PipelineName, configPipeline.Branch, run.Url)
fmt.Printf("[application_path:%s][pipeline_name:%s][branch:%s] Triggered pipeline: %s\n", configPipeline.ApplicationPath, configPipeline.PipelineName, configPipeline.Branch, run.URL)
} else {
fmt.Printf("[application_path:%s][pipeline_name:%s] Error: %v\n", configPipeline.ApplicationPath, configPipeline.PipelineName, err)
}
Expand All @@ -62,10 +69,10 @@ func printVersion() {

func perform(wercker WerckerTrigger, configPipeline *ConfigPipeline) (run *WerckerRun, err error) {
if len(configPipeline.Branch) == 0 {
configPipeline.Branch = DEFAULT_BRANCH
configPipeline.Branch = DefaultBranch
}
if len(configPipeline.PipelineName) == 0 {
configPipeline.PipelineName = DEFAULT_PIPELINE_NAME
configPipeline.PipelineName = DefaultPipelineName
}

pipeline, err := wercker.FindPipeline(configPipeline.ApplicationPath, configPipeline.PipelineName)
Expand All @@ -74,7 +81,7 @@ func perform(wercker WerckerTrigger, configPipeline *ConfigPipeline) (run *Werck
return nil, err
}

ret, err := wercker.TriggerNewRun(pipeline.Id, configPipeline.Branch)
ret, err := wercker.TriggerNewRun(pipeline.ID, configPipeline.Branch)

if err != nil {
return nil, err
Expand Down
34 changes: 17 additions & 17 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func (w *MockWercker) FindPipeline(appPath string, pipelineName string) (pipelin
return w.findPipeline(appPath, pipelineName)
}

func (w *MockWercker) TriggerNewRun(pipelineId string, branch string) (run *WerckerRun, err error) {
return w.triggerNewRun(pipelineId, branch)
func (w *MockWercker) TriggerNewRun(pipelineID string, branch string) (run *WerckerRun, err error) {
return w.triggerNewRun(pipelineID, branch)
}

func TestPerform_Success_MaxiumKeys(t *testing.T) {
appPath := "wercker/docs"
pipelineName := "build-wip"
branch := "develop"
url := "https://app.wercker.com/api/v3/runs/000000000000000000"
pipelineId := "xxxxxxxxxxxxxxxxxxxxxx"
pipelineID := "xxxxxxxxxxxxxxxxxxxxxx"

configPipeline := ConfigPipeline{
ApplicationPath: appPath,
Expand All @@ -42,29 +42,29 @@ func TestPerform_Success_MaxiumKeys(t *testing.T) {
assert.Equal(t, pipelineName, _pipelineName)

pipeline = new(WerckerPipeline)
pipeline.Id = pipelineId
pipeline.ID = pipelineID
pipeline.Name = pipelineName
return pipeline, nil
}
wercker.triggerNewRun = func(_pipelineId string, _branch string) (run *WerckerRun, err error) {
assert.Equal(t, pipelineId, _pipelineId)
assert.Equal(t, pipelineID, _pipelineId)
assert.Equal(t, branch, _branch)

run = new(WerckerRun)
run.Url = url
run.URL = url
return run, nil
}

run, err := perform(wercker, &configPipeline)

assert.NoError(t, err)
assert.Equal(t, url, run.Url)
assert.Equal(t, url, run.URL)
}

func TestPerform_Success_MinimumKeys(t *testing.T) {
appPath := "wercker/docs"
url := "https://app.wercker.com/api/v3/runs/000000000000000000"
pipelineId := "xxxxxxxxxxxxxxxxxxxxxx"
pipelineID := "xxxxxxxxxxxxxxxxxxxxxx"

configPipeline := ConfigPipeline{
ApplicationPath: appPath,
Expand All @@ -73,29 +73,29 @@ func TestPerform_Success_MinimumKeys(t *testing.T) {
wercker := NewStubWercker()
wercker.findPipeline = func(_appPath string, _pipelineName string) (pipeline *WerckerPipeline, err error) {
assert.Equal(t, appPath, _appPath)
assert.Equal(t, DEFAULT_PIPELINE_NAME, _pipelineName)
assert.Equal(t, DefaultPipelineName, _pipelineName)

pipeline = new(WerckerPipeline)
pipeline.Id = pipelineId
pipeline.Name = DEFAULT_PIPELINE_NAME
pipeline.ID = pipelineID
pipeline.Name = DefaultPipelineName
return pipeline, nil
}
wercker.triggerNewRun = func(_pipelineId string, _branch string) (run *WerckerRun, err error) {
assert.Equal(t, pipelineId, _pipelineId)
assert.Equal(t, DEFAULT_BRANCH, _branch)
assert.Equal(t, pipelineID, _pipelineId)
assert.Equal(t, DefaultBranch, _branch)

run = new(WerckerRun)
run.Url = url
run.URL = url
return run, nil
}

run, err := perform(wercker, &configPipeline)

assert.NoError(t, err)
assert.Equal(t, url, run.Url)
assert.Equal(t, url, run.URL)

assert.Equal(t, DEFAULT_BRANCH, configPipeline.Branch)
assert.Equal(t, DEFAULT_PIPELINE_NAME, configPipeline.PipelineName)
assert.Equal(t, DefaultBranch, configPipeline.Branch)
assert.Equal(t, DefaultPipelineName, configPipeline.PipelineName)
}

func TestPerform_Error(t *testing.T) {
Expand Down
43 changes: 28 additions & 15 deletions wercker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ import (
)

const (
MAX_LIMIT = 20
// MaxLimit represents paging per 1 page
MaxLimit = 20
)

// Wercker API client
type Wercker struct {
token string
}

// WerckerApplication represents a `application` resource
type WerckerApplication struct {
Id string `json:"id"`
ID string `json:"id"`
}

// WerckerRun represents a `run` resource
type WerckerRun struct {
Id string `json:"id"`
Url string `json:"url"`
ID string `json:"id"`
URL string `json:"url"`
CreatedAt string `json:"createdAt"`
CommitHash string `json:"commitHash"`
Message string `json:"message"`
Expand All @@ -34,34 +38,40 @@ type WerckerRun struct {
Pipeline WerckerPipeline `json:"pipeline"`
}

// WerckerPipeline represents a `pipeline` resource
type WerckerPipeline struct {
Id string `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
}

// WerckerTriggerNewRunParam represents request parameter of TriggerNewRun API
type WerckerTriggerNewRunParam struct {
PipelineId string `json:"pipelineId"`
PipelineID string `json:"pipelineId"`
Branch string `json:"branch"`
Message string `json:"message"`
}

// WerckerError represents error response body
type WerckerError struct {
StatusCode int `json:"statusCode"`
Error string `json:"error"`
Message string `json:"message"`
}

// WerckerTrigger represents interface of API client (for stubbing from test)
type WerckerTrigger interface {
FindPipeline(appPath string, pipelineName string) (pipeline *WerckerPipeline, err error)
TriggerNewRun(pipelineId string, branch string) (run *WerckerRun, err error)
TriggerNewRun(pipelineID string, branch string) (run *WerckerRun, err error)
}

// NewWercker returns new Wercker object
func NewWercker(token string) *Wercker {
w := new(Wercker)
w.token = token
return w
}

// GetApplication gets `application` with specified application path
func (w *Wercker) GetApplication(applicationPath string) (app *WerckerApplication, err error) {
req, err := http.NewRequest(
"GET",
Expand All @@ -82,7 +92,8 @@ func (w *Wercker) GetApplication(applicationPath string) (app *WerckerApplicatio
return app, err
}

func (w *Wercker) GetRuns(applicationId string, skip int) (runs []WerckerRun, err error) {
// GetRuns gets `runs` with specified application
func (w *Wercker) GetRuns(applicationID string, skip int) (runs []WerckerRun, err error) {
req, err := http.NewRequest(
"GET",
"https://app.wercker.com/api/v3/runs",
Expand All @@ -93,9 +104,9 @@ func (w *Wercker) GetRuns(applicationId string, skip int) (runs []WerckerRun, er
}

values := url.Values{}
values.Add("applicationId", applicationId)
values.Add("applicationId", applicationID)
values.Add("skip", strconv.Itoa(skip))
values.Add("limit", strconv.Itoa(MAX_LIMIT))
values.Add("limit", strconv.Itoa(MaxLimit))
req.URL.RawQuery = values.Encode()

body, err := w.execute(req)
Expand All @@ -108,6 +119,7 @@ func (w *Wercker) GetRuns(applicationId string, skip int) (runs []WerckerRun, er
return runs, err
}

// FindPipeline find `pipeline` with specified application path and pipeline name
func (w *Wercker) FindPipeline(applicationPath string, pipelineName string) (pipeline *WerckerPipeline, err error) {
application, err := w.GetApplication(applicationPath)
if err != nil {
Expand All @@ -117,7 +129,7 @@ func (w *Wercker) FindPipeline(applicationPath string, pipelineName string) (pip
skip := 0

for {
runs, err := w.GetRuns(application.Id, skip)
runs, err := w.GetRuns(application.ID, skip)
if err != nil {
return nil, err
}
Expand All @@ -128,18 +140,19 @@ func (w *Wercker) FindPipeline(applicationPath string, pipelineName string) (pip
}
}

if len(runs) < MAX_LIMIT {
if len(runs) < MaxLimit {
return nil, fmt.Errorf("Not Found pipeline: %s", pipelineName)
}

skip += MAX_LIMIT
skip += MaxLimit
}
}

func (w *Wercker) TriggerNewRun(pipelineId string, branch string) (run *WerckerRun, err error) {
// TriggerNewRun trigger new run with specified pipeline id and branch
func (w *Wercker) TriggerNewRun(pipelineID string, branch string) (run *WerckerRun, err error) {
currentTime := time.Now().Format("2006-01-02 15:04:05")
params := WerckerTriggerNewRunParam{
PipelineId: pipelineId,
PipelineID: pipelineID,
Branch: branch,
Message: "wercker_build_trigger: " + currentTime,
}
Expand Down
6 changes: 6 additions & 0 deletions wercker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ build:
code: |
go vet
- script:
name: golint
code: |
go get -u github.com/golang/lint/golint
golint -set_exit_status
after-steps:
# https://github.com/wercker/step-slack
- slack-notifier:
Expand Down
Loading

0 comments on commit fa969a1

Please sign in to comment.