Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Function "runPromote" is overly complex #1343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

varshith257
Copy link

@varshith257 varshith257 commented Jun 13, 2024

What type of PR is this?

/kind feature

What this PR does / why we need it:

The current runPromote function is too complex. Instead organised code into helper functions of runPromote function

Which issue(s) this PR fixes:

Special notes for your reviewer:

Does this PR introduce a user-facing change?

No

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/feature Categorizes issue or PR as related to a new feature. labels Jun 13, 2024
Copy link

linux-foundation-easycla bot commented Jun 13, 2024

CLA Signed


The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added area/artifacts Issues or PRs related to the hosting of release artifacts for subprojects cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jun 13, 2024
@k8s-ci-robot k8s-ci-robot added the area/release-eng Issues or PRs related to the Release Engineering subproject label Jun 13, 2024
@k8s-ci-robot k8s-ci-robot added the sig/release Categorizes an issue or PR as relevant to SIG Release. label Jun 13, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @varshith257!

It looks like this is your first PR to kubernetes-sigs/promo-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/promo-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jun 13, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @varshith257. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Jun 13, 2024
@varshith257 varshith257 marked this pull request as draft June 13, 2024 10:08
@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 13, 2024
@varshith257 varshith257 marked this pull request as ready for review June 13, 2024 10:08
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 13, 2024
@k8s-ci-robot k8s-ci-robot requested a review from jrsapi June 13, 2024 10:08
@varshith257
Copy link
Author

cc/@cpanato @xmudrii

Copy link
Member

@cpanato cpanato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ok-to-test

@cpanato
Copy link
Member

cpanato commented Jun 13, 2024

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jun 13, 2024
@varshith257 varshith257 requested a review from cpanato June 13, 2024 16:03
@varshith257
Copy link
Author

@saschagrunert PTAL

@varshith257
Copy link
Author

@xmudrii @cpanato Any review on it?

@cpanato
Copy link
Member

cpanato commented Jun 28, 2024

@varshith257 i would like to run that, but I need more time
but im general you split the function in smaller ones, correct?

@varshith257
Copy link
Author

@cpanato YES! As described in the issue to make it code reusability and reduce the cognitive load of it to understand, I have split it into smaller helper functions of runPromote

@varshith257
Copy link
Author

varshith257 commented Jul 22, 2024

@cpanato Can you have a look into it?

@cpanato
Copy link
Member

cpanato commented Jul 22, 2024

@cpanato Can you have a look into it?

i am on PTO will be back end of august only

@xmudrii
Copy link
Member

xmudrii commented Jul 22, 2024

I'll do the initial review in the upcoming days
/assign

@varshith257
Copy link
Author

varshith257 commented Jul 22, 2024

Thanks @xmudrii !

A quick info,
Do the project of refactoring of promo-tools project to support sig will be getting into Term 3 LFX Mentorship?

It has been cancelled in Term 2. I am one of the applicant for this project in Term 2 and eagerly waiting to mentor under the Kubernetes organisation

@xmudrii
Copy link
Member

xmudrii commented Jul 22, 2024

@varshith257 As far as I know, we don't have plans for submitting a project for the LFX Term 3, but follow our #sig-release Slack channel for the latest updates

@varshith257
Copy link
Author

varshith257 commented Jul 22, 2024

@xmudrii I am unable to join the Kubernetes slack. It's showing I don't have an email account for the workspace.

Can I get an invite Link for the Kubernetes slack to join?

Mail id: [email protected]

@xmudrii
Copy link
Member

xmudrii commented Jul 22, 2024

@varshith257 Please use slack.k8s.io to register on the Kubernetes Slack.

Copy link
Member

@xmudrii xmudrii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank your for this PR and the initiative to improve our code base! However, in order for this PR to get merged, the refactored code must match the original behavior prior to the refactor. I left some comments in places where this is not the case, once this is fixed, we can merge this PR. I also left some comments pointing what you can improve in this PR.

return nil
}

func prepareRepository(branchname string, opts *promoteOptions) (*git.Repo, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two nits:

  • given this is about the fork, maybe we should call it prepareFork
  • I have mixed feelings about branchname, I'd prefer branchName, although I see that the variable in rumPromote is called branchname

return nil
}

func prepareRepository(branchname string, opts *promoteOptions) (*git.Repo, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, I'd make all these functions that take promoteOptions as an argument, take promoteOptions as a receiver, e.g. func (opts *promoteOptions) prepareRepository (branchname string)

@@ -232,78 +267,82 @@ func runPromote(opts *promoteOptions) error {
)

// Read the current manifest to check later if new images come up
oldlist := make([]byte, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code is in the mustRun block only up to this point. I believe this function should do the same.

Comment on lines +335 to 339
// add the modified manifest to staging
logrus.Debugf("Adding %s to staging area", imagesListPath)
if err := repo.Add(imagesListPath); err != nil {
return false, fmt.Errorf("adding image manifest to staging area: %w", err)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, this doesn't really feel like part of this function to me. Additionally, if you would put it in the runPronmote function, you could avoid passing repo and instead pass the value of repo.Dir().

}()

// Handle manifests
modified, err := handleManifests(ctx, repo, opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleManifests is a bit broad name, what do you think about calling it growManifest instead?

Comment on lines +358 to +361
if mustRun(opts, fmt.Sprintf("Push changes to user's fork at %s/%s?", opts.userFork, k8sioRepo)) {
logrus.Infof("Pushing manifest changes to %s/%s", opts.userFork, k8sioRepo)
if err := repo.PushToRemote(github.UserForkName, branchname); err != nil {
return fmt.Errorf("pushing %s to %s/%s: %w", github.UserForkName, userForkOrg, userForkRepo, err)
return fmt.Errorf("pushing %s to %s/%s: %w", github.UserForkName, opts.userFork, k8sioRepo, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong to me, why did you replace userForkOrg with opts.userFork and userForkRepo with k8sioRepo?

Comment on lines +365 to 366
logrus.Infof("Exiting without creating a PR since changes were not pushed to %s/%s", opts.userFork, k8sioRepo)
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tool is supposed to exit here, however, it would continue running after this function. You should do something similar as in handleManifests or use os.Exit.

gh := github.New()
pr, err := gh.CreatePullRequest(
git.DefaultGithubOrg, k8sioRepo, k8sioDefaultBranch,
fmt.Sprintf("%s:%s", opts.userFork, branchname),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you replace userForkOrg with opts.userFork?

pr, err := gh.CreatePullRequest(
git.DefaultGithubOrg, k8sioRepo, k8sioDefaultBranch,
fmt.Sprintf("%s:%s", opts.userFork, branchname),
"Image promotion for "+opts.project+" "+strings.Join(opts.tags, " / "),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't match the original behavior, the commit message can be extended in some case, e.g. with the releng: prefix.

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: varshith257
Once this PR has been reviewed and has the lgtm label, please ask for approval from xmudrii. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 17, 2024
@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/artifacts Issues or PRs related to the hosting of release artifacts for subprojects area/release-eng Issues or PRs related to the Release Engineering subproject cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/release Categorizes an issue or PR as relevant to SIG Release. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Function "runPromote" is overly complex
4 participants