|
|
@ -1,6 +1,7 @@ |
|
|
|
package services |
|
|
|
|
|
|
|
import ( |
|
|
|
"database/sql" |
|
|
|
"fmt" |
|
|
|
log "github.com/Sirupsen/logrus" |
|
|
|
"github.com/google/go-github/github" |
|
|
@ -21,7 +22,7 @@ import ( |
|
|
|
|
|
|
|
// 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
|
|
|
|
var ownerRepoIssueRegex = regexp.MustCompile("([A-z0-9-_]+)/([A-z0-9-_]+)#([0-9]+)") |
|
|
|
var ownerRepoIssueRegex = regexp.MustCompile(`(([A-z0-9-_]+)/([A-z0-9-_]+))?#([0-9]+)`) |
|
|
|
|
|
|
|
type githubService struct { |
|
|
|
id string |
|
|
@ -62,9 +63,19 @@ func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (i |
|
|
|
}, nil |
|
|
|
} |
|
|
|
|
|
|
|
if len(args) < 2 { |
|
|
|
return &matrix.TextMessage{"m.notice", |
|
|
|
`Usage: !github create owner/repo "issue title" "description"`}, nil |
|
|
|
// We expect the args to look like:
|
|
|
|
// [ "owner/repo", "title text", "desc text" ]
|
|
|
|
// They can omit the owner/repo if there is a default one set.
|
|
|
|
|
|
|
|
if len(args) < 2 || strings.Count(args[0], "/") != 1 { |
|
|
|
// look for a default repo
|
|
|
|
defaultRepo := s.defaultRepo(roomID) |
|
|
|
if defaultRepo == "" { |
|
|
|
return &matrix.TextMessage{"m.notice", |
|
|
|
`Usage: !github create owner/repo "issue title" "description"`}, nil |
|
|
|
} |
|
|
|
// insert the default as the first arg to reuse the same code path
|
|
|
|
args = append([]string{defaultRepo}, args...) |
|
|
|
} |
|
|
|
|
|
|
|
var ( |
|
|
@ -101,31 +112,15 @@ func (s *githubService) cmdGithubCreate(roomID, userID string, args []string) (i |
|
|
|
return matrix.TextMessage{"m.notice", fmt.Sprintf("Created issue: %s", *issue.HTMLURL)}, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (s *githubService) expandIssue(roomID, userID string, matchingGroups []string) interface{} { |
|
|
|
if !s.HandleExpansions { |
|
|
|
return nil |
|
|
|
} |
|
|
|
// 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]) |
|
|
|
if err != nil { |
|
|
|
log.WithField("issue_number", matchingGroups[3]).Print("Bad issue number") |
|
|
|
return nil |
|
|
|
} |
|
|
|
owner := matchingGroups[1] |
|
|
|
repo := matchingGroups[2] |
|
|
|
|
|
|
|
func (s *githubService) expandIssue(roomID, userID, owner, repo string, issueNum int) interface{} { |
|
|
|
cli := s.githubClientFor(userID, true) |
|
|
|
|
|
|
|
i, _, err := cli.Issues.Get(owner, repo, num) |
|
|
|
i, _, err := cli.Issues.Get(owner, repo, issueNum) |
|
|
|
if err != nil { |
|
|
|
log.WithError(err).WithFields(log.Fields{ |
|
|
|
"owner": owner, |
|
|
|
"repo": repo, |
|
|
|
"number": num, |
|
|
|
"number": issueNum, |
|
|
|
}).Print("Failed to fetch issue") |
|
|
|
return nil |
|
|
|
} |
|
|
@ -150,7 +145,47 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin { |
|
|
|
plugin.Expansion{ |
|
|
|
Regexp: ownerRepoIssueRegex, |
|
|
|
Expand: func(roomID, userID string, matchingGroups []string) interface{} { |
|
|
|
return s.expandIssue(roomID, userID, matchingGroups) |
|
|
|
if !s.HandleExpansions { |
|
|
|
return nil |
|
|
|
} |
|
|
|
// 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 |
|
|
|
} |
|
|
|
if matchingGroups[1] == "" && matchingGroups[2] == "" && matchingGroups[3] == "" { |
|
|
|
// issue only match, this only works if there is a default repo
|
|
|
|
defaultRepo := s.defaultRepo(roomID) |
|
|
|
if defaultRepo == "" { |
|
|
|
return nil |
|
|
|
} |
|
|
|
segs := strings.Split(defaultRepo, "/") |
|
|
|
if len(segs) != 2 { |
|
|
|
log.WithFields(log.Fields{ |
|
|
|
"room_id": roomID, |
|
|
|
"default_repo": defaultRepo, |
|
|
|
}).Error("Default repo is malformed") |
|
|
|
return nil |
|
|
|
} |
|
|
|
// Fill in the missing fields in matching groups and fall through into ["foo/bar#11", "foo", "bar", "11"]
|
|
|
|
matchingGroups = []string{ |
|
|
|
defaultRepo + matchingGroups[0], |
|
|
|
defaultRepo, |
|
|
|
segs[0], |
|
|
|
segs[1], |
|
|
|
matchingGroups[4], |
|
|
|
} |
|
|
|
} |
|
|
|
num, err := strconv.Atoi(matchingGroups[4]) |
|
|
|
if err != nil { |
|
|
|
log.WithField("issue_number", matchingGroups[4]).Print("Bad issue number") |
|
|
|
return nil |
|
|
|
} |
|
|
|
return s.expandIssue(roomID, userID, matchingGroups[2], matchingGroups[3], num) |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
@ -285,6 +320,36 @@ func (s *githubService) Register(oldService types.Service, client *matrix.Client |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// defaultRepo returns the default repo for the given room, or an empty string.
|
|
|
|
func (s *githubService) defaultRepo(roomID string) string { |
|
|
|
logger := log.WithFields(log.Fields{ |
|
|
|
"room_id": roomID, |
|
|
|
"bot_user_id": s.serviceUserID, |
|
|
|
}) |
|
|
|
opts, err := database.GetServiceDB().LoadBotOptions(s.serviceUserID, roomID) |
|
|
|
if err != nil { |
|
|
|
if err != sql.ErrNoRows { |
|
|
|
logger.WithError(err).Error("Failed to load bot options") |
|
|
|
} |
|
|
|
return "" |
|
|
|
} |
|
|
|
// Expect opts to look like:
|
|
|
|
// { github: { default_repo: $OWNER_REPO } }
|
|
|
|
ghOpts, ok := opts.Options["github"].(map[string]interface{}) |
|
|
|
if !ok { |
|
|
|
logger.WithField("options", opts.Options).Error("Failed to cast bot options as github options") |
|
|
|
return "" |
|
|
|
} |
|
|
|
defaultRepo, ok := ghOpts["default_repo"].(string) |
|
|
|
if !ok { |
|
|
|
logger.WithField("default_repo", ghOpts["default_repo"]).Error( |
|
|
|
"Failed to cast default repo as a string", |
|
|
|
) |
|
|
|
return "" |
|
|
|
} |
|
|
|
return defaultRepo |
|
|
|
} |
|
|
|
|
|
|
|
func (s *githubService) joinWebhookRooms(client *matrix.Client) error { |
|
|
|
for roomID := range s.Rooms { |
|
|
|
if _, err := client.JoinRoom(roomID, "", ""); err != nil { |
|
|
|