diff --git a/command/pr.go b/command/pr.go index 126d3db01..313bb1f4d 100644 --- a/command/pr.go +++ b/command/pr.go @@ -24,6 +24,7 @@ func init() { prCmd.AddCommand(prStatusCmd) prCmd.AddCommand(prCloseCmd) prCmd.AddCommand(prReopenCmd) + prCmd.AddCommand(prMergeCmd) prCmd.AddCommand(prListCmd) prListCmd.Flags().IntP("limit", "L", 30, "Maximum number of items to fetch") @@ -80,6 +81,13 @@ var prReopenCmd = &cobra.Command{ RunE: prReopen, } +var prMergeCmd = &cobra.Command{ + Use: "merge ", + Short: "Merge a pull request", + Args: cobra.ExactArgs(1), + RunE: prMerge, +} + func prStatus(cmd *cobra.Command, args []string) error { ctx := contextForCommand(cmd) apiClient, err := apiClientForContext(ctx) @@ -420,6 +428,38 @@ func prReopen(cmd *cobra.Command, args []string) error { return nil } +func prMerge(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 + } + + pr, err := prFromArg(apiClient, baseRepo, args[0]) + if err != nil { + return err + } + + if pr.State == "MERGED" { + err := fmt.Errorf("%s Pull request #%d was already merged", utils.Red("!"), pr.Number) + return err + } + + err = api.PullRequestClose(apiClient, baseRepo, pr) + if err != nil { + return fmt.Errorf("API call failed: %w", err) + } + + fmt.Fprintf(colorableErr(cmd), "%s Merged pull request #%d\n", utils.Green("✔"), pr.Number) + + return nil +} + func printPrPreview(out io.Writer, pr *api.PullRequest) error { // Header (Title and State) fmt.Fprintln(out, utils.Bold(pr.Title))