Browse Source

DRY code - [NEEDS TESTING]

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/206/head
Michael Telatynski 7 years ago
parent
commit
8f60839c77
No known key found for this signature in database GPG Key ID: 3F879DA5AD802A5E
  1. 48
      src/github.com/matrix-org/go-neb/services/github/github.go

48
src/github.com/matrix-org/go-neb/services/github/github.go

@ -332,70 +332,46 @@ func (s *Service) cmdGithubAssign(roomID, userID string, args []string) (interfa
return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Added assignees to issue: %s", *issue.HTMLURL)}, nil return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Added assignees to issue: %s", *issue.HTMLURL)}, nil
} }
const cmdGithubCloseUsage = `!github close [owner/repo]#issue`
func (s *Service) cmdGithubClose(roomID, userID string, args []string) (interface{}, error) {
func (s *Service) githubIssueCloseReopen(roomID, userID string, args []string, state, verb, help string) (interface{}, error) {
cli, resp, err := s.requireGithubClientFor(userID) cli, resp, err := s.requireGithubClientFor(userID)
if cli == nil { if cli == nil {
return resp, err return resp, err
} }
if len(args) == 0 { if len(args) == 0 {
return &gomatrix.TextMessage{"m.notice", "Usage: " + cmdGithubCloseUsage}, nil
return &gomatrix.TextMessage{"m.notice", "Usage: " + help}, nil
} }
// get owner,repo,issue,resp out of args[0] // get owner,repo,issue,resp out of args[0]
owner, repo, issueNum, resp := s.getIssueDetailsFor(args[0], roomID, cmdGithubCloseUsage)
owner, repo, issueNum, resp := s.getIssueDetailsFor(args[0], roomID, help)
if resp != nil { if resp != nil {
return resp, nil return resp, nil
} }
state := "closed"
issueComment, res, err := cli.Issues.Edit(owner, repo, issueNum, &gogithub.IssueRequest{ issueComment, res, err := cli.Issues.Edit(owner, repo, issueNum, &gogithub.IssueRequest{
State: &state, State: &state,
}) })
if err != nil { if err != nil {
log.WithField("err", err).Print("Failed to close issue")
log.WithField("err", err).Print("Failed to %s issue", verb)
if res == nil { if res == nil {
return nil, fmt.Errorf("Failed to close issue. Failed to connect to Github")
return nil, fmt.Errorf("Failed to %s issue. Failed to connect to Github", verb)
} }
return nil, fmt.Errorf("Failed to close issue. HTTP %d", res.StatusCode)
return nil, fmt.Errorf("Failed to %s issue. HTTP %d", verb, res.StatusCode)
} }
return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Closed issue: %s", *issueComment.HTMLURL)}, nil return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Closed issue: %s", *issueComment.HTMLURL)}, nil
} }
const cmdGithubReopenUsage = `!github reopen [owner/repo]#issue`
func (s *Service) cmdGithubReopen(roomID, userID string, args []string) (interface{}, error) {
cli, resp, err := s.requireGithubClientFor(userID)
if cli == nil {
return resp, err
}
if len(args) == 0 {
return &gomatrix.TextMessage{"m.notice", "Usage: " + cmdGithubReopenUsage}, nil
}
const cmdGithubCloseUsage = `!github close [owner/repo]#issue`
// get owner,repo,issue,resp out of args[0]
owner, repo, issueNum, resp := s.getIssueDetailsFor(args[0], roomID, cmdGithubReopenUsage)
if resp != nil {
return resp, nil
func (s *Service) cmdGithubClose(roomID, userID string, args []string) (interface{}, error) {
return s.githubIssueCloseReopen(roomID, userID, args, "closed", "close", cmdGithubCloseUsage)
} }
state := "open"
issueComment, res, err := cli.Issues.Edit(owner, repo, issueNum, &gogithub.IssueRequest{
State: &state,
})
if err != nil {
log.WithField("err", err).Print("Failed to reopen issue")
if res == nil {
return nil, fmt.Errorf("Failed to reopen issue. Failed to connect to Github")
}
return nil, fmt.Errorf("Failed to reopen issue. HTTP %d", res.StatusCode)
}
const cmdGithubReopenUsage = `!github reopen [owner/repo]#issue`
return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Reopened issue: %s", *issueComment.HTMLURL)}, nil
func (s *Service) cmdGithubReopen(roomID, userID string, args []string) (interface{}, error) {
return s.githubIssueCloseReopen(roomID, userID, args, "closed", "close", cmdGithubCloseUsage)
} }
func (s *Service) getIssueDetailsFor(input, roomID, usage string) (owner, repo string, issueNum int, resp interface{}) { func (s *Service) getIssueDetailsFor(input, roomID, usage string) (owner, repo string, issueNum int, resp interface{}) {

Loading…
Cancel
Save