Skip to content

Commit

Permalink
fix: Make sure default status is set (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Oct 3, 2019
1 parent 6a59ef1 commit ec15dc3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
37 changes: 26 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const NotificationBase = require('screwdriver-notifications-base');
const Hoek = require('hoek');
const Joi = require('joi');
const emailer = require('./email');
const tinytim = require('tinytim');
Expand All @@ -18,15 +19,15 @@ const DEFAULT_STATUSES = ['FAILURE'];
const SCHEMA_ADDRESS = Joi.string().email();
const SCHEMA_ADDRESSES = Joi.array()
.items(SCHEMA_ADDRESS)
.min(0);
.min(1);
const SCHEMA_STATUS = Joi.string().valid(Object.keys(COLOR_MAP));
const SCHEMA_STATUSES = Joi.array()
.items(SCHEMA_STATUS)
.min(0);
.min(1);
const SCHEMA_EMAIL = Joi.alternatives().try(
Joi.object().keys({ addresses: SCHEMA_ADDRESSES, statuses: SCHEMA_STATUSES }),
SCHEMA_ADDRESS, SCHEMA_ADDRESSES
);
);
const SCHEMA_BUILD_SETTINGS = Joi.object()
.keys({
email: SCHEMA_EMAIL.required()
Expand Down Expand Up @@ -85,20 +86,34 @@ class EmailNotifier extends NotificationBase {
} catch (e) {
return;
}
if (typeof buildData.settings.email === 'string' ||
Array.isArray(buildData.settings.email)) {

const emailSettings = Hoek.reach(buildData, 'settings.email');

// Convert shorthand config to object
// ie: 'email: [[email protected], [email protected]]' or 'email: [email protected]'
if (typeof emailSettings === 'string' || Array.isArray(emailSettings)) {
buildData.settings.email = {
addresses: buildData.settings.email,
statuses: DEFAULT_STATUSES
addresses: emailSettings
};
}

if (!buildData.settings.email.statuses.includes(buildData.status)) {
// Make sure statuses are set
const defaultSettings = {
statuses: DEFAULT_STATUSES
};

buildData.settings.email = Object.assign(defaultSettings, buildData.settings.email);

const statuses = Hoek.reach(buildData, 'settings.email.statuses');

// Short circuit if status does not match
if (!statuses.includes(buildData.status)) {
return;
}

const subject = `${buildData.status} - Screwdriver ${buildData.pipeline.scmRepo.name} ` +
`${buildData.jobName} #${buildData.build.id}`;
const subject = `${buildData.status} - Screwdriver ` +
`${Hoek.reach(buildData, 'pipeline.scmRepo.name')} ` +
`${buildData.jobName} #${Hoek.reach(buildData, 'build.id')}`;
const message = `Build status: ${buildData.status}` +
`\nBuild link:${buildData.buildLink}`;
const html = tinytim.renderFile(path.resolve(__dirname, './template/email.html'), {
Expand All @@ -110,7 +125,7 @@ class EmailNotifier extends NotificationBase {

const mailOpts = {
from: this.config.from,
to: buildData.settings.email.addresses,
to: Hoek.reach(buildData, 'settings.email.addresses'),
subject,
text: message,
html
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@
],
"devDependencies": {
"chai": "^3.5.0",
"eslint": "^3.9.1",
"eslint-config-screwdriver": "^2.0.9",
"hapi": "^17.0.0",
"jenkins-mocha": "^6.0.0",
"eslint": "^4.19.1",
"eslint-config-screwdriver": "^3.0.1",
"@hapi/hapi": "^17.8.5",
"jenkins-mocha": "^8.0.0",
"mockery": "^2.0.0",
"sinon": "^1.17.7"
"sinon": "^7.4.2"
},
"dependencies": {
"hoek": "^6.1.3",
"joi": "^10.2.2",
"nodemailer": "^4.6.7",
"screwdriver-notifications-base": "^3.0.0",
"nodemailer": "^4.7.0",
"screwdriver-notifications-base": "^3.0.2",
"tinytim": "^0.1.1"
},
"release": {
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const Hapi = require('hapi');
const Hapi = require('@hapi/hapi');
const assert = require('chai').assert;
const mockery = require('mockery');
const sinon = require('sinon');
Expand Down

0 comments on commit ec15dc3

Please sign in to comment.