From a202f3d463211b042984534f1f24dd30f081d20a Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Sun, 31 Mar 2019 18:46:47 +0100 Subject: [PATCH] Require a repo for search, either given or default --- .../go-neb/services/github/github.go | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 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 245c7c0..5f00e11 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 @@ -89,14 +89,36 @@ func (s *Service) requireGithubClientFor(userID string) (cli *gogithub.Client, r } const numberGithubSearchSummaries = 3 -const cmdGithubSearchUsage = `!github search owner/repo "search query"` +const cmdGithubSearchUsage = `!github search [owner/repo] "search query"` func (s *Service) cmdGithubSearch(roomID, userID string, args []string) (interface{}, error) { cli := s.githubClientFor(userID, true) - if len(args) < 2 { + if len(args) < 1 { return &gomatrix.TextMessage{"m.notice", "Usage: " + cmdGithubSearchUsage}, nil } + // Look for a default if the first arg doesn't look like an owner/repo + ownerRepoGroups := ownerRepoRegex.FindStringSubmatch(args[0]) + + if len(ownerRepoGroups) == 0 { + // look for a default repo + defaultRepo := s.defaultRepo(roomID) + if defaultRepo == "" { + return &gomatrix.TextMessage{"m.notice", "Need to specify repo. Usage: " + cmdGithubSearchUsage}, nil + } + // default repo should pass the regexp + ownerRepoGroups = ownerRepoRegex.FindStringSubmatch(defaultRepo) + if len(ownerRepoGroups) == 0 { + return &gomatrix.TextMessage{"m.notice", "Malformed default repo. Usage: " + cmdGithubSearchUsage}, nil + } + + // insert the default as the first arg to reuse the same indices + args = append([]string{defaultRepo}, args...) + // continue through now that ownerRepoGroups has matching groups + } + + // The first arg is always a repo, and should have the repo: prefix. + args[0] = fmt.Sprintf("repo:%s", args[0]) query := strings.Join(args, " ") searchResult, res, err := cli.Search.Issues(query, nil)