func CreatePullRequest()

in tools/version-tracker/pkg/github/github.go [303:343]


func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOwner, baseBranch, headRepoOwner, headBranch, currentRevision, latestRevision string) (*github.PullRequest, error) {
	var pullRequest *github.PullRequest

	// Check if there is already a pull request from the head branch to the base branch.
	pullRequests, _, err := client.PullRequests.List(context.Background(), baseRepoOwner, constants.BuildToolingRepoName, &github.PullRequestListOptions{
		Base: baseBranch,
		Head: fmt.Sprintf("%s:%s", headRepoOwner, headBranch),
	})
	if err != nil {
		return nil, fmt.Errorf("listing pull requests from %s:%s -> %s:%s: %v", headRepoOwner, headBranch, baseRepoOwner, baseBranch, err)
	}

	if len(pullRequests) > 0 {
		pullRequest = pullRequests[0]
		logger.Info(fmt.Sprintf("A pull request already exists for %s:%s", headRepoOwner, headBranch), "Pull request", *pullRequest.HTMLURL)

		pullRequest.Body = github.String(body)
		pullRequest, _, err = client.PullRequests.Edit(context.Background(), baseRepoOwner, constants.BuildToolingRepoName, *pullRequest.Number, pullRequest)
		if err != nil {
			return nil, fmt.Errorf("editing existing pull request [%s]: %v", *pullRequest.HTMLURL, err)
		}
	} else {
		logger.V(6).Info(fmt.Sprintf("Creating pull request with updated versions for [%s/%s] repository", org, repo))

		newPR := &github.NewPullRequest{
			Title:               github.String(title),
			Head:                github.String(fmt.Sprintf("%s:%s", headRepoOwner, headBranch)),
			Base:                github.String(baseBranch),
			Body:                github.String(body),
			MaintainerCanModify: github.Bool(true),
		}
		pullRequest, _, err = client.PullRequests.Create(context.Background(), baseRepoOwner, constants.BuildToolingRepoName, newPR)
		if err != nil {
			return nil, fmt.Errorf("creating pull request with updated versions from %s to %s: %v", headBranch, baseBranch, err)
		}

		logger.Info(fmt.Sprintf("Created pull request: %s", *pullRequest.HTMLURL))
	}

	return pullRequest, nil
}