Browse Source

Make regexp top-level as per PR comments

kegan/github
Kegan Dougal 9 years ago
parent
commit
d6b946f200
  1. 8
      src/github.com/matrix-org/go-neb/services/github/github.go

8
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. // 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 // 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 { type githubService struct {
id string id string
@ -31,7 +31,7 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin {
Commands: []plugin.Command{}, Commands: []plugin.Command{},
Expansions: []plugin.Expansion{ Expansions: []plugin.Expansion{
plugin.Expansion{ plugin.Expansion{
Regexp: regexp.MustCompile(ownerRepoIssueRegex),
Regexp: ownerRepoIssueRegex,
Expand: func(roomID, matchingText string) interface{} { Expand: func(roomID, matchingText string) interface{} {
cli := githubClient("") cli := githubClient("")
owner, repo, num, err := ownerRepoNumberFromText(matchingText) 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' // ownerRepoNumberFromText parses a GH issue string that looks like 'owner/repo#11'
// into its constituient parts. Returns: owner, repo, issue#. // into its constituient parts. Returns: owner, repo, issue#.
func ownerRepoNumberFromText(ownerRepoNumberText string) (string, string, int, error) { func ownerRepoNumberFromText(ownerRepoNumberText string) (string, string, int, error) {
// TODO: cache this?
re := regexp.MustCompile(ownerRepoIssueRegex)
// [full_string, owner, repo, issue_number] // [full_string, owner, repo, issue_number]
groups := re.FindStringSubmatch(ownerRepoNumberText)
groups := ownerRepoIssueRegex.FindStringSubmatch(ownerRepoNumberText)
if len(groups) != 4 { if len(groups) != 4 {
return "", "", 0, fmt.Errorf("No match found for '%s'", ownerRepoNumberText) return "", "", 0, fmt.Errorf("No match found for '%s'", ownerRepoNumberText)
} }

Loading…
Cancel
Save