Skip to content

Commit

Permalink
feat: Allow disabling backend notifications via env var
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Apr 20, 2021
1 parent adbabd1 commit e822033
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"author": "Serverless, Inc.",
"dependencies": {
"chalk": "^4.1.0",
"ci-info": "^3.1.1",
"inquirer": "^7.3.3",
"js-yaml": "^4.0.0",
"jwt-decode": "^3.1.2",
Expand All @@ -32,6 +33,7 @@
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"process-utils": "^4.0.0",
"proxyquire": "^2.1.3",
"sinon": "^9.2.4",
"sinon-chai": "^3.5.0",
"standard-version": "^9.1.1",
Expand Down
39 changes: 39 additions & 0 deletions process-backend-notification-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const isPlainObject = require('type/plain-object/is');
const coerceNaturalNumber = require('type/natural-number/coerce');
const coerceTimeValue = require('type/time-value/coerce');
const toShortString = require('type/lib/to-short-string');
const ci = require('ci-info');
const configUtils = require('./config');

const configPropertyName = 'shownNotificationsHistory';
Expand All @@ -13,6 +14,31 @@ const logError = (message) => {
process.stdout.write(`Notifications error: ${message}\n`);
};

const NOTIFICATIONS_MODE_OFF = '0';
const NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION = '1';
const NOTIFICATIONS_MODE_ON = '2';

const ALLOWED_NOTIFICATIONS_MODES = new Set([
NOTIFICATIONS_MODE_ON,
NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION,
NOTIFICATIONS_MODE_OFF,
]);

const getNotificationsMode = () => {
const modeFromEnv = process.env.SLS_NOTIFICATIONS_MODE;

if (modeFromEnv && ALLOWED_NOTIFICATIONS_MODES.has(modeFromEnv)) return modeFromEnv;

if (ci.isCI) return NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION;

return NOTIFICATIONS_MODE_ON;
};

const OUTDATED_VERSION_NOTIFICATION_CODES = new Set([
'OUTDATED_MINOR_VERSION',
'OUTDATED_MAJOR_VERSION',
]);

module.exports = (notifications) => {
if (!Array.isArray(notifications)) {
if (notifications && Array.isArray(notifications.notifications)) {
Expand All @@ -25,13 +51,16 @@ module.exports = (notifications) => {
return null;
}
}

const shownNotificationsHistory = configUtils.get(configPropertyName) || {};
Object.keys(shownNotificationsHistory).forEach((code) => {
const timeValue = coerceTimeValue(shownNotificationsHistory[code]);
if (!timeValue) delete shownNotificationsHistory[code];
else shownNotificationsHistory[code] = timeValue;
});

const notificationsMode = getNotificationsMode();

return (
notifications
.filter((notification, index) => {
Expand All @@ -49,6 +78,16 @@ module.exports = (notifications) => {
);
return false;
}

if (notificationsMode === NOTIFICATIONS_MODE_OFF) return false;

if (
notificationsMode === NOTIFICATIONS_MODE_ONLY_OUTDATED_VERSION &&
!OUTDATED_VERSION_NOTIFICATION_CODES.has(notification.code)
) {
return false;
}

notification.visibilityInterval = coerceNaturalNumber(notification.visibilityInterval);
if (notification.visibilityInterval == null) notification.visibilityInterval = 24;
return true;
Expand Down
43 changes: 42 additions & 1 deletion test/process-backend-notification-request.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
'use strict';

const { expect } = require('chai');
const processTargetNotifications = require('../process-backend-notification-request');
const overrideEnv = require('process-utils/override-env');
const wait = require('timers-ext/promise/sleep');
const proxyquire = require('proxyquire');

const processTargetNotifications = proxyquire('../process-backend-notification-request', {
'ci-info': { isCI: false },
});

const testOrderFixture = [
{ code: 'CODE12', message: 'Some notification', visibilityInterval: 12 },
Expand Down Expand Up @@ -67,4 +72,40 @@ describe('process-backend-notification-request', () => {
await validateNotificationAndEnsureClockProgress('CODE0B');
await validateNotificationAndEnsureClockProgress('CODE0C');
});

it('Should ignore all notifications if SLS_NOTIFICATIONS_MODE set to 0', async () => {
let notification;
overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '0' } }, () => {
notification = processTargetNotifications([
{ code: 'CODE123', message: 'Some notification #1' },
{ code: 'CODE456', message: 'Some notification #2' },
]);
});

expect(notification).to.be.null;
});

it('Should only consider outdated version notifs if SLS_NOTIFICATIONS_MODE set to 1', async () => {
let notification;
overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '1' } }, () => {
notification = processTargetNotifications([
{ code: 'CODE456', message: 'Some notification' },
{ code: 'OUTDATED_MINOR_VERSION', message: 'outdated' },
]);
});

expect(notification.code).to.equal('OUTDATED_MINOR_VERSION');
});

it('Should consider all notifs if SLS_NOTIFICATIONS_MODE set to 2', async () => {
let notification;
overrideEnv({ variables: { SLS_NOTIFICATIONS_MODE: '2' } }, () => {
notification = processTargetNotifications([
{ code: 'CODE123', message: 'Some notification #1' },
{ code: 'CODE456', message: 'Some notification #2' },
]);
});

expect(notification.code).to.equal('CODE123');
});
});

0 comments on commit e822033

Please sign in to comment.