From 7d85e444c5f7076985e01284f2ab3f2a3811f836 Mon Sep 17 00:00:00 2001 From: onelong Date: Mon, 10 Sep 2018 19:28:22 +0800 Subject: [PATCH 1/5] chore(package): Move coffee-scirpt to coffeescript --- docs/adapters/development.md | 2 +- docs/deploying/azure.md | 2 +- docs/scripting.md | 6 +++--- package.json | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/adapters/development.md b/docs/adapters/development.md index 3b39484cf..617748d8b 100644 --- a/docs/adapters/development.md +++ b/docs/adapters/development.md @@ -52,7 +52,7 @@ exports.use = (robot) -> "hubot": ">=2.0" }, "devDependencies": { - "coffee-script": ">=1.2.0" + "coffeescript": ">=1.2.0" } ``` diff --git a/docs/deploying/azure.md b/docs/deploying/azure.md index ddaf0dd65..a62d4c076 100644 --- a/docs/deploying/azure.md +++ b/docs/deploying/azure.md @@ -40,7 +40,7 @@ Then, edit this file and look for the sections that give you steps 1, 2 and 3. Y Now, create a new file in the base directory of hubot called `server.js` and put these two lines into it: - require('coffee-script/register'); + require('coffeescript/register'); module.exports = require('hubot/bin/hubot.coffee'); Finally you will need to add the environment variables to the website to make sure it runs properly. You can either do it through the GUI (under configuration) or you can use the Azure PowerShell command line, as follows (example is showing slack as an adapter and mynewhubot as the website name). diff --git a/docs/scripting.md b/docs/scripting.md index ce1b5dbbe..e5e13afb6 100644 --- a/docs/scripting.md +++ b/docs/scripting.md @@ -871,12 +871,12 @@ You'll also need to install: You may also want to install: - * *coffee-script* (if you're writing your tests in CoffeeScript rather than JavaScript) + * *coffeescript* (if you're writing your tests in CoffeeScript rather than JavaScript) * a mocking library such as *Sinon.js* (if your script performs webservice calls or other asynchronous actions) Here is a sample script that tests the first couple of commands in the -[Hubot sample script](https://github.com/hubotio/generator-hubot/blob/master/generators/app/templates/scripts/example.coffee). This script uses *Mocha*, *chai*, *coffee-script*, and of course *hubot-test-helper*: +[Hubot sample script](https://github.com/hubotio/generator-hubot/blob/master/generators/app/templates/scripts/example.coffee). This script uses *Mocha*, *chai*, *coffeescript*, and of course *hubot-test-helper*: **test/example-test.coffee** ```coffeescript @@ -918,7 +918,7 @@ describe 'example script', -> **sample output** ```bash -% mocha --require coffee-script/register test/*.coffee +% mocha --require coffeescript/register test/*.coffee example script diff --git a/package.json b/package.json index 42df686c3..da88cc036 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "async": ">=0.1.0 <1.0.0", "chalk": "^1.0.0", "cline": "^0.8.2", - "coffee-script": "1.6.3", + "coffeescript": "1.6.3", "connect-multiparty": "^2.1.1", "express": "^4.16.3", "log": "1.4.0", From 69b6f5fa8b4ef815430e45433daf6866b9f28da4 Mon Sep 17 00:00:00 2001 From: Keita Tsukamoto Date: Wed, 7 Nov 2018 13:30:12 +0900 Subject: [PATCH 2/5] docs(src/robot.js): add comment for alias --- src/robot.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/robot.js b/src/robot.js index f6a93640e..ae5029020 100644 --- a/src/robot.js +++ b/src/robot.js @@ -25,6 +25,7 @@ class Robot { // adapter - A String of the adapter name. // httpd - A Boolean whether to enable the HTTP daemon. // name - A String of the robot name, defaults to Hubot. + // alias - A String of the alias of the robot name constructor (adapterPath, adapter, httpd, name, alias) { if (name == null) { name = 'Hubot' From 6ac682351deae2aff701ebac461b94ea2ea7480c Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Tue, 4 Dec 2018 15:51:47 +1100 Subject: [PATCH 3/5] Adapter docs: document ES2015 entry point The default Hubot require entrypoint is only compatible with CoffeeScript. A separate entrypoint has to be used when writing ES2015 classes. --- docs/adapters/development.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/docs/adapters/development.md b/docs/adapters/development.md index 3b39484cf..3ffee9a3c 100644 --- a/docs/adapters/development.md +++ b/docs/adapters/development.md @@ -6,7 +6,19 @@ permalink: /docs/adapters/development/ ## Adapter Basics -All adapters inherit from the Adapter class in the `src/adapter.coffee` file. There are certain methods that you will want to override. Here is a basic stub of what an extended Adapter class would look like: +All adapters inherit from the Adapter class in the `src/adapter.coffee` file. If you're writing your adapter in CoffeeScript, require the primary version of the adapter: + +```coffee +Adapter = require('hubot').Adapter +``` + +If you're writing your adapter in ES2015, you must require the ES2015 entrypoint instead: + +```js +const Adapter = require('hubot/es2015').Adapter; +``` + +There are certain methods that you will want to override. Here is a basic stub of what an extended Adapter class would look like: ```coffee class Sample extends Adapter From 685e0ad0ce399d628304e7d6ca291c9d70aa7644 Mon Sep 17 00:00:00 2001 From: Bernard Vander Beken Date: Thu, 13 Dec 2018 11:27:30 +0100 Subject: [PATCH 4/5] Fix spelling --- docs/adapters.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/adapters.md b/docs/adapters.md index d7096d65d..77f63df31 100644 --- a/docs/adapters.md +++ b/docs/adapters.md @@ -11,7 +11,7 @@ Hubot includes two official adapters: * [Shell](./adapters/shell.md), i.e. for use with development * [Campfire](./adapters/campfire.md) -There are Third-party adapaters are available for most chat services. Here are the most popular ones: +There are Third-party adapters available for most chat services. Here are the most popular ones: * [Gitter](https://github.com/huafu/hubot-gitter2) * [HipChat](https://github.com/hipchat/hubot-hipchat) From a6201ad4e856a0d467a925d6335bc422b9ed1577 Mon Sep 17 00:00:00 2001 From: Misty De Meo Date: Tue, 18 Dec 2018 20:07:30 +1100 Subject: [PATCH 5/5] feat(Brain): ensure User objects are actually User objects --- src/brain.js | 30 ++++++++++++++++++++++++++++++ test/brain_test.js | 19 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/brain.js b/src/brain.js index f6697dfa6..7e7b14363 100644 --- a/src/brain.js +++ b/src/brain.js @@ -4,6 +4,28 @@ const EventEmitter = require('events').EventEmitter const User = require('./user') +// If necessary, reconstructs a User object. Returns either: +// +// 1. If the original object was falsy, null +// 2. If the original object was a User object, the original object +// 3. If the original object was a plain JavaScript object, return +// a User object with all of the original object's properties. +let reconstructUserIfNecessary = function (user) { + if (!user) { + return null + } + + if (!user.constructor || (user.constructor && user.constructor.name !== 'User')) { + let id = user.id + delete user.id + // Use the old user as the "options" object, + // populating the new user with its values. + return new User(id, user) + } else { + return user + } +} + class Brain extends EventEmitter { // Represents somewhat persistent storage for the robot. Extend this. // @@ -116,6 +138,14 @@ class Brain extends EventEmitter { this.data[k] = data[k] } + // Ensure users in the brain are still User objects. + if (data && data.users) { + for (let k in data.users) { + let user = this.data.users[k] + this.data.users[k] = reconstructUserIfNecessary(user) + } + } + this.emit('loaded', this.data) } diff --git a/test/brain_test.js b/test/brain_test.js index 99740695c..22c11f87b 100644 --- a/test/brain_test.js +++ b/test/brain_test.js @@ -58,6 +58,14 @@ describe('Brain', function () { this.brain.mergeData({}) expect(this.brain.emit).to.have.been.calledWith('loaded', this.brain.data) }) + + it('coerces loaded data into User objects', function () { + this.brain.mergeData({users: {'4': {'name': 'new', 'id': '4'}}}) + let user = this.brain.userForId('4') + expect(user.constructor.name).to.equal('User') + expect(user.id).to.equal('4') + expect(user.name).to.equal('new') + }) }) describe('#save', () => it('emits a save event', function () { @@ -308,5 +316,16 @@ describe('Brain', function () { expect(result).to.have.members([this.user1, this.user2]) expect(result).to.not.have.members([this.user3]) }) + + it('returns User objects, not POJOs', function () { + expect(this.brain.userForId('1').constructor.name).to.equal('User') + for (let user of this.brain.usersForFuzzyName('Guy')) { + expect(user.constructor.name).to.equal('User') + } + + for (let user of this.brain.usersForRawFuzzyName('Guy One')) { + expect(user.constructor.name).to.equal('User') + } + }) }) })