Browse Source

Add Register() function to Services

Fix plugin_tests and make them a bit more intelligible.
kegan/github-service
Kegan Dougal 9 years ago
parent
commit
55f857db96
  1. 5
      src/github.com/matrix-org/go-neb/api.go
  2. 4
      src/github.com/matrix-org/go-neb/plugin/plugin.go
  3. 28
      src/github.com/matrix-org/go-neb/plugin/plugin_test.go
  4. 1
      src/github.com/matrix-org/go-neb/services/echo/echo.go
  5. 45
      src/github.com/matrix-org/go-neb/services/github/github.go
  6. 1
      src/github.com/matrix-org/go-neb/types/types.go

5
src/github.com/matrix-org/go-neb/api.go

@ -198,6 +198,11 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac
return nil, &errors.HTTPError{err, "Error parsing config JSON", 400} return nil, &errors.HTTPError{err, "Error parsing config JSON", 400}
} }
err := service.Register()
if err != nil {
return nil, &errors.HTTPError{err, "Failed to register service", 500}
}
client, err := s.clients.Client(service.ServiceUserID()) client, err := s.clients.Client(service.ServiceUserID())
if err != nil { if err != nil {
return nil, &errors.HTTPError{err, "Unknown matrix client", 400} return nil, &errors.HTTPError{err, "Unknown matrix client", 400}

4
src/github.com/matrix-org/go-neb/plugin/plugin.go

@ -31,7 +31,7 @@ type Command struct {
// the appropriate RFC. // the appropriate RFC.
type Expansion struct { type Expansion struct {
Regexp *regexp.Regexp Regexp *regexp.Regexp
Expand func(roomID, matchingText string) interface{}
Expand func(roomID, userID, matchingText string) interface{}
} }
// matches if the arguments start with the path of the command. // matches if the arguments start with the path of the command.
@ -95,7 +95,7 @@ func runExpansionsForPlugin(plugin Plugin, event *matrix.Event, body string) []i
continue continue
} }
matches[matchingText] = true matches[matchingText] = true
if response := expansion.Expand(event.RoomID, matchingText); response != nil {
if response := expansion.Expand(event.RoomID, event.Sender, matchingText); response != nil {
responses = append(responses, response) responses = append(responses, response)
} }
} }

28
src/github.com/matrix-org/go-neb/plugin/plugin_test.go

@ -26,21 +26,21 @@ func makeTestEvent(msgtype, body string) *matrix.Event {
type testResponse struct { type testResponse struct {
RoomID string RoomID string
Sender string
Arguments []string Arguments []string
} }
func makeTestResponse(roomID, sender string, arguments []string) interface{} { func makeTestResponse(roomID, sender string, arguments []string) interface{} {
return testResponse{roomID, sender, arguments}
return testResponse{roomID, arguments}
} }
type testExpansion struct { type testExpansion struct {
RoomID string RoomID string
UserID string
MatchingText string MatchingText string
} }
func makeTestExpansion(roomID, matchingText string) interface{} {
return testExpansion{roomID, matchingText}
func makeTestExpansion(roomID, userID, matchingText string) interface{} {
return testExpansion{roomID, userID, matchingText}
} }
func makeTestPlugin(paths [][]string, regexps []*regexp.Regexp) Plugin { func makeTestPlugin(paths [][]string, regexps []*regexp.Regexp) Plugin {
@ -74,7 +74,7 @@ func TestRunCommands(t *testing.T) {
"arg1", "arg 2", "arg 3", "arg1", "arg 2", "arg 3",
})} })}
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("runCommands(%q, %q) == %q, want %q", plugins, event, got, want)
t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want)
} }
} }
@ -89,7 +89,7 @@ func TestRunCommandsBestMatch(t *testing.T) {
"arg1", "arg1",
})} })}
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("runCommands(%q, %q) == %q, want %q", plugins, event, got, want)
t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want)
} }
} }
@ -105,7 +105,7 @@ func TestRunCommandsMultiplePlugins(t *testing.T) {
makeTestResponse(myRoomID, mySender, []string{"first", "arg1"}), makeTestResponse(myRoomID, mySender, []string{"first", "arg1"}),
} }
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("runCommands(%q, %q) == %q, want %q", plugins, event, got, want)
t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want)
} }
} }
@ -119,7 +119,7 @@ func TestRunCommandsInvalidShell(t *testing.T) {
makeTestResponse(myRoomID, mySender, []string{"'mismatched", `quotes"`}), makeTestResponse(myRoomID, mySender, []string{"'mismatched", `quotes"`}),
} }
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("runCommands(%q, %q) == %q, want %q", plugins, event, got, want)
t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want)
} }
} }
@ -133,12 +133,12 @@ func TestExpansion(t *testing.T) {
event := makeTestEvent("m.text", "test banana for scale") event := makeTestEvent("m.text", "test banana for scale")
got := runCommands(plugins, event) got := runCommands(plugins, event)
want := []interface{}{ want := []interface{}{
makeTestExpansion(myRoomID, "anana"),
makeTestExpansion(myRoomID, "ale"),
makeTestExpansion(myRoomID, "ban"),
makeTestExpansion(myRoomID, mySender, "anana"),
makeTestExpansion(myRoomID, mySender, "ale"),
makeTestExpansion(myRoomID, mySender, "ban"),
} }
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("runCommands(%q, %q) == %q, want %q", plugins, event, got, want)
t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want)
} }
} }
@ -151,9 +151,9 @@ func TestExpansionDuplicateMatches(t *testing.T) {
event := makeTestEvent("m.text", "badger badger badger") event := makeTestEvent("m.text", "badger badger badger")
got := runCommands(plugins, event) got := runCommands(plugins, event)
want := []interface{}{ want := []interface{}{
makeTestExpansion(myRoomID, "badger"),
makeTestExpansion(myRoomID, mySender, "badger"),
} }
if !reflect.DeepEqual(got, want) { if !reflect.DeepEqual(got, want) {
t.Errorf("runCommands(%q, %q) == %q, want %q", plugins, event, got, want)
t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want)
} }
} }

