func()

in custom-targets/git-ops/git-deployer/deploy.go [222:281]


func (d *deployer) handleDestinationBranch(ctx context.Context, gitRepo *gitRepository, secret string) error {
	// If no destination branch is provided then there is no need to open a pull request.
	if len(d.params.gitDestinationBranch) == 0 {
		return nil
	}

	title := d.params.gitPullRequestTitle
	if len(title) == 0 {
		title = fmt.Sprintf("Cloud Deploy: Release %s, Rollout %s", d.req.Release, d.req.Rollout)
	}
	body := d.params.gitPullRequestBody
	if len(body) == 0 {
		body = fmt.Sprintf("Project: %s\nLocation: %s\nDelivery Pipeline: %s\nTarget: %s\nRelease: %s\nRollout: %s",
			d.req.Project,
			d.req.Location,
			d.req.Pipeline,
			d.req.Target,
			d.req.Release,
			d.req.Rollout,
		)
	}

	gitProvider, err := provider.CreateProvider(gitRepo.hostname, gitRepo.repoName, gitRepo.owner, secret)
	if err != nil {
		return fmt.Errorf("unable to create git provider: %v", err)
	}
	fmt.Printf("Opening pull request from %s to %s\n", d.params.gitSourceBranch, d.params.gitDestinationBranch)
	pr, err := gitProvider.OpenPullRequest(d.params.gitSourceBranch, d.params.gitDestinationBranch, title, body)
	if err != nil {
		return fmt.Errorf("unable to open pull request from %s to %s: %v", d.params.gitSourceBranch, d.params.gitDestinationBranch, err)
	}

	if !d.params.enablePullRequestMerge {
		return nil
	}
	fmt.Println("Merging the pull request")
	mr, err := gitProvider.MergePullRequest(pr.Number)
	if err != nil {
		return fmt.Errorf("unable to merge pull request %d: %v", pr.Number, err)
	}

	if !d.params.enableArgoSyncPoll {
		return nil
	}
	fmt.Printf("Argo sync polling is enabled, setting up cluster credentials for %s\n", d.params.gkeCluster)
	if _, err := gcloudClusterCredentials(d.params.gkeCluster); err != nil {
		return fmt.Errorf("unable to set up cluster credentials: %v", err)
	}
	fmt.Printf("Checking for the existence of the Argo Application %s in namespace %s\n", d.params.argoApp, d.params.argoNamespace)
	if _, err := verifyResourceExists(argoCRType, d.params.argoApp, d.params.argoNamespace); err != nil {
		return fmt.Errorf("argo application custom resource not found: %v", err)
	}

	fmt.Println("Polling Argo Application until it's synced with the merged changes")
	if err := pollSyncStatus(d.params.argoApp, d.params.argoNamespace, mr.Sha, d.params.argoSyncTimeout); err != nil {
		return fmt.Errorf("unable to verify argo application is synced: %v", err)
	}
	fmt.Printf("Argo Application synced with the merged changes\n")
	return nil
}