Skip to content

Commit

Permalink
Fix condition for patches warning comment on upgrade PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
abhay-krishna committed Mar 28, 2024
1 parent dd38cc8 commit 2530789
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 3 additions & 3 deletions tools/version-tracker/pkg/commands/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 6 additions & 3 deletions tools/version-tracker/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down
25 changes: 14 additions & 11 deletions tools/version-tracker/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)),
Expand All @@ -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),
}
Expand Down

0 comments on commit 2530789

Please sign in to comment.