1
src/github.com/matrix-org/go-neb/services/echo/echo.go

@ -18,6 +18,7 @@ func (e *echoService) ServiceUserID() string { return e.UserID }
func (e *echoService) ServiceID() string { return e.id } func (e *echoService) ServiceID() string { return e.id }
func (e *echoService) ServiceType() string { return "echo" } func (e *echoService) ServiceType() string { return "echo" }
func (e *echoService) RoomIDs() []string { return e.Rooms } func (e *echoService) RoomIDs() []string { return e.Rooms }
func (e *echoService) Register() error { return nil }
func (e *echoService) Plugin(roomID string) plugin.Plugin { func (e *echoService) Plugin(roomID string) plugin.Plugin {
return plugin.Plugin{ return plugin.Plugin{
Commands: []plugin.Command{ Commands: []plugin.Command{

45
src/github.com/matrix-org/go-neb/services/github/github.go

@ -12,6 +12,7 @@ import (
"net/http" "net/http"
"regexp" "regexp"
"strconv" "strconv"
"strings"
) )
// Matches alphanumeric then a /, then more alphanumeric then a #, then a number. // Matches alphanumeric then a /, then more alphanumeric then a #, then a number.
@ -20,28 +21,42 @@ var ownerRepoIssueRegex = regexp.MustCompile("([A-z0-9-_]+)/([A-z0-9-_]+)#([0-9]
type githubService struct { type githubService struct {
id string id string
UserID string
Rooms map[string][]string // room_id => ["push","issue","pull_request"]
BotUserID string
GithubUserID string
WebhookRooms map[string][]string // room_id => ["push","issue","pull_request"]
} }
func (s *githubService) ServiceUserID() string { return s.UserID }
func (s *githubService) ServiceUserID() string { return s.BotUserID }
func (s *githubService) ServiceID() string { return s.id } func (s *githubService) ServiceID() string { return s.id }
func (s *githubService) ServiceType() string { return "github" } func (s *githubService) ServiceType() string { return "github" }
func (s *githubService) RoomIDs() []string { func (s *githubService) RoomIDs() []string {
var keys []string var keys []string
for k := range s.Rooms {
for k := range s.WebhookRooms {
keys = append(keys, k) keys = append(keys, k)
} }
return keys return keys
} }
func (s *githubService) Plugin(roomID string) plugin.Plugin { func (s *githubService) Plugin(roomID string) plugin.Plugin {
return plugin.Plugin{ return plugin.Plugin{
Commands: []plugin.Command{},
Commands: []plugin.Command{
plugin.Command{
Path: []string{"github", "create"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
cli := githubClientFor(userID, false)
if cli == nil {
// TODO: send starter link
return &matrix.TextMessage{"m.notice",
userID + " : You have not linked your Github account."}, nil
}
return &matrix.TextMessage{"m.notice", strings.Join(args, " ")}, nil
},
},
},
Expansions: []plugin.Expansion{ Expansions: []plugin.Expansion{
plugin.Expansion{ plugin.Expansion{
Regexp: ownerRepoIssueRegex, Regexp: ownerRepoIssueRegex,
Expand: func(roomID, matchingText string) interface{} {
cli := githubClient("")
Expand: func(roomID, userID, matchingText string) interface{} {
cli := githubClientFor(userID, true)
owner, repo, num, err := ownerRepoNumberFromText(matchingText) owner, repo, num, err := ownerRepoNumberFromText(matchingText)
if err != nil { if err != nil {
log.WithError(err).WithField("text", matchingText).Print( log.WithError(err).WithField("text", matchingText).Print(
@ -75,7 +90,7 @@ func (s *githubService) OnReceiveWebhook(w http.ResponseWriter, req *http.Reques
return return
} }
for roomID, notif := range s.Rooms {
for roomID, notif := range s.WebhookRooms {
notifyRoom := false notifyRoom := false
for _, notifyType := range notif { for _, notifyType := range notif {
if evType == notifyType { if evType == notifyType {
@ -99,6 +114,20 @@ func (s *githubService) OnReceiveWebhook(w http.ResponseWriter, req *http.Reques
} }
w.WriteHeader(200) w.WriteHeader(200)
} }
func (s *githubService) Register() error {
return nil
}
func githubClientFor(userID string, allowUnauth bool) *github.Client {
var cli *github.Client
// TODO get the token for this user
if cli == nil && allowUnauth {
return githubClient("")
}
return cli
}
// githubClient returns a github Client which can perform Github API operations. // githubClient returns a github Client which can perform Github API operations.
// If `token` is empty, a non-authenticated client will be created. This should be // If `token` is empty, a non-authenticated client will be created. This should be

1
src/github.com/matrix-org/go-neb/types/types.go

@ -35,6 +35,7 @@ type Service interface {
RoomIDs() []string RoomIDs() []string
Plugin(roomID string) plugin.Plugin Plugin(roomID string) plugin.Plugin
OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client)
Register() error
} }
var servicesByType = map[string]func(string) Service{} var servicesByType = map[string]func(string) Service{}

Loading…
Cancel
Save