From dbda4d01190f8de745255db661d910fba0d95498 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 22 Oct 2017 11:12:06 +0100 Subject: [PATCH 1/4] add !github reopen cmd Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../go-neb/services/github/github.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) 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 89398bb..f981401 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 @@ -365,6 +365,39 @@ func (s *Service) cmdGithubClose(roomID, userID string, args []string) (interfac return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Closed issue: %s", *issueComment.HTMLURL)}, nil } +const cmdGithubReopenUsage = `!github close [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 + } + + 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) + } + + return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Reopened issue: %s", *issueComment.HTMLURL)}, nil +} + func (s *Service) getIssueDetailsFor(input, roomID, usage string) (owner, repo string, issueNum int, resp interface{}) { // We expect the input to look like: // "[owner/repo]#issue" @@ -472,6 +505,12 @@ func (s *Service) Commands(cli *gomatrix.Client) []types.Command { return s.cmdGithubClose(roomID, userID, args) }, }, + types.Command{ + Path: []string{"github", "reopen"}, + Command: func(roomID, userID string, args []string) (interface{}, error) { + return s.cmdGithubReopen(roomID, userID, args) + }, + }, types.Command{ Path: []string{"github", "help"}, Command: func(roomID, userID string, args []string) (interface{}, error) { @@ -483,6 +522,7 @@ func (s *Service) Commands(cli *gomatrix.Client) []types.Command { cmdGithubCommentUsage, cmdGithubAssignUsage, cmdGithubCloseUsage, + cmdGithubReopenUsage, }, "\n"), }, nil }, From f9b708a31f16f66c851bb44531c43ead02735454 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 22 Oct 2017 11:31:05 +0100 Subject: [PATCH 2/4] fix help message Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/github.com/matrix-org/go-neb/services/github/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f981401..12349de 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 @@ -365,7 +365,7 @@ func (s *Service) cmdGithubClose(roomID, userID string, args []string) (interfac return gomatrix.TextMessage{"m.notice", fmt.Sprintf("Closed issue: %s", *issueComment.HTMLURL)}, nil } -const cmdGithubReopenUsage = `!github close [owner/repo]#issue` +const cmdGithubReopenUsage = `!github reopen [owner/repo]#issue` func (s *Service) cmdGithubReopen(roomID, userID string, args []string) (interface{}, error) { cli, resp, err := s.requireGithubClientFor(userID) 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 3/4] 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{}) { From aa00ab2322ca954a6e9377450fecfefc18648f01 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Sun, 22 Oct 2017 11:36:12 +0100 Subject: [PATCH 4/4] fix typos Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/github.com/matrix-org/go-neb/services/github/github.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 87b69d6..eab6f55 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 @@ -371,7 +371,7 @@ func (s *Service) cmdGithubClose(roomID, userID string, args []string) (interfac const cmdGithubReopenUsage = `!github reopen [owner/repo]#issue` func (s *Service) cmdGithubReopen(roomID, userID string, args []string) (interface{}, error) { - return s.githubIssueCloseReopen(roomID, userID, args, "closed", "close", cmdGithubCloseUsage) + return s.githubIssueCloseReopen(roomID, userID, args, "open", "open", cmdGithubCloseUsage) } func (s *Service) getIssueDetailsFor(input, roomID, usage string) (owner, repo string, issueNum int, resp interface{}) {