MCPcopy Create free account
hub / github.com/cli/cli / submitPR

Function submitPR

pkg/cmd/pr/create/create.go:1099–1160  ·  view source on GitHub ↗
(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataState, projectV1Support gh.ProjectsV1Support, uploader *attachments.Uploader)

Source from the content-addressed store, hash-verified

1097}
1098
1099func submitPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataState, projectV1Support gh.ProjectsV1Support, uploader *attachments.Uploader) error {
1100 client := ctx.Client
1101
1102 params := map[string]any{
1103 "title": state.Title,
1104 "body": state.Body,
1105 "draft": state.Draft,
1106 "baseRefName": ctx.PRRefs.BaseRef(),
1107 "headRefName": ctx.PRRefs.QualifiedHeadRef(),
1108 "maintainerCanModify": opts.MaintainerCanModify,
1109 }
1110
1111 if params["title"] == "" {
1112 return errors.New("pull request title must not be blank")
1113 }
1114
1115 err := shared.AddMetadataToIssueParams(client, ctx.PRRefs.BaseRepo(), params, &state, projectV1Support)
1116 if err != nil {
1117 return err
1118 }
1119
1120 if opts.DryRun {
1121 if opts.IO.IsStdoutTTY() {
1122 return renderPullRequestTTY(opts.IO, params, &state)
1123 } else {
1124 return renderPullRequestPlain(opts.IO.Out, params, &state)
1125 }
1126 }
1127
1128 var uploadErr error
1129 if uploader != nil {
1130 body, uploaded, err := uploader.UploadAndAttach(context.Background(), state.Body, opts.Assets)
1131 // With nothing uploaded, a body that lost the files it was written
1132 // around is not what the caller asked to create. The branch is already
1133 // pushed by now, so the message says what was not created rather than
1134 // claiming the run had no effect.
1135 if err != nil && uploaded == 0 {
1136 return fmt.Errorf("%w\nno pull request was created", err)
1137 }
1138 uploadErr = err
1139 params["body"] = body
1140 }
1141
1142 opts.IO.StartProgressIndicator()
1143 pr, err := api.CreatePullRequest(client, ctx.PRRefs.BaseRepo(), params)
1144 opts.IO.StopProgressIndicator()
1145 if pr != nil {
1146 fmt.Fprintln(opts.IO.Out, pr.URL)
1147 }
1148 if err != nil {
1149 if pr != nil {
1150 err = fmt.Errorf("pull request update failed: %w", err)
1151 } else {
1152 err = fmt.Errorf("pull request create failed: %w", err)
1153 }
1154 // A failed upload and a failed create have different remedies, and a
1155 // failed create leaves the uploaded assets referenced by nothing, so
1156 // that error reads first.

Callers 1

createRunFunction · 0.85

Calls 13

AddMetadataToIssueParamsFunction · 0.92
CreatePullRequestFunction · 0.92
renderPullRequestTTYFunction · 0.85
renderPullRequestPlainFunction · 0.85
IsStdoutTTYMethod · 0.80
UploadAndAttachMethod · 0.80
JoinMethod · 0.80
BaseRefMethod · 0.65
QualifiedHeadRefMethod · 0.65
BaseRepoMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected