Skip to content

Commit

Permalink
make it possible to enable @here for certain events
Browse files Browse the repository at this point in the history
  • Loading branch information
jharting committed Dec 19, 2017
1 parent cd6c26a commit 4777824
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ The following environment variables can be used to tune Insights Slackbot:
* `SLACK_CHANNEL` - Slack channel the bot will post messages to (insights-webhooks by default)
* `SERVER_SECRET`- secret part of the URL that prevents unauthorized actors from posting
* `SERVER_PORT` - server port (3006 by default)
* `INSIGHTS_URL` - the base URL of Red Hat Insights. Defaults to https://access.redhat.com/insightsbeta
* `ATHERE` - comma-separated list of events for which `@here` should be used. None by default
4 changes: 3 additions & 1 deletion app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ module.exports = {
},
insights: {
url: process.env.INSIGHTS_URL || 'https://access.redhat.com/insightsbeta'
}
},

here: process.env.ATHERE ? String(process.env.ATHERE).split(',') : []
};
38 changes: 36 additions & 2 deletions app/events/events.unit.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* global describe, it */
'use strict';

require('should');
const test = require('../../test');
const events = require('./');
const config = require('../config');

describe('Events', function () {

Expand Down Expand Up @@ -53,6 +54,20 @@ describe('Events', function () {
});
});

describe('system:unregistered', function () {
it('produces desired output', function () {
const msg = {
system: {
system_id: 'dd18ed75-44f5-4fd1-8ca0-ed08c2d9c320',
toString: 'jozef_vm04'
}
};

const result = events['system:unregistered'](msg);
result.should.equal('@here System unregistered: *jozef_vm04* (dd18ed75-44f5-4fd1-8ca0-ed08c2d9c320)');
});
});

describe('policy:new', function () {
it('produces desired output', function () {
const msg = {
Expand All @@ -67,7 +82,7 @@ describe('Events', function () {
});
});

describe('policy:new', function () {
describe('policy:removed', function () {
it('produces desired output', function () {
const msg = {
policy: {
Expand All @@ -79,4 +94,23 @@ describe('Events', function () {
result.should.equal('Policy removed: *policy-1*');
});
});

describe('configuration', function () {
it('it uses configured base URL', function () {
test.sandbox.stub(config.insights, 'url').value('https://access.redhat.com/insights');

const msg = {
system: {
system_id: 'dd18ed75-44f5-4fd1-8ca0-ed08c2d9c320',
toString: 'jozef_vm04'
}
};

const result = events['system:registered'](msg);
result.should.equal('System registered: <https://access.redhat.com/insights/inventory?machine=dd18ed75-44f5-4fd1-8ca0-ed08c2d9c320|jozef_vm04>' +
' (dd18ed75-44f5-4fd1-8ca0-ed08c2d9c320)');
});


});
});
19 changes: 16 additions & 3 deletions app/events/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const config = require('../config');
const baseUrl = config.insights.url;

const SEVERITIES = {
INFO: 'low severity',
Expand Down Expand Up @@ -30,11 +29,11 @@ function getProblemDescription (msg) {
}

function systemLink (system) {
return `<${baseUrl}/inventory?machine=${system.system_id}|${system.toString}>`;
return `<${config.insights.url}/inventory?machine=${system.system_id}|${system.toString}>`;
}

function ruleLink (rule) {
return `<${baseUrl}/actions/${rule.category.toLowerCase()}/${encodeURIComponent(rule.rule_id)}|${rule.description}>`;
return `<${config.insights.url}/actions/${rule.category.toLowerCase()}/${encodeURIComponent(rule.rule_id)}|${rule.description}>`;
}

module.exports = {
Expand Down Expand Up @@ -68,3 +67,17 @@ module.exports = {
return `Policy removed: *${msg.policy.policy_id}*`;
}
};

// wrap event processor with @here for certain events configured via ATHERE
config.here.forEach(event => {
const delegate = module.exports[event];

if (!delegate) {
throw new Error(`Invalid event type: ${event}`);
}

module.exports[event] = function (...args) {
const result = delegate(...args);
return `@here ${result}`;
};
});
102 changes: 100 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"lint": "./node_modules/eslint/bin/eslint.js app server.js",
"nsp": "./node_modules/nsp/bin/nsp check",
"test": "SLACK_TOKEN=xoxb123 ./node_modules/mocha/bin/mocha '**/*.unit.js'",
"test": "SLACK_TOKEN=xoxb123 ATHERE=system:unregistered ./node_modules/mocha/bin/mocha '**/*.unit.js'",
"verify": "npm run lint && npm test && npm run nsp"
},
"pre-push": [
Expand All @@ -31,7 +31,8 @@
"body-parser": "1.18.2",
"bunyan": "1.8.12",
"express": "4.16.0",
"lodash": "4.17.4"
"lodash": "4.17.4",
"sinon": "^4.1.3"
},
"devDependencies": {
"eslint": "^4.11.0",
Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* global beforeEach, afterEach */
'use strict';

require('should');
const sinon = require('sinon');

beforeEach(() => exports.sandbox = sinon.sandbox.create());
afterEach(() => exports.sandbox.restore());

0 comments on commit 4777824

Please sign in to comment.