MCPcopy
hub / github.com/cli/cli / closeRun

Function closeRun

pkg/cmd/pr/close/close.go:65–164  ·  view source on GitHub ↗
(opts *CloseOptions)

Source from the content-addressed store, hash-verified

63}
64
65func closeRun(opts *CloseOptions) error {
66 cs := opts.IO.ColorScheme()
67
68 findOptions := shared.FindOptions{
69 Selector: opts.SelectorArg,
70 Fields: []string{"state", "number", "title", "isCrossRepository", "headRefName"},
71 }
72 pr, baseRepo, err := opts.Finder.Find(findOptions)
73 if err != nil {
74 return err
75 }
76
77 if pr.State == "MERGED" {
78 fmt.Fprintf(opts.IO.ErrOut, "%s Pull request %s#%d (%s) can't be closed because it was already merged\n", cs.FailureIcon(), ghrepo.FullName(baseRepo), pr.Number, pr.Title)
79 return cmdutil.SilentError
80 } else if !pr.IsOpen() {
81 fmt.Fprintf(opts.IO.ErrOut, "%s Pull request %s#%d (%s) is already closed\n", cs.WarningIcon(), ghrepo.FullName(baseRepo), pr.Number, pr.Title)
82 return nil
83 }
84
85 httpClient, err := opts.HttpClient()
86 if err != nil {
87 return err
88 }
89
90 if opts.Comment != "" {
91 commentOpts := &shared.CommentableOptions{
92 Body: opts.Comment,
93 HttpClient: opts.HttpClient,
94 InputType: shared.InputTypeInline,
95 Quiet: true,
96 RetrieveCommentable: func() (shared.Commentable, ghrepo.Interface, error) {
97 return pr, baseRepo, nil
98 },
99 }
100 err := shared.CommentableRun(commentOpts)
101 if err != nil {
102 return err
103 }
104 }
105
106 err = api.PullRequestClose(httpClient, baseRepo, pr.ID)
107 if err != nil {
108 return fmt.Errorf("API call failed: %w", err)
109 }
110
111 fmt.Fprintf(opts.IO.ErrOut, "%s Closed pull request %s#%d (%s)\n", cs.SuccessIconWithColor(cs.Red), ghrepo.FullName(baseRepo), pr.Number, pr.Title)
112
113 if opts.DeleteBranch {
114 ctx := context.Background()
115 branchSwitchString := ""
116 apiClient := api.NewClientFromHTTP(httpClient)
117 localBranchExists := opts.GitClient.HasLocalBranch(ctx, pr.HeadRefName)
118
119 if opts.DeleteLocalBranch {
120 if localBranchExists {
121 currentBranch, err := opts.Branch()
122 if err != nil {

Callers 1

NewCmdCloseFunction · 0.70

Calls 15

FullNameFunction · 0.92
CommentableRunFunction · 0.92
PullRequestCloseFunction · 0.92
NewClientFromHTTPFunction · 0.92
RepoDefaultBranchFunction · 0.92
BranchDeleteRemoteFunction · 0.92
ColorSchemeMethod · 0.80
FailureIconMethod · 0.80
IsOpenMethod · 0.80
WarningIconMethod · 0.80
SuccessIconWithColorMethod · 0.80
DeleteLocalBranchMethod · 0.80

Tested by

no test coverage detected