From 2530789694f74dd0a97278a502571395c2c9c590 Mon Sep 17 00:00:00 2001 From: Abhay Krishna Arunachalam Date: Wed, 27 Mar 2024 18:13:13 -0700 Subject: [PATCH] Fix condition for patches warning comment on upgrade PRs --- .../pkg/commands/upgrade/upgrade.go | 6 ++--- tools/version-tracker/pkg/git/git.go | 9 ++++--- tools/version-tracker/pkg/github/github.go | 25 +++++++++++-------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tools/version-tracker/pkg/commands/upgrade/upgrade.go b/tools/version-tracker/pkg/commands/upgrade/upgrade.go index 9a45099b0c..5787d35748 100644 --- a/tools/version-tracker/pkg/commands/upgrade/upgrade.go +++ b/tools/version-tracker/pkg/commands/upgrade/upgrade.go @@ -32,7 +32,7 @@ import ( // Run contains the business logic to execute the `upgrade` subcommand. func Run(upgradeOptions *types.UpgradeOptions) error { var currentRevision, latestRevision string - var patchApplySucceeded bool + var patchApplySucceeded, addPatchWarningComment bool var totalPatchCount int var updatedFiles []string patchesWarningComment := constants.PatchesCommentBody @@ -179,7 +179,6 @@ func Run(upgradeOptions *types.UpgradeOptions) error { // Upgrade project if latest commit was made after current commit and the semver of the latest revision is // greater than the semver of the current version. if needsUpgrade || slices.Contains(constants.ProjectsWithUnconventionalUpgradeFlows, projectName) { - // Checkout a new branch to keep track of version upgrade chaneges. err = git.Checkout(worktree, headBranchName) if err != nil { @@ -273,6 +272,7 @@ func Run(upgradeOptions *types.UpgradeOptions) error { return fmt.Errorf("applying patches to repository: %v", err) } if !patchApplySucceeded { + addPatchWarningComment = true patchesWarningComment = fmt.Sprintf(constants.PatchesCommentBody, appliedPatchesCount, totalPatchCount, failedPatch, applyFailedFiles) } } @@ -368,7 +368,7 @@ func Run(upgradeOptions *types.UpgradeOptions) error { // Create a pull request from the bramch in the head repository to the target branch in the aws/eks-anywhere-build-tooling repository. logger.Info("Creating pull request with updated files") - err = github.CreatePullRequest(client, projectOrg, projectRepo, commitMessage, pullRequestBody, baseRepoOwner, baseBranchName, headRepoOwner, headBranchName, currentRevision, latestRevision, patchApplySucceeded, patchesWarningComment) + err = github.CreatePullRequest(client, projectOrg, projectRepo, commitMessage, pullRequestBody, baseRepoOwner, baseBranchName, headRepoOwner, headBranchName, currentRevision, latestRevision, addPatchWarningComment, patchesWarningComment) if err != nil { return fmt.Errorf("creating pull request to %s repository: %v", constants.BuildToolingRepoName, err) } diff --git a/tools/version-tracker/pkg/git/git.go b/tools/version-tracker/pkg/git/git.go index 7d47186671..6b1ba40949 100644 --- a/tools/version-tracker/pkg/git/git.go +++ b/tools/version-tracker/pkg/git/git.go @@ -19,7 +19,7 @@ import ( // CloneRepo clones the remote repository to a destination folder and creates a Git remote. func CloneRepo(cloneURL, destination, headRepoOwner string) (*git.Repository, string, error) { - logger.V(6).Info(fmt.Sprintf("Cloning repository [%s] to %s directory\n", cloneURL, destination)) + logger.V(6).Info(fmt.Sprintf("Cloning repository [%s] to %s directory", cloneURL, destination)) progress := io.Discard if logger.Verbosity >= 6 { progress = os.Stdout @@ -30,8 +30,11 @@ func CloneRepo(cloneURL, destination, headRepoOwner string) (*git.Repository, st }) if err != nil { if err == git.ErrRepositoryAlreadyExists { - logger.V(6).Info(fmt.Sprintf("Repo already exists at %s\n", destination)) + logger.V(6).Info(fmt.Sprintf("Repo already exists at %s", destination)) repo, err = git.PlainOpen(destination) + if err != nil { + return nil, "", fmt.Errorf("opening repo from %s directory: %v", destination, err) + } } else { return nil, "", fmt.Errorf("cloning repo %s to %s directory: %v", cloneURL, destination, err) } @@ -75,7 +78,7 @@ func ResetToMain(worktree *git.Worktree, baseRepoHeadCommit string) error { // Checkout checks out the working tree at the given branch, creating a new branch if necessary. func Checkout(worktree *git.Worktree, branch string) error { - logger.V(6).Info(fmt.Sprintf("Checking out branch [%s] in local worktree\n", branch)) + logger.V(6).Info(fmt.Sprintf("Checking out branch [%s] in local worktree", branch)) err := worktree.Checkout(&git.CheckoutOptions{ Branch: plumbing.NewBranchReferenceName(branch), diff --git a/tools/version-tracker/pkg/github/github.go b/tools/version-tracker/pkg/github/github.go index 32d5ac268c..c969f77e5d 100644 --- a/tools/version-tracker/pkg/github/github.go +++ b/tools/version-tracker/pkg/github/github.go @@ -21,7 +21,7 @@ import ( // getReleasesForRepo retrieves the list of releases for the given GitHub repository. func getReleasesForRepo(client *github.Client, org, repo string) ([]*github.RepositoryRelease, error) { - logger.V(6).Info(fmt.Sprintf("Getting releases for [%s/%s] repository\n", org, repo)) + logger.V(6).Info(fmt.Sprintf("Getting releases for [%s/%s] repository", org, repo)) var allReleases []*github.RepositoryRelease listReleasesOptions := &github.ListOptions{ PerPage: constants.GithubPerPage, @@ -48,7 +48,7 @@ func getReleasesForRepo(client *github.Client, org, repo string) ([]*github.Repo // getTagsForRepo retrieves the list of tags for the given GitHub repository. func getTagsForRepo(client *github.Client, org, repo string) ([]*github.RepositoryTag, error) { - logger.V(6).Info(fmt.Sprintf("Getting tags for [%s/%s] repository\n", org, repo)) + logger.V(6).Info(fmt.Sprintf("Getting tags for [%s/%s] repository", org, repo)) var allTags []*github.RepositoryTag listTagOptions := &github.ListOptions{ PerPage: constants.GithubPerPage, @@ -72,7 +72,7 @@ func getTagsForRepo(client *github.Client, org, repo string) ([]*github.Reposito // getCommitsForRepo retrieves the list of commits for the given GitHub repository. func getCommitsForRepo(client *github.Client, org, repo string) ([]*github.RepositoryCommit, error) { - logger.V(6).Info(fmt.Sprintf("Getting commits for [%s/%s] repository\n", org, repo)) + logger.V(6).Info(fmt.Sprintf("Getting commits for [%s/%s] repository", org, repo)) var allCommits []*github.RepositoryCommit listCommitOptions := &github.CommitsListOptions{ ListOptions: github.ListOptions{ @@ -98,7 +98,7 @@ func getCommitsForRepo(client *github.Client, org, repo string) ([]*github.Repos // getCommitDateEpoch gets the Unix epoch time equivalent of a given Github commit's date. func getCommitDateEpoch(client *github.Client, org, repo, commitSHA string) (int64, error) { - logger.V(6).Info(fmt.Sprintf("Getting date for commit %s in [%s/%s] repository\n", commitSHA, org, repo)) + logger.V(6).Info(fmt.Sprintf("Getting date for commit %s in [%s/%s] repository", commitSHA, org, repo)) commit, _, err := client.Repositories.GetCommit(context.Background(), org, repo, commitSHA, nil) if err != nil { @@ -123,7 +123,7 @@ func GetFileContents(client *github.Client, org, repo, filePath, ref string) ([] // GetLatestRevision returns the latest revision (GitHub release or tag) for a given GitHub repository. func GetLatestRevision(client *github.Client, org, repo, currentRevision string) (string, bool, error) { - logger.V(6).Info(fmt.Sprintf("Getting latest revision for [%s/%s] repository\n", org, repo)) + logger.V(6).Info(fmt.Sprintf("Getting latest revision for [%s/%s] repository", org, repo)) var latestRevision string needsUpgrade := false @@ -247,7 +247,7 @@ func getCommitForTag(allTags []*github.RepositoryTag, searchTag string) string { // GetGoVersionForLatestRevision gets the Go version used to build the latest revision of the project. func GetGoVersionForLatestRevision(client *github.Client, org, repo, latestRevision string) (string, error) { - logger.V(6).Info(fmt.Sprintf("Getting Go version corresponding to latest revision %s for [%s/%s] repository\n", latestRevision, org, repo)) + logger.V(6).Info(fmt.Sprintf("Getting Go version corresponding to latest revision %s for [%s/%s] repository", latestRevision, org, repo)) cwd, err := os.Getwd() if err != nil { return "", fmt.Errorf("retrieving current working directory: %v", err) @@ -341,10 +341,9 @@ func GetGoVersionForLatestRevision(client *github.Client, org, repo, latestRevis } // CreatePullRequest creates a pull request from the head branch to the base branch on the base repository. -func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOwner, baseBranch, headRepoOwner, headBranch, currentRevision, latestRevision string, patchApplySucceeded bool, patchesWarningComment string) error { +func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOwner, baseBranch, headRepoOwner, headBranch, currentRevision, latestRevision string, addPatchWarningComment bool, patchesWarningComment string) error { var pullRequest *github.PullRequest var patchWarningCommentExists bool - logger.V(6).Info(fmt.Sprintf("Creating pull request with updated versions for [%s/%s] repository\n", org, repo)) // 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{ @@ -357,7 +356,7 @@ func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOw if len(pullRequests) > 0 { pullRequest = pullRequests[0] - logger.Info(fmt.Sprintf("A pull request already exists for %s:%s\n", headRepoOwner, headBranch), "Pull request", *pullRequest.HTMLURL) + 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) @@ -367,7 +366,7 @@ func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOw // If patches to the project failed to apply, check if the PR already has a comment warning about // the incomplete PR and patches needing to be regenerated. - if !patchApplySucceeded { + if addPatchWarningComment { pullRequestComments, _, err := client.Issues.ListComments(context.Background(), baseRepoOwner, constants.BuildToolingRepoName, *pullRequest.Number, nil) if err != nil { return fmt.Errorf("listing comments on pull request [%s]: %v", pullRequest.HTMLURL, err) @@ -380,6 +379,8 @@ func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOw } } } 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)), @@ -391,11 +392,13 @@ func CreatePullRequest(client *github.Client, org, repo, title, body, baseRepoOw if err != nil { return 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)) } // If patches failed to apply and no patch warning comment exists (always the case for a new PR), then add a comment with the // warning. - if !patchApplySucceeded && !patchWarningCommentExists { + if addPatchWarningComment && !patchWarningCommentExists { patchWarningComment := &github.IssueComment{ Body: github.String(patchesWarningComment), }