Browse Source

Merge branch 'kegan/github-starter-links' into kegan/jira-starter-links

kegan/jira-starter-links
Kegan Dougal 8 years ago
parent
commit
c651293452
  1. 27
      src/github.com/matrix-org/go-neb/matrix/types.go
  2. 143
      src/github.com/matrix-org/go-neb/services/github/github.go

27
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)
}

143
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
@ -45,56 +46,87 @@ 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 {
return matrix.StarterLinkMessage{
Body: "You need to OAuth with Github before you can create issues.",
Link: s.StarterLink,
}, 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 +134,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)
},
},
},

Loading…
Cancel
Save