From c85abe84da8e21a17875d23f63cedfb684506296 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Wed, 23 Aug 2017 16:34:02 +0100
Subject: [PATCH] update defaultRepo wording
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
---
.../go-neb/services/github/github.go | 70 ++++++++++++++++++-
1 file changed, 67 insertions(+), 3 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 969bce0..068db63 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
@@ -11,6 +11,7 @@ import (
"strconv"
"strings"
+ "bytes"
log "github.com/Sirupsen/logrus"
gogithub "github.com/google/go-github/github"
"github.com/matrix-org/go-neb/database"
@@ -77,7 +78,64 @@ func (s *Service) requireGithubClientFor(userID string) (cli *gogithub.Client, r
return
}
-const cmdGithubCreateUsage = `!github create owner/repo "issue title" "description"`
+const numberGithubSearchSummaries = 3
+const cmdGithubSearchUsage = `!github create owner/repo "search query"`
+
+func (s *Service) cmdGithubSearch(roomID, userID string, args []string) (interface{}, error) {
+ cli, resp, err := s.requireGithubClientFor(userID)
+ if cli == nil {
+ return resp, err
+ }
+ if len(args) < 2 {
+ return &gomatrix.TextMessage{"m.notice", "Usage: " + cmdGithubSearchUsage}, nil
+ }
+
+ query := fmt.Sprintf("repo:%s %s", args[0], strings.Join(args[1:], " "))
+ searchResult, res, err := cli.Search.Issues(query, nil)
+
+ if err != nil {
+ log.WithField("err", err).Print("Failed to react to issue")
+ if res == nil {
+ return nil, fmt.Errorf("Failed to react to issue. Failed to connect to Github")
+ }
+ return nil, fmt.Errorf("Failed to react to issue. HTTP %d", res.StatusCode)
+ }
+
+ if searchResult.Total == nil || *searchResult.Total == 0 {
+ return &gomatrix.TextMessage{"m.notice", "No results found for your search query!"}, nil
+ }
+
+ numResults := *searchResult.Total
+ var htmlBuffer bytes.Buffer
+ var plainBuffer bytes.Buffer
+
+ numberOfSummaries := numberGithubSearchSummaries
+ if numResults < numberGithubSearchSummaries {
+ numberOfSummaries = numResults
+ }
+
+ summarizedIssues := searchResult.Issues[0:numberOfSummaries]
+
+ htmlBuffer.WriteString(fmt.Sprintf("Found %d results, here are the most relevant: