Modernize and refactor stackstorm.js #187
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR breaks up
src/stackstorm.js
intosrc/stackstorm_api.js
andsrc/stackstorm.js
(which is now just a wrapper).Additionally, the
stackstorm_api.js
module is refactored into aStackStormApi
JS "class", and the closed over global functions in the original anonymous closure have been broken out into their own constituent public methods.Notes
The
stop
function was reverted to the previous functionality to ensure that this refactoring doesn't break anything. That change will land in a future release.Thestop
function was changed slightly. Instead of opening a new connection to ST2's event stream and then immediately closing it:it now saves the event stream handler and uses that to close and stop the event stream handler:Before,
authenticate()
was run conditionally:And now that check is separated from the
authenticate()
call:I compensate for this in
stackstorm.js
almost perfectly:But, that isn’t quite good enough for some of the tests. So in order to test things like the environment variables
ST2_API
andHUBOT_2FA
being handled, you now have to specify proper values forST2_AUTH_URL
,ST2_AUTH_USERNAME
, andST2_AUTH_PASSWORD
:test-st2bot-envvars.js:69
test-st2bot-envvars.js:102
Coverage Differential
Test coverage went from:
to:
(unchanged coverage results omitted)
Further Work
Now that
StackStormApi
has been turned into a JS class, it should be easier to write unit tests for each of its public methods, and to update it to use new style JS classes with theclass
andextends
keywords.stop()
method to shutdown the previously openedst2stream
instead of opening a new one and immediately shutting it down - requires manual testing to make sure it doesn't break anything, or add a comment thatst2client.js
caches the connection and reuses it, so opening the same connection simply reuses the old connection and correctly shuts it downStackStormApi
public methodsstackstorm.js
wrapperprocess.on
unhandled rejection handler registrationrobot.error
handler registrationrobot.respond
handler registrationrobot.router.post
endpoint (see Code Refactor #133'sstackstorm.js:51
)stackstorm_api.js
fromstackstorm.js
a bit, similar to how it is implemented in Code Refactor #133StackStormApi
inherit fromEventEmitter
st2.chatops_announcement
andst2.execution_error
events (consumed instackstorm.js:68
andstackstorm.js:72
, respectively)HUBOT_2FA
)loadCommands
andstart
Note that I disagree with how
commandFactory
was turned into an event emitter in #133 (stackstorm.js:84
) so I'm not including it in this checklist.Edit: Tracking overall refactoring progress in #133.