Skip to content

Commit

Permalink
feat(core): Only enable extension functionality if the module is loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
etcinit committed Feb 20, 2017
1 parent 51ab22e commit 971c693
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app/connectors/utilities/has_module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utilities

import "github.com/etcinit/phabulous/app/interfaces"

// HasModule returns whether or not a bot connector has a specific module
// loaded.
func HasModule(bot interfaces.Bot, name string) bool {
for _, module := range bot.GetModules() {
if module.GetName() == name {
return true
}
}

return false
}
30 changes: 30 additions & 0 deletions app/connectors/utilities/has_module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package utilities

import (
"testing"

"github.com/etcinit/phabulous/app/interfaces"
"github.com/etcinit/phabulous/app/testing/mocks"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

func Test_HasModule(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockBot := mocks.NewMockBot(mockCtrl)
moduleA := mocks.NewMockModule(mockCtrl)
moduleB := mocks.NewMockModule(mockCtrl)

mockBot.EXPECT().GetModules().Times(2).Return([]interfaces.Module{
moduleA,
moduleB,
})

moduleA.EXPECT().GetName().Times(2).Return("module-a")
moduleB.EXPECT().GetName().Times(1).Return("module-b")

assert.True(t, HasModule(mockBot, "module-a"))
assert.False(t, HasModule(mockBot, "module-c"))
}
7 changes: 7 additions & 0 deletions app/modules/core/command_summon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/etcinit/gonduit"
"github.com/etcinit/gonduit/entities"
"github.com/etcinit/gonduit/requests"
"github.com/etcinit/phabulous/app/connectors/utilities"
"github.com/etcinit/phabulous/app/gonduit/extensions"
phabulousRequests "github.com/etcinit/phabulous/app/gonduit/extensions/requests"
"github.com/etcinit/phabulous/app/gonduit/extensions/responses"
Expand Down Expand Up @@ -282,6 +283,12 @@ func (c *SummonCommand) lookupSlackMap(
conn *gonduit.Conn,
revision *entities.DifferentialRevision,
) (*responses.PhabulousToSlackResponse, []slack.User, error) {
// If the extension module is not loaded, we don't attempt to use this
// functionality.
if !utilities.HasModule(bot, "extension") {
return &responses.PhabulousToSlackResponse{}, []slack.User{}, nil
}

var slackMap *responses.PhabulousToSlackResponse
var slackUsers []slack.User
var err error
Expand Down
43 changes: 42 additions & 1 deletion app/testing/mocks/generated.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Automatically generated by MockGen. DO NOT EDIT!
// Source: github.com/etcinit/phabulous/app/interfaces (interfaces: Bot,Message,HandlerTuple)
// Source: github.com/etcinit/phabulous/app/interfaces (interfaces: Bot,Message,HandlerTuple,Module)

package mocks

Expand Down Expand Up @@ -268,3 +268,44 @@ func (_m *MockHandlerTuple) GetPattern() *regexp.Regexp {
func (_mr *_MockHandlerTupleRecorder) GetPattern() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetPattern")
}

// Mock of Module interface
type MockModule struct {
ctrl *gomock.Controller
recorder *_MockModuleRecorder
}

// Recorder for MockModule (not exported)
type _MockModuleRecorder struct {
mock *MockModule
}

func NewMockModule(ctrl *gomock.Controller) *MockModule {
mock := &MockModule{ctrl: ctrl}
mock.recorder = &_MockModuleRecorder{mock}
return mock
}

func (_m *MockModule) EXPECT() *_MockModuleRecorder {
return _m.recorder
}

func (_m *MockModule) GetCommands() []interfaces.Command {
ret := _m.ctrl.Call(_m, "GetCommands")
ret0, _ := ret[0].([]interfaces.Command)
return ret0
}

func (_mr *_MockModuleRecorder) GetCommands() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetCommands")
}

func (_m *MockModule) GetName() string {
ret := _m.ctrl.Call(_m, "GetName")
ret0, _ := ret[0].(string)
return ret0
}

func (_mr *_MockModuleRecorder) GetName() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "GetName")
}
2 changes: 1 addition & 1 deletion app/testing/mocks/stub.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package mocks

//go:generate mockgen -destination=generated.go -package=mocks github.com/etcinit/phabulous/app/interfaces Bot,Message,HandlerTuple
//go:generate mockgen -destination=generated.go -package=mocks github.com/etcinit/phabulous/app/interfaces Bot,Message,HandlerTuple,Module

0 comments on commit 971c693

Please sign in to comment.