Skip to content

Commit

Permalink
move internal package to types (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas authored Sep 23, 2017
1 parent b2f15f6 commit b467657
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 33 deletions.
17 changes: 17 additions & 0 deletions engine/actions/trigger/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func TestTemplate(t *testing.T) {
}`)
assert.Equal(t, expected, buf.String(), "json")
}

func TestTemplateWithOrigin(t *testing.T) {
temp, e := template.New("test").Parse(`{{.Origin.Ref}}`)
assert.NoError(t, e, "should not error")
msg := message{
Time: time.Now(),
Number: 1,
Title: "title1",
State: "open",
Owner: "my",
Repo: "repo",
Origin: types.Origin{User: "self", Ref: "test"}}
var buf bytes.Buffer
temp.Execute(&buf, msg)
expected := "test"
assert.Equal(t, expected, buf.String(), "json")
}
16 changes: 10 additions & 6 deletions runner/hook_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"net/http"

"github.com/bivas/rivi/config/client"
"github.com/bivas/rivi/runner/internal"
"github.com/bivas/rivi/runner/types"
"github.com/bivas/rivi/types/builder"
"github.com/bivas/rivi/util/log"
"github.com/spf13/viper"
)

type hookListener struct {
conf client.ClientConfig
queue internal.HookListenerQueue
queue types.HookListenerQueue

logger log.Logger
}
Expand All @@ -22,21 +22,25 @@ func (h *hookListener) HandleEvent(r *http.Request) *HandledEventResult {
if !ok {
return &HandledEventResult{Message: "Skipping hook processing"}
}
h.queue.Enqueue(internal.NewMessage(h.conf, data))
h.queue.Enqueue(types.NewMessage(h.conf, data))
return &HandledEventResult{Message: "Processing " + data.GetShortName()}
}

func NewHookListener(clientConfiguration string) (Runner, error) {
logger := runnerLog.Get("hook.listener")
var conf client.ClientConfig
if clientConfiguration == "" {
conf = client.NewClientConfig(viper.New())
} else {
conf = client.NewClientConfigFromFile(clientConfiguration)
}
return NewHookListenerWithClientConfig(conf), nil
}

func NewHookListenerWithClientConfig(config client.ClientConfig) Runner {
logger := runnerLog.Get("hook.listener")
return &hookListener{
conf: conf,
conf: config,
queue: CreateHookListenerQueue(),
logger: logger,
}, nil
}
}
8 changes: 4 additions & 4 deletions runner/hook_listener_queue.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package runner

import (
"github.com/bivas/rivi/runner/internal"
"github.com/bivas/rivi/runner/local"
"github.com/bivas/rivi/runner/types"
)

var defaultHookListenerQueueProvider internal.HookListenerQueueProvider = local.CreateHookListenerQueue
var defaultHookListenerQueueProvider types.HookListenerQueueProvider = local.CreateHookListenerQueue

func SetHookListenerQueueProvider(fn internal.HookListenerQueueProvider) {
func SetHookListenerQueueProvider(fn types.HookListenerQueueProvider) {
defaultHookListenerQueueProvider = fn
}

func CreateHookListenerQueue() internal.HookListenerQueue {
func CreateHookListenerQueue() types.HookListenerQueue {
return defaultHookListenerQueueProvider()
}
30 changes: 30 additions & 0 deletions runner/hook_listener_queue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package runner

import (
"testing"

"github.com/bivas/rivi/runner/types"
"github.com/stretchr/testify/assert"
)

type testHookListenerQueue struct {
Created bool
}

func (*testHookListenerQueue) Enqueue(*types.Message) {
panic("implement me")
}

func TestSetHookListenerQueueProvider(t *testing.T) {
queue := &testHookListenerQueue{false}
SetHookListenerQueueProvider(func() types.HookListenerQueue {
queue.Created = true
return queue
})
assert.False(t, queue.Created, "created should be false")
result := CreateHookListenerQueue()
assert.NotNil(t, result, "test result")
_, ok := result.(*testHookListenerQueue)
assert.True(t, ok, "same type")
assert.True(t, queue.Created, "created")
}
15 changes: 15 additions & 0 deletions runner/hook_listener_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package runner

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewHookListener(t *testing.T) {
result, err := NewHookListener("")
assert.NoError(t, err, "new failed")
assert.NotNil(t, result, "nil runner")
_, ok := result.(*hookListener)
assert.True(t, ok, "must be of type hook lisetner")
}
10 changes: 5 additions & 5 deletions runner/local/hook_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package local
import (
"time"

"github.com/bivas/rivi/runner/internal"
"github.com/bivas/rivi/runner/types"
"github.com/bivas/rivi/util/log"

"github.com/patrickmn/go-cache"
)

type channelHookHandler struct {
incoming <-chan *internal.Message
incoming <-chan *types.Message
logger log.Logger

processingCache *cache.Cache
Expand Down Expand Up @@ -38,14 +38,14 @@ func (h *channelHookHandler) Run() {
log.MetaFields{
log.F("issue", key),
}, "Sending data to job handler")
c.(internal.JobHandler).Send(msg)
c.(types.JobHandler).Send(msg)
}
}

func NewChannelHookHandler(incoming <-chan *internal.Message) internal.HookHandler {
func NewChannelHookHandler(incoming <-chan *types.Message) types.HookHandler {
processingCache := cache.New(time.Minute, 2*time.Minute)
processingCache.OnEvicted(func(key string, value interface{}) {
value.(internal.JobHandler).Send(nil)
value.(types.JobHandler).Send(nil)
})
return &channelHookHandler{
incoming: incoming,
Expand Down
12 changes: 6 additions & 6 deletions runner/local/hook_listener_queue.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package local

import (
"github.com/bivas/rivi/runner/internal"
"github.com/bivas/rivi/runner/types"
"github.com/bivas/rivi/util/log"
)

type channelHookListenerQueue struct {
incoming chan *internal.Message
incoming chan *types.Message
}

func (c *channelHookListenerQueue) Enqueue(data *internal.Message) {
func (c *channelHookListenerQueue) Enqueue(data *types.Message) {
if data == nil {
close(c.incoming)
return
}
c.incoming <- data
}

func channelHookListenerQueueProvider() internal.HookListenerQueue {
func channelHookListenerQueueProvider() types.HookListenerQueue {
log.Get("hook.listener.queue").DebugWith(
log.MetaFields{
log.F("type", "channel")}, "Creating hook listener queue provider")
incomingHooks := make(chan *internal.Message)
incomingHooks := make(chan *types.Message)
handler := NewChannelHookHandler(incomingHooks)
go handler.Run()
return &channelHookListenerQueue{incomingHooks}
}

func CreateHookListenerQueue() internal.HookListenerQueue {
func CreateHookListenerQueue() types.HookListenerQueue {
return channelHookListenerQueueProvider()
}
10 changes: 5 additions & 5 deletions runner/local/job_handler.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package local

import (
"github.com/bivas/rivi/runner/internal"
"github.com/bivas/rivi/runner/types"
"github.com/bivas/rivi/util/log"
)

type jobHandler struct {
channel chan *internal.Message
channel chan *types.Message
work *workUnit

logger log.Logger
}

func (h *jobHandler) Send(data *internal.Message) {
func (h *jobHandler) Send(data *types.Message) {
if data == nil {
h.logger.Debug("Closing channel")
close(h.channel)
Expand All @@ -25,8 +25,8 @@ func (h *jobHandler) Start() {
go h.work.Handle()
}

func NewJobHandler() internal.JobHandler {
c := make(chan *internal.Message)
func NewJobHandler() types.JobHandler {
c := make(chan *types.Message)
h := &jobHandler{
channel: c,
work: &workUnit{
Expand Down
6 changes: 3 additions & 3 deletions runner/local/work_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import (
"github.com/bivas/rivi/config"
"github.com/bivas/rivi/engine"
"github.com/bivas/rivi/runner/env"
"github.com/bivas/rivi/runner/internal"
"github.com/bivas/rivi/runner/types"
"github.com/bivas/rivi/types/builder"
"github.com/bivas/rivi/util/log"
"github.com/bivas/rivi/util/state"
)

type workUnit struct {
incoming <-chan *internal.Message
incoming <-chan *types.Message
logger log.Logger
}

func (w *workUnit) internalHandle(msg *internal.Message) error {
func (w *workUnit) internalHandle(msg *types.Message) error {
environment, err := env.GetEnvironment()
if err != nil {
w.logger.ErrorWith(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package types

type HookHandler interface {
Run()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package types

type HookListenerQueue interface {
Enqueue(*Message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package types

type JobHandler interface {
Send(*Message)
Expand Down
2 changes: 1 addition & 1 deletion runner/internal/message.go → runner/types/message.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package types

import (
"github.com/bivas/rivi/config/client"
Expand Down

0 comments on commit b467657

Please sign in to comment.