diff --git a/api/queries_pr.go b/api/queries_pr.go index 0b2fd378a..6af709947 100644 --- a/api/queries_pr.go +++ b/api/queries_pr.go @@ -1,11 +1,13 @@ package api import ( + "context" "fmt" "strings" "time" "github.com/cli/cli/internal/ghrepo" + "github.com/shurcooL/githubv4" ) type PullRequestsPayload struct { @@ -20,9 +22,11 @@ type PullRequestAndTotalCount struct { } type PullRequest struct { + ID string Number int Title string State string + Closed bool URL string BaseRefName string HeadRefName string @@ -755,6 +759,25 @@ loop: return &res, nil } +func PullRequestClose(client *Client, repo ghrepo.Interface, pr PullRequest) error { + var mutation struct { + ClosePullRequest struct { + PullRequest struct { + ID githubv4.ID + } + } `graphql:"closePullRequest(input: $input)"` + } + + input := githubv4.ClosePullRequestInput{ + PullRequestID: pr.ID, + } + + v4 := githubv4.NewClient(client.http) + err := v4.Mutate(context.Background(), &mutation, input, nil) + + return err +} + func min(a, b int) int { if a < b { return a diff --git a/command/pr.go b/command/pr.go index 009548061..49e1f3c9a 100644 --- a/command/pr.go +++ b/command/pr.go @@ -21,16 +21,17 @@ func init() { RootCmd.AddCommand(prCmd) prCmd.AddCommand(prCheckoutCmd) prCmd.AddCommand(prCreateCmd) - prCmd.AddCommand(prListCmd) prCmd.AddCommand(prStatusCmd) - prCmd.AddCommand(prViewCmd) + prCmd.AddCommand(prCloseCmd) + prCmd.AddCommand(prListCmd) prListCmd.Flags().IntP("limit", "L", 30, "Maximum number of items to fetch") prListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|merged|all}") prListCmd.Flags().StringP("base", "B", "", "Filter by base branch") prListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label") prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee") + prCmd.AddCommand(prViewCmd) prViewCmd.Flags().BoolP("web", "w", false, "Open a pull request in the browser") } @@ -65,6 +66,11 @@ is displayed. With '--web', open the pull request in a web browser instead.`, RunE: prView, } +var prCloseCmd = &cobra.Command{ + Use: "close [{ | }]", + Short: "Close a pull request", + RunE: prClose, +} func prStatus(cmd *cobra.Command, args []string) error { ctx := contextForCommand(cmd) @@ -328,6 +334,38 @@ func prView(cmd *cobra.Command, args []string) error { } } +func prClose(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.Closed { + fmt.Fprintf(colorableErr(cmd), "%s Pull request #%d is already closed\n", utils.Yellow("!"), pr.Number) + return nil + } + + err = api.PullRequestClose(apiClient, baseRepo, *pr) + if err != nil { + return fmt.Errorf("API call failed:%w", err) + } + + fmt.Fprintf(colorableErr(cmd), "%s Closed pull request #%d\n", utils.Red("✔"), pr.Number) + + return nil +} + func printPrPreview(out io.Writer, pr *api.PullRequest) error { // Header (Title and State) fmt.Fprintln(out, utils.Bold(pr.Title))