func BackportToRelease()

in tools/eksDistroBuildToolingOpsTools/pkg/eksGoRelease/createPatch.go [23:90]


func BackportToRelease(ctx context.Context, r *Release, dryrun bool, cve, commit, email, user string) error {
	// Setup Git Clients
	token, err := github.GetGithubToken()
	if err != nil {
		logger.V(4).Error(err, "no github token found")
		return fmt.Errorf("getting Github PAT from environment at variable %s: %v", github.PersonalAccessTokenEnvVar, err)
	}

	ghUser := github.NewGitHubUser(user, email, token)
	// Creating git client in memory and clone 'eks-distro-build-tooling
	forkUrl := fmt.Sprintf(constants.EksGoRepoUrl, ghUser.User())
	gClient := git.NewClient(git.WithInMemoryFilesystem(), git.WithRepositoryUrl(forkUrl), git.WithAuth(&http.BasicAuth{Username: ghUser.User(), Password: ghUser.Token()}))
	if err := gClient.Clone(ctx); err != nil {
		logger.Error(err, "Cloning repo", "user", ghUser.User())
		return err
	}

	// Increment Release
	if err := bumpRelease(gClient, r); err != nil {
		logger.Error(err, "increment release")
		return err
	}

	// Create new branch
	commitBranch := r.EksGoReleaseVersion()
	if err := gClient.Branch(commitBranch); err != nil {
		logger.Error(err, "git branch", "branch name", r.EksGoReleaseVersion(), "repo", forkUrl, "client", gClient)
		return err
	}

	// Update files for new patch versions of golang
	if err := updateVersionReadme(gClient, r); err != nil {
		logger.Error(err, "Update Readme")
		return err
	}

	if err := updateGitTag(gClient, r); err != nil {
		logger.Error(err, "Update GitTag")
		return err
	}

	/* -----
	 * Begin applying previous patches and attempting to cherry-pick the new commit. Any errors from here on out should result in cutting a pr without a new patch,
	 * but shouldn't fail the automation because the patch can be generated manually
	----- */
	prSubject := fmt.Sprintf(backportPRSubjectFmt, r.EksGoReleaseVersion(), cve)
	commitMsg := fmt.Sprintf(backportCommitMsgFmt, r.EksGoReleaseVersion())
	golangClient := git.NewClient(git.WithInMemoryFilesystem(), git.WithRepositoryUrl(constants.GoRepoUrl), git.WithAuth(&http.BasicAuth{Username: user, Password: token}))
	if err := createPatchFile(ctx, r, gClient, golangClient, commit); err != nil {
		logger.V(3).Info("Generate Patch failed, continuing with PR")
		// no longer update gospec with patch file since no patch was created
		if !dryrun {
			prFailureDescription := fmt.Sprintf(backportPRDescriptionFailureFmt, cve, r.EksGoReleaseVersion(), commit)
			if err := createReleasePR(ctx, dryrun, r, ghUser, gClient, prSubject, prFailureDescription, commitMsg, commitBranch); err != nil {
				logger.Error(err, "Create Release PR")
				return err
			}
		}
	}

	if !dryrun {
		prSuccessDescription := fmt.Sprintf(backportPRDescriptionSuccessFmt, cve, commit, r.EksGoReleaseVersion())
		if err := createReleasePR(ctx, dryrun, r, ghUser, gClient, prSubject, prSuccessDescription, commitMsg, commitBranch); err != nil {
			logger.Error(err, "Create Release PR")
		}
	}
	return nil
}