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
Enables service installation with a name #2616
Conversation
@pmuens, I noticed that you wrote the original download test, what kind of a test should I write for this? The current installation test just seems to mock download or something like that (the url is invalid). |
49d99b7
to
bd37e27
Compare
Hey @laardee. Great addition to the Hmm. Couldn't you stub the Maybe @eahefnawy and @flomotlik can add some thoughts here. Today I stumbled upon an improvement for the Basically it should be moved in the Let me know if you need any help! |
@pmuens thanks, I wrote unit test for renameService af53bcb, it creates serverless.yml and package.json files (only service and name keys), then it uses renameService to change those and verifies that. I also moved downloadStub to beforeEach as you suggested and added one expect to check that the path that is created contains the new name for service df499e7. |
Hey @laardee thank you. Sounds good! 👍 Will review and take a deep dive ASAP. |
I changed yml read/write in renameService function to use plain fs module with string replace to preserve comments in serverless.yml, js-yaml module doesn't seem to support comments nodeca/js-yaml#305. |
4ea3510
to
80b1bb7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @laardee thanks for your work on this one and the requested updates 💯 ! Great job 👍 Really like the functionality!
I added a few comments.
Pinging @eahefnawy and @flomotlik for feedback as well!
``` | ||
|
||
This example will download the .zip file of the `authentication` service from GitHub, create a new directory with the name `my-authentication` in the current working directory, unzips the files in this directory and renames service to `my-authentication` if `serverless.yml` exists in the service root. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"... and renames the service"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -22,6 +24,11 @@ class Install { | |||
required: true, | |||
shortcut: 'u', | |||
}, | |||
name: { | |||
usage: 'Name for the service', | |||
required: false, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be left out as it means that same as require: false
(AFAIK we don't follow this pattern in other plugins).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
.then(contents => this.serverless.utils.writeFile(packageFile, contents)), | ||
]) | ||
.then(() => resolve(` as "${name}"`)) | ||
.catch((error) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be enhanced following this pattern we use throughout the codebase (
serverless/lib/plugins/aws/deploy/compile/events/apiGateway/lib/validate.js
Lines 43 to 50 in 5430fd8
const errorMessage = [ | |
`Invalid http event in function "${functionName}"`, | |
' in serverless.yml.', | |
' If you define an http event, make sure you pass a valid value for it,', | |
' either as string syntax, or object syntax.', | |
' Please check the docs for more options.', | |
].join(''); | |
throw new this.serverless.classes.Error(errorMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or you could let Serverless catch the error and remove the catch
block altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refactored this to be synchronous and used this.serverless.classes.Error
for throwing error. Error message could still be better.
@@ -77,5 +129,18 @@ describe('Install', () => { | |||
expect(downloadStub.args[0][0]).to.equal(`${install.options.url}/archive/master.zip`); | |||
}); | |||
}); | |||
|
|||
it('should download and rename the service based on the GitHub URL', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we enhance this test somehow that the call to the renaming method is tested as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pmuens, so basically either serverless.yml should be created to the download folder beforehand or downloadStub should pass zip containing serverless.yml as a payload. The first one might not work, because file should be created to folder before running the install function, which checks if the folder exists and if so it won't install the package. I'll check if I can pass the zip as a payload.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the downloadStub mocks the zip loading and saves serverless.yml, then installation process renames the service value and finally the result is tested.
readFile(serviceFile, 'utf-8') | ||
.then(contents => | ||
contents.replace(/service\s*:.+/gi, (match) => { | ||
const fractions = match.split('#'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are commented out parts in a serverless.yml
file that common?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know about others, but at least we have some comments on our workshop boilerplates and other similar project templates.
return fractions.join(' #'); | ||
})) | ||
.then(contents => writeFile(serviceFile, contents)), | ||
this.serverless.utils.readFile(packageFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does a service always have a package.json
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, fixed.
cba0f00
to
165cd42
Compare
@pmuens, ready for review round two 😅 |
Hey @laardee I take a deep dive later again because we're currently focused on 1.2 (https://github.com/serverless/serverless/milestone/16) to get this out of the door next week. Again. Thanks for working on this really good addition! 👍 💃 |
5822407
to
001dc0f
Compare
40462ac
to
dcdbbda
Compare
@pmuens, have you already had time to look into this? I know this is not the first priority PR so I don't mind waiting and rebasing 😄 |
@laardee thanks for getting back. 👍 Unfortunately I didn't have any time yet 😞 . But we'll plan the next milestones soon and will prioritize this PR soon. Sorry for the inconveniences. |
dcdbbda
to
81a19eb
Compare
81a19eb
to
24ccca2
Compare
24ccca2
to
2829566
Compare
rebased, tests are now passing 👌 |
I wonder if 🎅 comes before next review round. 😀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @laardee sorry for the slow response / review on our end!
🎅 is finally here 😆 ! I looked over the code today and tested it. Works great! Good job 👍
I just pushed some minor changes on top of your PR. Basically the tests are now side-effect free and more predictable.
GTM from my side. Waiting for another Approval and then we can merge it! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What did you implement:
This PR enables to rename the service when installing from GitHub, for example
serverless install --name new-service-name --url https://github.com/SC5/serverless-messenger-boilerplate
will installserverless-messenger-boilerplate
to foldernew-service-name
and changeservice
value inserverless.yml
tonew-service-name
.Closes #2626
How did you implement it:
Added new parameter
name
to install class and renameService helper function for renaming service in yaml file.How can we verify it:
serverless install --name new-service-name --url https://github.com/SC5/serverless-messenger-boilerplate
should install service tonew-service-name
folder and change service name tonew-service-name
.Todos:
Is this ready for review?: YES