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: implement create site from template feature #3948

Merged
merged 23 commits into from Feb 7, 2022

Conversation

charliegerard
Copy link
Contributor

@charliegerard charliegerard commented Jan 4, 2022

Summary

There's a new feature in the UI to start a site from a template that I think should also be available via the CLI so this PR implements this feature πŸ™‚

FYI, I re-used a lot of the code already written for the sites:create command.

Related to #3947

PR test plan:

  • Check the update to the docs site
  • When running the command netlify sites:create-template, you should be prompted to pick a template out of the ones currently available (Next.js blog theme, Next.js starter, Nuxt, Hugo).
  • Step 2 should be asking to pick a team and list the current user's teams.
  • Step 3 should be asking you to log into GitHub either via the Netlify UI or by entering a GitHub token.
  • When authenticated with GitHub, step 4 should ask you to enter a site name, if you pick a name that already exists as a repo on your github account, it should ask you to pick another one.
  • If you picked a unique name, it should succeed, create a repo in your Github account, a site on Netlify and the CLI should show you the links to the site and repo.
  • If you don't input a site name, it should pick a random one and assign it to both the repo and site.

For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code πŸ§‘β€πŸ’». This ensures we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or something that`s on fire πŸ”₯ (e.g. incident related), you can skip this step.
  • Read the contribution guidelines πŸ“–. This ensures your code follows our style guide and
    passes our tests.
  • Update or add documentation (if features were changed or added) πŸ“
  • Make sure the status checks below are successful βœ…

A picture of a cute animal (not mandatory, but encouraged)

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Jan 4, 2022
@github-actions
Copy link

github-actions bot commented Jan 4, 2022

πŸ“Š Benchmark results

Comparing with 127a1c6

Package size: 368 MB

⬇️ 0.00% decrease vs. 127a1c6

^  363 MB  362 MB  363 MB  363 MB  363 MB  362 MB  363 MB  363 MB  362 MB  362 MB  361 MB  361 MB  368 MB 
β”‚   β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”    β”Œβ”€β”€β”  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
β”‚   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |β–’β–’|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
Legend

@charliegerard charliegerard force-pushed the cg/addCreateSitesFromTemplateFeature branch 2 times, most recently from 3022af0 to e79b826 Compare January 12, 2022 01:13
@charliegerard charliegerard marked this pull request as ready for review January 26, 2022 23:59
@charliegerard charliegerard requested a review from a team as a code owner January 26, 2022 23:59
@charliegerard
Copy link
Contributor Author

@maxcell & @tzmanics adding you as reviewers if you wanna give it a try πŸ™‚

Copy link
Collaborator

@lukasholzer lukasholzer left a comment

Choose a reason for hiding this comment

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

Thanks for contributing such an awesome feature 🐳 Overall a very nice done PR! Only some minor changes.

Can you please add a small unit test with ava that mocks the api requests away but tests the flow of this command?
Even though it is beta but beta often moves to prod by just removing the word beta πŸ˜‚

src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
console.log(
`Choose a unique site name (e.g. ${siteSuggestion}.netlify.app) or leave it blank for a random name. You can update the site name later.`,
)
const { name: nameInput } = await inquirer.prompt([
Copy link
Collaborator

Choose a reason for hiding this comment

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

is there a possibility to skip that input if it is optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was mostly copied from the sites:create command. I think in general i'd wanna give people the choice to pick a title or not?

src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Show resolved Hide resolved
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

Thanks @charliegerard and @lukasholzer, I added a few comments of my own too

src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Show resolved Hide resolved
throw new Error('Duplicate repo')
}
} else {
site = await api.createSiteInTeam({
Copy link
Contributor

Choose a reason for hiding this comment

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

This will create the site, but does not set up the CI part. Meaning a commit to the repo won't trigger a build on Netlify.
See

if (options.withCi) {
if we want the behavior to configure GitHub as well

@charliegerard
Copy link
Contributor Author

Thanks for the feedback @lukasholzer & @erezrokah! Working through it now and will ask for another review soon, thanks!

@charliegerard charliegerard force-pushed the cg/addCreateSitesFromTemplateFeature branch from 00f1991 to 1204098 Compare February 3, 2022 18:09

return name
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are only to be able to extract the piece of code that is reused in the sites-create-template.js file to avoid duplication, the logic isn't changed

*/

test.skip('netlify sites:create-template', async (t) => {
const siteTemplateQuestions = [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@lukasholzer I tried to add some tests for this command but I've been struggling and could do with some guidance on how to mock the API calls to Github if you have some idea or if you know somewhere else in the code I should look at, please? πŸ™ I tried using sinon and also nock but didn't manage to make it work 😞

For now I skipped these tests cause they ping the real Github API.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @charliegerard I've added a commit showcasing how to mock the github api – so you should good to go writing the test.

The problem you faced was that it is impossible to mock something that is spawned in a child process -> what you do by calling execa -> basically a wrapper over child_process spawn.

So the cool thing is that you can start up a command without invoking it as a child process and then you achieve ultimate power of having the same global context and therefore mocking things out.

The only importance is that you need to do the mocks before you require the actual code. As it needs to patch the functions first with the stubbed logic.

I hope you are good to go now :) otherwise don't hesitate to reach out to me maybe we can do a 🍐 on that if it helps you!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Here the commit: aee0ff5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome, thank you so much! I'll have a look!

@charliegerard
Copy link
Contributor Author

charliegerard commented Feb 3, 2022

@lukasholzer & @erezrokah I think I implemented most of the feedback, the main thing missing is around the tests that I've been having some issues with. Let me know what you think!

Also, i'm not sure i understand why this check fails πŸ€”.

I also added the --with-ci option but it doesn't seem to work, I think i'm confused with the step asking to provide the SSH URL of the remote repo. Even when i do, the deploy fails and it's marking deploys as manual :/

@charliegerard
Copy link
Contributor Author

@lukasholzer I added some tests so this PR should be ready for review again πŸ™‚ Thanks!

lukasholzer
lukasholzer previously approved these changes Feb 7, 2022
Copy link
Collaborator

@lukasholzer lukasholzer left a comment

Choose a reason for hiding this comment

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

Thank you so much for adding the tests and improving our overall test coverage πŸ’― This deserves a big 🐬 for awesomeness! πŸš€

erezrokah
erezrokah previously approved these changes Feb 7, 2022
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

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

This looks greats @charliegerard πŸš€ That you for following up with everything.

Added a few non blocking comments.

src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
src/commands/sites/sites-create-template.js Outdated Show resolved Hide resolved
@erezrokah
Copy link
Contributor

@charliegerard I re-approved so you can add automerge when you're ready

@charliegerard
Copy link
Contributor Author

@erezrokah Thanks so much! Do I need to rename the branch or anything? Looking at the contributing file, it says to have a branch named releases/<tag>/<version>. If not, I'll just enable auto-merge πŸ˜ƒ

@lukasholzer lukasholzer added the automerge Add to Kodiak auto merge queue label Feb 7, 2022
@kodiakhq kodiakhq bot merged commit 3ebfea9 into main Feb 7, 2022
@kodiakhq kodiakhq bot deleted the cg/addCreateSitesFromTemplateFeature branch February 7, 2022 18:26
@erezrokah
Copy link
Contributor

@erezrokah Thanks so much! Do I need to rename the branch or anything? Looking at the contributing file, it says to have a branch named releases/<tag>/<version>. If not, I'll just enable auto-merge

That's for pre-releases. For official releases once you merge a PR, a release PR is generated.
See #4207. Once the release PR is merged a new release is published.

I took the liberty of merging an releasing this in #4207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Add to Kodiak auto merge queue type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants