From f7162d7591a28fdb2d0bec6552b48d0b4ef4f6a9 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 29 Apr 2020 12:29:41 -0700 Subject: [PATCH] Implement issue reopen --- command/issue.go | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/command/issue.go b/command/issue.go index d1b57485e..331e89443 100644 --- a/command/issue.go +++ b/command/issue.go @@ -41,6 +41,7 @@ func init() { issueViewCmd.Flags().BoolP("web", "w", false, "Open an issue in the browser") issueCmd.AddCommand(issueCloseCmd) + issueCmd.AddCommand(issueReopenCmd) } var issueCmd = &cobra.Command{ @@ -82,11 +83,17 @@ With '--web', open the issue in a web browser instead.`, RunE: issueView, } var issueCloseCmd = &cobra.Command{ - Use: "close ", - Short: "close and issue issues", + Use: "close ", + Short: "close issue", Args: cobra.ExactArgs(1), RunE: issueClose, } +var issueReopenCmd = &cobra.Command{ + Use: "reopen ", + Short: "reopen issue", + Args: cobra.ExactArgs(1), + RunE: issueReopen, +} func issueList(cmd *cobra.Command, args []string) error { ctx := contextForCommand(cmd) @@ -559,7 +566,41 @@ func issueClose(cmd *cobra.Command, args []string) error { fmt.Fprintf(colorableErr(cmd), "%s Closed issue #%d\n", utils.Red("✔"), issue.Number) return nil +} +func issueReopen(cmd *cobra.Command, args []string) error { + ctx := contextForCommand(cmd) + apiClient, err := apiClientForContext(ctx) + if err != nil { + return err + } + + baseRepo, err := determineBaseRepo(cmd, ctx) + if err != nil { + return err + } + + issue, err := issueFromArg(apiClient, baseRepo, args[0]) + var idErr *api.IssuesDisabledError + if errors.As(err, &idErr) { + return fmt.Errorf("issues disabled for %s", ghrepo.FullName(baseRepo)) + } else if err != nil { + return fmt.Errorf("failed to find issue #%d: %w", issue.Number, err) + } + + if !issue.Closed { + fmt.Fprintf(colorableErr(cmd), "%s Issue #%d was already open\n", utils.Yellow("!"), issue.Number) + return nil + } + + err = api.IssueClose(apiClient, baseRepo, *issue) + if err != nil { + return fmt.Errorf("API call failed:%w", err) + } + + fmt.Fprintf(colorableErr(cmd), "%s Reopened issue #%d\n", utils.Red("✔"), issue.Number) + + return nil } func displayURL(urlStr string) string {