From 928a243ad2768b17f586fa2ad9b39c95442a5810 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 16 Aug 2016 15:42:55 +0100 Subject: [PATCH] Change Expansion.Expand to include matching groups rather than just the match --- .../matrix-org/go-neb/plugin/plugin.go | 7 ++-- .../matrix-org/go-neb/plugin/plugin_test.go | 18 +++++----- .../go-neb/services/github/github.go | 36 ++++++++----------- .../matrix-org/go-neb/services/jira/jira.go | 20 +++++------ 4 files changed, 36 insertions(+), 45 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/plugin/plugin.go b/src/github.com/matrix-org/go-neb/plugin/plugin.go index 33123aa..05ee1c1 100644 --- a/src/github.com/matrix-org/go-neb/plugin/plugin.go +++ b/src/github.com/matrix-org/go-neb/plugin/plugin.go @@ -31,7 +31,7 @@ type Command struct { // the appropriate RFC. type Expansion struct { Regexp *regexp.Regexp - Expand func(roomID, userID, matchingText string) interface{} + Expand func(roomID, userID string, matchingGroups []string) interface{} } // matches if the arguments start with the path of the command. @@ -94,13 +94,14 @@ func runExpansionsForPlugin(plugin Plugin, event *matrix.Event, body string) []i for _, expansion := range plugin.Expansions { matches := map[string]bool{} - for _, matchingText := range expansion.Regexp.FindAllString(body, -1) { + for _, matchingGroups := range expansion.Regexp.FindAllStringSubmatch(body, -1) { + matchingText := matchingGroups[0] // first element is always the complete match if matches[matchingText] { // Only expand the first occurance of a matching string continue } matches[matchingText] = true - if response := expansion.Expand(event.RoomID, event.Sender, matchingText); response != nil { + if response := expansion.Expand(event.RoomID, event.Sender, matchingGroups); response != nil { responses = append(responses, response) } } diff --git a/src/github.com/matrix-org/go-neb/plugin/plugin_test.go b/src/github.com/matrix-org/go-neb/plugin/plugin_test.go index 6afe6ab..6ef4c96 100644 --- a/src/github.com/matrix-org/go-neb/plugin/plugin_test.go +++ b/src/github.com/matrix-org/go-neb/plugin/plugin_test.go @@ -34,13 +34,13 @@ func makeTestResponse(roomID, sender string, arguments []string) interface{} { } type testExpansion struct { - RoomID string - UserID string - MatchingText string + RoomID string + UserID string + MatchingGroups []string } -func makeTestExpansion(roomID, userID, matchingText string) interface{} { - return testExpansion{roomID, userID, matchingText} +func makeTestExpansion(roomID, userID string, matchingGroups []string) interface{} { + return testExpansion{roomID, userID, matchingGroups} } func makeTestPlugin(paths [][]string, regexps []*regexp.Regexp) Plugin { @@ -133,9 +133,9 @@ func TestExpansion(t *testing.T) { event := makeTestEvent("m.text", "test banana for scale") got := runCommands(plugins, event) want := []interface{}{ - makeTestExpansion(myRoomID, mySender, "anana"), - makeTestExpansion(myRoomID, mySender, "ale"), - makeTestExpansion(myRoomID, mySender, "ban"), + makeTestExpansion(myRoomID, mySender, []string{"anana"}), + makeTestExpansion(myRoomID, mySender, []string{"ale"}), + makeTestExpansion(myRoomID, mySender, []string{"ban"}), } if !reflect.DeepEqual(got, want) { t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want) @@ -151,7 +151,7 @@ func TestExpansionDuplicateMatches(t *testing.T) { event := makeTestEvent("m.text", "badger badger badger") got := runCommands(plugins, event) want := []interface{}{ - makeTestExpansion(myRoomID, mySender, "badger"), + makeTestExpansion(myRoomID, mySender, []string{"badger"}), } if !reflect.DeepEqual(got, want) { t.Errorf("runCommands(\nplugins=%+v\nevent=%+v\n)\n%+v\nwanted: %+v", plugins, event, got, want) diff --git a/src/github.com/matrix-org/go-neb/services/github/github.go b/src/github.com/matrix-org/go-neb/services/github/github.go index 5b566ff..044aafa 100644 --- a/src/github.com/matrix-org/go-neb/services/github/github.go +++ b/src/github.com/matrix-org/go-neb/services/github/github.go @@ -102,14 +102,21 @@ func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (i return matrix.TextMessage{"m.notice", fmt.Sprintf("Created issue: %s", *issue.HTMLURL)}, nil } -func (s *githubService) expandIssue(roomID, userID, matchingText string) interface{} { - cli := s.githubClientFor(userID, true) - owner, repo, num, err := ownerRepoNumberFromText(matchingText) +func (s *githubService) expandIssue(roomID, userID string, matchingGroups []string) interface{} { + // matchingGroups => ["foo/bar#11", "foo", "bar", "11"] + if len(matchingGroups) != 4 { + log.WithField("groups", matchingGroups).Print("Unexpected number of groups") + return nil + } + num, err := strconv.Atoi(matchingGroups[3]) if err != nil { - log.WithError(err).WithField("text", matchingText).Print( - "Failed to extract owner,repo,number from matched string") + log.WithField("issue_number", matchingGroups[3]).Print("Bad issue number") return nil } + owner := matchingGroups[1] + repo := matchingGroups[2] + + cli := s.githubClientFor(userID, true) i, _, err := cli.Issues.Get(owner, repo, num) if err != nil { @@ -140,8 +147,8 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin { Expansions: []plugin.Expansion{ plugin.Expansion{ Regexp: ownerRepoIssueRegex, - Expand: func(roomID, userID, matchingText string) interface{} { - return s.expandIssue(roomID, userID, matchingText) + Expand: func(roomID, userID string, matchingGroups []string) interface{} { + return s.expandIssue(roomID, userID, matchingGroups) }, }, }, @@ -322,21 +329,6 @@ func getTokenForUser(realmID, userID string) (string, error) { return ghSession.AccessToken, nil } -// ownerRepoNumberFromText parses a GH issue string that looks like 'owner/repo#11' -// into its constituient parts. Returns: owner, repo, issue#. -func ownerRepoNumberFromText(ownerRepoNumberText string) (string, string, int, error) { - // [full_string, owner, repo, issue_number] - groups := ownerRepoIssueRegex.FindStringSubmatch(ownerRepoNumberText) - if len(groups) != 4 { - return "", "", 0, fmt.Errorf("No match found for '%s'", ownerRepoNumberText) - } - num, err := strconv.Atoi(groups[3]) - if err != nil { - return "", "", 0, err - } - return groups[1], groups[2], num, nil -} - func removeHook(logger *log.Entry, cli *github.Client, owner, repo, webhookEndpointURL string) { // Get a list of webhooks for this owner/repo and find the one which has the // same endpoint URL which is what github uses to determine equivalence. diff --git a/src/github.com/matrix-org/go-neb/services/jira/jira.go b/src/github.com/matrix-org/go-neb/services/jira/jira.go index 8793705..bd73494 100644 --- a/src/github.com/matrix-org/go-neb/services/jira/jira.go +++ b/src/github.com/matrix-org/go-neb/services/jira/jira.go @@ -148,17 +148,15 @@ func (s *jiraService) cmdJiraCreate(roomID, userID string, args []string) (inter }, nil } -func (s *jiraService) expandIssue(roomID, userID, issueKey string) interface{} { - issueKey = strings.ToUpper(issueKey) - logger := log.WithField("issue_key", issueKey) - // [ISSU-123, ISSU, 123] - groups := issueKeyRegex.FindStringSubmatch(issueKey) - if len(groups) != 3 { - logger.Print("Failed to find issue key") +func (s *jiraService) expandIssue(roomID, userID string, issueKeyGroups []string) interface{} { + // issueKeyGroups => ["SYN-123", "SYN", "123"] + if len(issueKeyGroups) != 3 { + log.WithField("groups", issueKeyGroups).Error("Bad number of groups") return nil } - - projectKey := groups[1] + issueKey := strings.ToUpper(issueKeyGroups[0]) + logger := log.WithField("issue_key", issueKey) + projectKey := strings.ToUpper(issueKeyGroups[1]) realmID := s.realmIDForProject(roomID, projectKey) if realmID == "" { @@ -223,8 +221,8 @@ func (s *jiraService) Plugin(roomID string) plugin.Plugin { Expansions: []plugin.Expansion{ plugin.Expansion{ Regexp: issueKeyRegex, - Expand: func(roomID, userID, issueKey string) interface{} { - return s.expandIssue(roomID, userID, issueKey) + Expand: func(roomID, userID string, issueKeyGroups []string) interface{} { + return s.expandIssue(roomID, userID, issueKeyGroups) }, }, },