From 0dd4d357ce9d138bebca84937230dfa947e2127b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 15 Aug 2016 10:23:02 +0100 Subject: [PATCH 1/2] Bring out expand/cmd into stand-alone functions --- .../go-neb/services/github/github.go | 141 ++++++++++-------- 1 file changed, 75 insertions(+), 66 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 f0b2bb6..4fc9dd4 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 @@ -45,56 +45,86 @@ func (s *githubService) RoomIDs() []string { } return keys } + +func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (interface{}, error) { + cli := s.githubClientFor(userID, false) + if cli == nil { + // TODO: send starter link + return &matrix.TextMessage{"m.notice", + userID + " : You have not linked your Github account."}, nil + } + + if len(args) < 2 { + return &matrix.TextMessage{"m.notice", + `Usage: !github create owner/repo "issue title" "description"`}, nil + } + + var ( + ownerRepo string + title *string + desc *string + ) + ownerRepo = args[0] + o := strings.Split(ownerRepo, "/") + if len(o) != 2 { + return &matrix.TextMessage{"m.notice", + `Usage: !github create owner/repo "issue title" "description"`}, nil + } + + if len(args) == 2 { + title = &args[1] + } else if len(args) == 3 { + title = &args[1] + desc = &args[2] + } else { // > 3 args is probably a title without quote marks + joinedTitle := strings.Join(args[1:], " ") + title = &joinedTitle + } + + issue, res, err := cli.Issues.Create(o[0], o[1], &github.IssueRequest{ + Title: title, + Body: desc, + }) + if err != nil { + log.WithField("err", err).Print("Failed to create issue") + return nil, fmt.Errorf("Failed to create issue. HTTP %d", res.StatusCode) + } + + return matrix.TextMessage{"m.notice", fmt.Sprintf("Created issue: %s", *issue.HTMLURL)}, nil +} + +func (s *githubService) expandIssue(roomID, userID, matchingText string) interface{} { + cli := s.githubClientFor(userID, true) + owner, repo, num, err := ownerRepoNumberFromText(matchingText) + if err != nil { + log.WithError(err).WithField("text", matchingText).Print( + "Failed to extract owner,repo,number from matched string") + return nil + } + + i, _, err := cli.Issues.Get(owner, repo, num) + if err != nil { + log.WithError(err).WithFields(log.Fields{ + "owner": owner, + "repo": repo, + "number": num, + }).Print("Failed to fetch issue") + return nil + } + + return &matrix.TextMessage{ + "m.notice", + fmt.Sprintf("%s : %s", *i.HTMLURL, *i.Title), + } +} + func (s *githubService) Plugin(roomID string) plugin.Plugin { return plugin.Plugin{ Commands: []plugin.Command{ plugin.Command{ Path: []string{"github", "create"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - cli := s.githubClientFor(userID, false) - if cli == nil { - // TODO: send starter link - return &matrix.TextMessage{"m.notice", - userID + " : You have not linked your Github account."}, nil - } - - if len(args) < 2 { - return &matrix.TextMessage{"m.notice", - `Usage: !github create owner/repo "issue title" "description"`}, nil - } - - var ( - ownerRepo string - title *string - desc *string - ) - ownerRepo = args[0] - o := strings.Split(ownerRepo, "/") - if len(o) != 2 { - return &matrix.TextMessage{"m.notice", - `Usage: !github create owner/repo "issue title" "description"`}, nil - } - - if len(args) == 2 { - title = &args[1] - } else if len(args) == 3 { - title = &args[1] - desc = &args[2] - } else { // > 3 args is probably a title without quote marks - joinedTitle := strings.Join(args[1:], " ") - title = &joinedTitle - } - - issue, res, err := cli.Issues.Create(o[0], o[1], &github.IssueRequest{ - Title: title, - Body: desc, - }) - if err != nil { - log.WithField("err", err).Print("Failed to create issue") - return nil, fmt.Errorf("Failed to create issue. HTTP %d", res.StatusCode) - } - - return matrix.TextMessage{"m.notice", fmt.Sprintf("Created issue: %s", *issue.HTMLURL)}, nil + return s.cmdGithubCreate(roomID, userID, args) }, }, }, @@ -102,28 +132,7 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin { plugin.Expansion{ Regexp: ownerRepoIssueRegex, Expand: func(roomID, userID, matchingText string) interface{} { - cli := s.githubClientFor(userID, true) - owner, repo, num, err := ownerRepoNumberFromText(matchingText) - if err != nil { - log.WithError(err).WithField("text", matchingText).Print( - "Failed to extract owner,repo,number from matched string") - return nil - } - - i, _, err := cli.Issues.Get(owner, repo, num) - if err != nil { - log.WithError(err).WithFields(log.Fields{ - "owner": owner, - "repo": repo, - "number": num, - }).Print("Failed to fetch issue") - return nil - } - - return &matrix.TextMessage{ - "m.notice", - fmt.Sprintf("%s : %s", *i.HTMLURL, *i.Title), - } + return s.expandIssue(roomID, userID, matchingText) }, }, }, From 5bccd5219050298e8cb633e3c9563c0e6d1d98b4 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 15 Aug 2016 10:38:09 +0100 Subject: [PATCH 2/2] Add StarterLink as a field to the Github Service config Use it verbatim in the responses to !commands when the caller has not authed with Github. --- .../matrix-org/go-neb/matrix/types.go | 27 +++++++++++++++++++ .../go-neb/services/github/github.go | 8 +++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/matrix/types.go b/src/github.com/matrix-org/go-neb/matrix/types.go index 0db9054..900b16a 100644 --- a/src/github.com/matrix-org/go-neb/matrix/types.go +++ b/src/github.com/matrix-org/go-neb/matrix/types.go @@ -1,6 +1,7 @@ package matrix import ( + "encoding/json" "html" "regexp" ) @@ -114,3 +115,29 @@ func GetHTMLMessage(msgtype, htmlText string) HTMLMessage { FormattedBody: htmlText, } } + +// StarterLinkMessage represents a message with a starter_link custom data. +type StarterLinkMessage struct { + Body string + Link string +} + +// MarshalJSON converts this message into actual event content JSON. +func (m StarterLinkMessage) MarshalJSON() ([]byte, error) { + var data map[string]string + + if m.Link != "" { + data = map[string]string{ + "org.matrix.neb.starter_link": m.Link, + } + } + + msg := struct { + MsgType string `json:"msgtype"` + Body string `json:"body"` + Data map[string]string `json:"data,omitempty"` + }{ + "m.notice", m.Body, data, + } + return json.Marshal(msg) +} 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 4fc9dd4..75bc820 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 @@ -28,6 +28,7 @@ type githubService struct { ClientUserID string RealmID string SecretToken string + StarterLink string Rooms map[string]struct { // room_id => {} Repos map[string]struct { // owner/repo => { events: ["push","issue","pull_request"] } Events []string @@ -49,9 +50,10 @@ func (s *githubService) RoomIDs() []string { func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (interface{}, error) { cli := s.githubClientFor(userID, false) if cli == nil { - // TODO: send starter link - return &matrix.TextMessage{"m.notice", - userID + " : You have not linked your Github account."}, nil + return matrix.StarterLinkMessage{ + Body: "You need to OAuth with Github before you can create issues.", + Link: s.StarterLink, + }, nil } if len(args) < 2 {