From 8f60839c77f48307657784c36819621ee45e1b4c Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 22 Oct 2017 11:35:42 +0100 Subject: [PATCH] DRY code - [NEEDS TESTING] Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../go-neb/services/github/github.go | 50 +++++-------------- 1 file changed, 13 insertions(+), 37 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 12349de..87b69d6 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 @@ -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 } -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) if cli == nil { return resp, err } 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] - owner, repo, issueNum, resp := s.getIssueDetailsFor(args[0], roomID, cmdGithubCloseUsage) + owner, repo, issueNum, resp := s.getIssueDetailsFor(args[0], roomID, help) if resp != nil { return resp, nil } - state := "closed" issueComment, res, err := cli.Issues.Edit(owner, repo, issueNum, &gogithub.IssueRequest{ State: &state, }) 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 { - 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 } -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 - } - - // 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 - } +const cmdGithubCloseUsage = `!github close [owner/repo]#issue` - state := "open" - issueComment, res, err := cli.Issues.Edit(owner, repo, issueNum, &gogithub.IssueRequest{ - State: &state, - }) +func (s *Service) cmdGithubClose(roomID, userID string, args []string) (interface{}, error) { + return s.githubIssueCloseReopen(roomID, userID, args, "closed", "close", cmdGithubCloseUsage) +} - 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{}) {