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

Function submitPR

pkg/cmd/pr/create/create.go:1096–1157  ·  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

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