func()

in tools/eksDistroBuildToolingOpsTools/pkg/prManager/prManager.go [141:196]


func (p *PrCreator) createPR(ctx context.Context, opts *CreatePrOpts) (pr *gogithub.PullRequest, err error) {
	if opts.PrSubject == "" {
		return nil, fmt.Errorf("PR subject is required")
	}

	if p.prRepoOwner != "" && p.prRepoOwner != p.sourceOwner {
		opts.CommitBranch = fmt.Sprintf("%s:%s", p.sourceOwner, opts.CommitBranch)
	} else {
		p.prRepoOwner = p.sourceOwner
	}

	if p.prRepo == "" {
		p.prRepo = p.sourceRepo
	}

	newPR := &gogithub.NewPullRequest{
		Title:               &opts.PrSubject,
		Head:                &opts.CommitBranch,
		Base:                &opts.PrBranch,
		Body:                &opts.PrDescription,
		MaintainerCanModify: gogithub.Bool(true),
	}

	var pullRequest *gogithub.PullRequest
	var resp *gogithub.Response
	err = p.retrier.Retry(func() error {
		pullRequest, resp, err = p.client.PullRequests.Create(ctx, p.prRepoOwner, p.prRepo, newPR)
		if resp.StatusCode == github.SecondaryRateLimitStatusCode {
			if strings.Contains(err.Error(), github.SecondaryRateLimitResponse) {
				return fmt.Errorf("rate limited while attempting to create github pull request: %v", err)
			}
		}
		if err != nil && strings.Contains(err.Error(), github.PullRequestAlreadyExistsForBranchError) {
			// there can only be one PR per branch; if there's already an existing PR for the branch, we won't create one, but continue
			logger.V(1).Info("A Pull Request already exists for the given branch", "branch", opts.CommitBranch)
			getPrOpts := &GetPrOpts{
				CommitBranch: opts.CommitBranch,
				BaseBranch:   "main",
			}
			pullRequest, err = p.getPr(ctx, getPrOpts)
			if err != nil {
				return err
			}
			return nil
		}
		if err != nil {
			return fmt.Errorf("creating Github pull request: %v; resp: %v", err, resp)
		}
		logger.V(1).Info("Github Pull Request Created", "Pull Request URL", pullRequest.GetHTMLURL())
		return nil
	})
	if err != nil {
		return nil, fmt.Errorf("creating github pull request: %v", err)
	}
	return pullRequest, nil
}