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

Function CreatePullRequest

api/queries_pr.go:490–615  ·  view source on GitHub ↗

CreatePullRequest creates a pull request in a GitHub repository

(client *Client, repo *Repository, params map[string]any)

Source from the content-addressed store, hash-verified

488
489// CreatePullRequest creates a pull request in a GitHub repository
490func CreatePullRequest(client *Client, repo *Repository, params map[string]any) (*PullRequest, error) {
491 query := `
492 mutation PullRequestCreate($input: CreatePullRequestInput!) {
493 createPullRequest(input: $input) {
494 pullRequest {
495 id
496 url
497 }
498 }
499 }`
500
501 inputParams := map[string]any{
502 "repositoryId": repo.ID,
503 }
504 for key, val := range params {
505 switch key {
506 case "title", "body", "draft", "baseRefName", "headRefName", "maintainerCanModify":
507 inputParams[key] = val
508 }
509 }
510 variables := map[string]any{
511 "input": inputParams,
512 }
513
514 result := struct {
515 CreatePullRequest struct {
516 PullRequest PullRequest
517 }
518 }{}
519
520 err := client.GraphQL(repo.RepoHost(), query, variables, &result)
521 if err != nil {
522 return nil, err
523 }
524 pr := &result.CreatePullRequest.PullRequest
525
526 // metadata parameters aren't currently available in `createPullRequest`,
527 // but they are in `updatePullRequest`
528 updateParams := make(map[string]any)
529 for key, val := range params {
530 switch key {
531 case "assigneeIds", "labelIds", "projectIds", "milestoneId":
532 if !isBlank(val) {
533 updateParams[key] = val
534 }
535 }
536 }
537 if len(updateParams) > 0 {
538 updateQuery := `
539 mutation PullRequestCreateMetadata($input: UpdatePullRequestInput!) {
540 updatePullRequest(input: $input) { clientMutationId }
541 }`
542 updateParams["pullRequestId"] = pr.ID
543 variables := map[string]any{
544 "input": updateParams,
545 }
546 err := client.GraphQL(repo.RepoHost(), updateQuery, variables, &result)
547 if err != nil {

Callers 1

submitPRFunction · 0.92

Calls 6

isBlankFunction · 0.85
RequestReviewsByLoginFunction · 0.85
UpdateProjectV2ItemsFunction · 0.85
GraphQLMethod · 0.80
RepoHostMethod · 0.65

Tested by

no test coverage detected