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

FEAT: added reset branch to ref command #1716

Merged
merged 9 commits into from
Dec 10, 2024

Conversation

jamesqquick
Copy link
Contributor

What/Why?

When moving from OCC to development, users should have the ability to check out a ref that is then set to the main branch so they can continue smoothly in their development journey. This will put them in a state where they have the exact code they originally deployed in OCC but will be set to the main branch.

  • create reset-main boolean option for CLI
  • create new resetBranchToRef to be called if ghRef and resetMain are present
  • update checkoutRef to return a boolean status to determine whether or not it was run successfully

Testing

  • test existing OCC generated command in isolation - should work as expected
  • test existing OCC generated command with --reset-main flag with the end result of being in the main branch with code matching the gh-ref provided

@jamesqquick jamesqquick requested a review from a team as a code owner December 9, 2024 15:29
Copy link

changeset-bot bot commented Dec 9, 2024

⚠️ No Changeset found

Latest commit: ff33633

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Dec 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
catalyst-latest ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 10, 2024 6:15pm
catalyst-soul ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 10, 2024 6:15pm
3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
catalyst ⬜️ Ignored (Inspect) Dec 10, 2024 6:15pm
catalyst-au ⬜️ Ignored (Inspect) Visit Preview Dec 10, 2024 6:15pm
catalyst-uk ⬜️ Ignored (Inspect) Visit Preview Dec 10, 2024 6:15pm

Comment on lines 9 to 19
const checkoutSuccess = checkoutRef(repoDir, targetBranch);

// Reset current branch to the specified ref
if (checkoutSuccess) {
execSync(`git reset --hard ${ref}`, {
cwd: repoDir,
stdio: 'inherit',
encoding: 'utf8',
});
console.log(`Reset ${targetBranch} to ${ref} successfully.`);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we rely on errors being thrown instead of boolean conditions? If checkoutSuccess is false, we essentially do "nothing".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean checking the boolean condition of checkoutSuccess? Right now, checkoutRef function handles errors gracefully, meaning it doesn't surface it's errors to a caller. Because of that, I don't know how I would know if it ran successfully or not?

Right now, if checkoutSuccess is false the error handling and logging has already been handled in checkoutRef.

Does that address your question?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ummm... so to me CLI application logic flow should be handled by errors being thrown instead of conditional statements like you have currently. It allows us to surface errors and handle them through the catch statement, rather than if x, do y, else noop.

I think it should be something like this:

try {
  checkoutRef(...);

  execSync(...);
} catch (error) {
  if (error instanceof CheckoutRefError) {
    // handle fallback logic
  }

  // ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I understand that. The way things are written already, this would mean reworking checkoutRef to throw the errors that it's already handling? Then, we have to handle that error every time it's called, which is only once for now and potentially replicating logging for it?

packages/create-catalyst/src/utils/clone-catalyst.ts Outdated Show resolved Hide resolved
…to callers for checkoutRef and resetBranchtoRef
Copy link
Contributor

@matthewvolk matthewvolk left a comment

Choose a reason for hiding this comment

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

Looks good, I met with James and functionally this PR does what we want; there are improvements we can make to automatically set the upstream of local main to pull from the branch deployed by OCC. But to get this across the finish line, I'm going to approve this one.

@matthewvolk matthewvolk self-requested a review December 10, 2024 18:11
Copy link
Contributor

@matthewvolk matthewvolk left a comment

Choose a reason for hiding this comment

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

Looks good, but let's make note of the comment below 👍

Comment on lines +3 to +9
export function resetBranchToRef(projectDir: string, ghRef: string): void {
execSync(`git reset --hard ${ghRef}`, {
cwd: projectDir,
stdio: 'inherit',
encoding: 'utf8',
});
}
Copy link
Contributor

@matthewvolk matthewvolk Dec 10, 2024

Choose a reason for hiding this comment

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

As discussed internally, in the unlikely event that a bad ghRef is passed, this will throw and kill the CLI process. This will leave the user running the CLI in a state where the repository was cloned successfully, but dependencies wouldn't be installed nor would the environment file be written (see code reference below)

cloneCatalyst({ repository, projectName, projectDir, ghRef });
writeEnv(projectDir, {
channelId: channelId.toString(),
storeHash,
storefrontToken,
arbitraryEnv: options.env,
});
await installDependencies(projectDir);

For now, we'll accept this since it's unlikely the ghRef provided by OCC will be rejected. That said, we do need to handle errors in a follow up PR.

@jamesqquick jamesqquick added this pull request to the merge queue Dec 10, 2024
Merged via the queue into main with commit 2aa495d Dec 10, 2024
13 of 14 checks passed
@jamesqquick jamesqquick deleted the james/update-clone-to-reset-main branch December 10, 2024 18:22
Copy link
Contributor

⚡️🏠 Lighthouse report

Lighthouse ran against https://catalyst-latest-gqaiu2jx2-bigcommerce-platform.vercel.app

🖥️ Desktop

We ran Lighthouse against the changes on a desktop and produced this report. Here's the summary:

Category Score
🟢 Performance 99
🟢 Accessibility 96
🟢 Best practices 100
🟠 SEO 82

📱 Mobile

We ran Lighthouse against the changes on a mobile and produced this report. Here's the summary:

Category Score
🟢 Performance 91
🟢 Accessibility 96
🟢 Best practices 100
🟠 SEO 85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants