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

Function submitPR

pkg/cmd/pr/create/create.go:1096–1161  ·  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, uploadResult, err := uploader.UploadAndAttach(context.Background(), state.Body, opts.BodyDir, opts.Assets)
1128 opts.AttachEvent.RecordOperations(uploadResult)
1129 // With nothing uploaded, a body that lost the files it was written
1130 // around is not what the caller asked to create. The branch is already
1131 // pushed by now, so the message says what was not created rather than
1132 // claiming the run had no effect.
1133 if err != nil && uploadResult.Uploaded == 0 {
1134 return fmt.Errorf("%w\nno pull request was created", err)
1135 }
1136 uploadErr = err
1137 params["body"] = body
1138 }
1139
1140 opts.IO.StartProgressIndicator()
1141 pr, err := api.CreatePullRequest(client, ctx.PRRefs.BaseRepo(), params)
1142 opts.IO.StopProgressIndicator()
1143 if pr != nil {
1144 fmt.Fprintln(opts.IO.Out, pr.URL)
1145 }
1146 if err != nil {
1147 if pr != nil {
1148 err = fmt.Errorf("pull request update failed: %w", err)
1149 } else {
1150 err = fmt.Errorf("pull request create failed: %w", err)
1151 }
1152 // A failed upload and a failed create have different remedies, and a
1153 // failed create leaves the uploaded assets referenced by nothing, so

Callers 1

createRunFunction · 0.85

Calls 14

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

Tested by

no test coverage detected