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

Function submitPR

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

1093}
1094
1095func submitPR(opts CreateOptions, ctx CreateContext, state shared.IssueMetadataState, projectV1Support gh.ProjectsV1Support, uploader *attachments.Uploader) error {
1096 client := ctx.Client
1097
1098 params := map[string]any{
1099 "title": state.Title,
1100 "body": state.Body,
1101 "draft": state.Draft,
1102 "baseRefName": ctx.PRRefs.BaseRef(),
1103 "headRefName": ctx.PRRefs.QualifiedHeadRef(),
1104 "maintainerCanModify": opts.MaintainerCanModify,
1105 }
1106
1107 if params["title"] == "" {
1108 return errors.New("pull request title must not be blank")
1109 }
1110
1111 err := shared.AddMetadataToIssueParams(client, ctx.PRRefs.BaseRepo(), params, &state, projectV1Support)
1112 if err != nil {
1113 return err
1114 }
1115
1116 if opts.DryRun {
1117 if opts.IO.IsStdoutTTY() {
1118 return renderPullRequestTTY(opts.IO, params, &state)
1119 } else {
1120 return renderPullRequestPlain(opts.IO.Out, params, &state)
1121 }
1122 }
1123
1124 var uploadErr error
1125 if uploader != nil {
1126 body, uploadResult, err := uploader.UploadAndAttach(context.Background(), state.Body, opts.BodyDir, opts.Assets)
1127 opts.AttachEvent.RecordOperations(uploadResult)
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 && uploadResult.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

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