Skip to content

Commit f9a55f4

Browse files
committed
Add close pull request code
1 parent 66abe9e commit f9a55f4

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

api/queries_pr.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package api
22

33
import (
4+
"context"
45
"fmt"
56
"strings"
67
"time"
78

89
"github.com/cli/cli/internal/ghrepo"
10+
"github.com/shurcooL/githubv4"
911
)
1012

1113
type PullRequestsPayload struct {
@@ -20,9 +22,11 @@ type PullRequestAndTotalCount struct {
2022
}
2123

2224
type PullRequest struct {
25+
ID string
2326
Number int
2427
Title string
2528
State string
29+
Closed bool
2630
URL string
2731
BaseRefName string
2832
HeadRefName string
@@ -755,6 +759,25 @@ loop:
755759
return &res, nil
756760
}
757761

762+
func PullRequestClose(client *Client, repo ghrepo.Interface, pr PullRequest) error {
763+
var mutation struct {
764+
ClosePullRequest struct {
765+
PullRequest struct {
766+
ID githubv4.ID
767+
}
768+
} `graphql:"closePullRequest(input: $input)"`
769+
}
770+
771+
input := githubv4.ClosePullRequestInput{
772+
PullRequestID: pr.ID,
773+
}
774+
775+
v4 := githubv4.NewClient(client.http)
776+
err := v4.Mutate(context.Background(), &mutation, input, nil)
777+
778+
return err
779+
}
780+
758781
func min(a, b int) int {
759782
if a < b {
760783
return a

command/pr.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ func init() {
2121
RootCmd.AddCommand(prCmd)
2222
prCmd.AddCommand(prCheckoutCmd)
2323
prCmd.AddCommand(prCreateCmd)
24-
prCmd.AddCommand(prListCmd)
2524
prCmd.AddCommand(prStatusCmd)
26-
prCmd.AddCommand(prViewCmd)
25+
prCmd.AddCommand(prCloseCmd)
2726

27+
prCmd.AddCommand(prListCmd)
2828
prListCmd.Flags().IntP("limit", "L", 30, "Maximum number of items to fetch")
2929
prListCmd.Flags().StringP("state", "s", "open", "Filter by state: {open|closed|merged|all}")
3030
prListCmd.Flags().StringP("base", "B", "", "Filter by base branch")
3131
prListCmd.Flags().StringSliceP("label", "l", nil, "Filter by label")
3232
prListCmd.Flags().StringP("assignee", "a", "", "Filter by assignee")
3333

34+
prCmd.AddCommand(prViewCmd)
3435
prViewCmd.Flags().BoolP("web", "w", false, "Open a pull request in the browser")
3536
}
3637

@@ -65,6 +66,11 @@ is displayed.
6566
With '--web', open the pull request in a web browser instead.`,
6667
RunE: prView,
6768
}
69+
var prCloseCmd = &cobra.Command{
70+
Use: "close [{<number> | <url>}]",
71+
Short: "Close a pull request",
72+
RunE: prClose,
73+
}
6874

6975
func prStatus(cmd *cobra.Command, args []string) error {
7076
ctx := contextForCommand(cmd)
@@ -328,6 +334,38 @@ func prView(cmd *cobra.Command, args []string) error {
328334
}
329335
}
330336

337+
func prClose(cmd *cobra.Command, args []string) error {
338+
ctx := contextForCommand(cmd)
339+
apiClient, err := apiClientForContext(ctx)
340+
if err != nil {
341+
return err
342+
}
343+
344+
baseRepo, err := determineBaseRepo(cmd, ctx)
345+
if err != nil {
346+
return err
347+
}
348+
349+
pr, err := prFromArg(apiClient, baseRepo, args[0])
350+
if err != nil {
351+
return err
352+
}
353+
354+
if pr.Closed {
355+
fmt.Fprintf(colorableErr(cmd), "%s Pull request #%d is already closed\n", utils.Yellow("!"), pr.Number)
356+
return nil
357+
}
358+
359+
err = api.PullRequestClose(apiClient, baseRepo, *pr)
360+
if err != nil {
361+
return fmt.Errorf("API call failed:%w", err)
362+
}
363+
364+
fmt.Fprintf(colorableErr(cmd), "%s Closed pull request #%d\n", utils.Red("✔"), pr.Number)
365+
366+
return nil
367+
}
368+
331369
func printPrPreview(out io.Writer, pr *api.PullRequest) error {
332370
// Header (Title and State)
333371
fmt.Fprintln(out, utils.Bold(pr.Title))

0 commit comments

Comments
 (0)