From d6b946f200b4300c117fdadf3aca4f8dba11dc92 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 1 Aug 2016 11:58:41 +0100 Subject: [PATCH] Make regexp top-level as per PR comments --- .../matrix-org/go-neb/services/github/github.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) 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 1ed5f88..df58df0 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 @@ -14,7 +14,7 @@ import ( // Matches alphanumeric then a /, then more alphanumeric then a #, then a number. // E.g. owner/repo#11 (issue/PR numbers) - Captured groups for owner/repo/number -const ownerRepoIssueRegex = "([A-z0-9-_]+)/([A-z0-9-_]+)#([0-9]+)" +var ownerRepoIssueRegex = regexp.MustCompile("([A-z0-9-_]+)/([A-z0-9-_]+)#([0-9]+)") type githubService struct { id string @@ -31,7 +31,7 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin { Commands: []plugin.Command{}, Expansions: []plugin.Expansion{ plugin.Expansion{ - Regexp: regexp.MustCompile(ownerRepoIssueRegex), + Regexp: ownerRepoIssueRegex, Expand: func(roomID, matchingText string) interface{} { cli := githubClient("") owner, repo, num, err := ownerRepoNumberFromText(matchingText) @@ -78,10 +78,8 @@ func githubClient(token string) *github.Client { // 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) { - // TODO: cache this? - re := regexp.MustCompile(ownerRepoIssueRegex) // [full_string, owner, repo, issue_number] - groups := re.FindStringSubmatch(ownerRepoNumberText) + groups := ownerRepoIssueRegex.FindStringSubmatch(ownerRepoNumberText) if len(groups) != 4 { return "", "", 0, fmt.Errorf("No match found for '%s'", ownerRepoNumberText) }