Browse Source

Combine together short and long form regexp matching to avoid expanding twice on long form

pull/47/head
Kegan Dougal 8 years ago
parent
commit
95d8f86fa4
  1. 50
      src/github.com/matrix-org/go-neb/services/github/github.go

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

@ -22,8 +22,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
var ownerRepoIssueRegex = regexp.MustCompile("([A-z0-9-_]+)/([A-z0-9-_]+)#([0-9]+)")
var issueRegex = regexp.MustCompile("#([0-9]+)")
var ownerRepoIssueRegex = regexp.MustCompile(`(([A-z0-9-_]+)/([A-z0-9-_]+))?#([0-9]+)`)
type githubService struct { type githubService struct {
id string id string
@ -114,18 +113,14 @@ func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (i
} }
func (s *githubService) expandIssue(roomID, userID string, matchingGroups []string) interface{} { 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])
// matchingGroups => ["foo/bar#11", "foo/bar", "foo", "bar", "11"]
num, err := strconv.Atoi(matchingGroups[4])
if err != nil { if err != nil {
log.WithField("issue_number", matchingGroups[3]).Print("Bad issue number")
log.WithField("issue_number", matchingGroups[4]).Print("Bad issue number")
return nil return nil
} }
owner := matchingGroups[1]
repo := matchingGroups[2]
owner := matchingGroups[2]
repo := matchingGroups[3]
cli := s.githubClientFor(userID, true) cli := s.githubClientFor(userID, true)
@ -162,20 +157,18 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin {
if !s.HandleExpansions { if !s.HandleExpansions {
return nil return nil
} }
return s.expandIssue(roomID, userID, matchingGroups)
},
},
plugin.Expansion{
Regexp: issueRegex,
Expand: func(roomID, userID string, matchingGroups []string) interface{} {
if !s.HandleExpansions {
// There's an optional group in the regex so matchingGroups can look like:
// [foo/bar#55 foo/bar foo bar 55]
// [#55 55]
if len(matchingGroups) != 5 {
log.WithField("groups", matchingGroups).WithField("len", len(matchingGroups)).Print(
"Unexpected number of groups",
)
return nil return nil
} }
// This expansion only works if there is a default repo. If there is a default,
// convert the matchingGroups of [ "#11", "11" ] into:
// ["foo/bar#11", "foo", "bar", "11"]
// and then use the same code path as the longer form.
log.Print(matchingGroups)
if matchingGroups[1] == "" && matchingGroups[2] == "" && matchingGroups[3] == "" {
// issue only match, this only works if there is a default repo
defaultRepo := s.defaultRepo(roomID) defaultRepo := s.defaultRepo(roomID)
if defaultRepo == "" { if defaultRepo == "" {
return nil return nil
@ -188,13 +181,16 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin {
}).Error("Default repo is malformed") }).Error("Default repo is malformed")
return nil return nil
} }
// convert ["#11", "11"] into ["foo/bar#11", "foo", "bar", "11"]
return s.expandIssue(roomID, userID, []string{
// Fill in the missing fields in matching groups and fall through into ["foo/bar#11", "foo", "bar", "11"]
matchingGroups = []string{
defaultRepo + matchingGroups[0], defaultRepo + matchingGroups[0],
defaultRepo,
segs[0], segs[0],
segs[1], segs[1],
matchingGroups[1],
})
matchingGroups[4],
}
}
return s.expandIssue(roomID, userID, matchingGroups)
}, },
}, },
}, },

Loading…
Cancel
Save