Skip to content

Commit 3ce04e1

Browse files
authoredFeb 8, 2021
feat(2339): [3] Add feature flag for throwing notifications validation errs (#2340)
* feat(2339): Add feature flag for throwing notifications validation errs * fix: validator test
1 parent ea8bb9a commit 3ce04e1

File tree

6 files changed

+69
-50
lines changed

6 files changed

+69
-50
lines changed
 

‎bin/server

+36-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
/* eslint-disable import/no-dynamic-require */
66
const config = require('config');
7-
const server = require('../');
87
const hoek = require('@hapi/hoek');
98
const { Bookend } = require('screwdriver-build-bookend');
109
const logger = require('screwdriver-logger');
@@ -53,19 +52,21 @@ const multiBuildClusterEnabled = convertToBool(config.get('multiBuildCluster').e
5352

5453
// Default cluster environment variable
5554
const clusterEnvConfig = config.get('build').environment; // readonly
56-
const clusterEnv = Object.assign({}, clusterEnvConfig);
55+
const clusterEnv = { ...clusterEnvConfig };
5756

58-
Object.keys(clusterEnv).forEach((k) => {
57+
Object.keys(clusterEnv).forEach(k => {
5958
clusterEnv[k] = String(clusterEnv[k]);
6059
});
6160

62-
const externalJoin = config.get('build').externalJoin; // flag to allow external join
61+
const { externalJoin } = config.get('build'); // flag to allow external join
62+
63+
const notificationsValidationErr = hoek.reach(config.get('notifications').options, 'throwValidationErr'); // flag to throw notifications validation err or not
6364

6465
// Setup Datastore
6566
const datastoreConfig = config.get('datastore');
6667
const DatastorePlugin = require(`screwdriver-datastore-${datastoreConfig.plugin}`);
6768

68-
const datastorePluginConfig = Object.assign({}, datastoreConfig[datastoreConfig.plugin]);
69+
const datastorePluginConfig = { ...datastoreConfig[datastoreConfig.plugin] };
6970

7071
// Readonly Datastore
7172
const datastoreROConfig = datastorePluginConfig.readOnly;
@@ -77,8 +78,7 @@ if (datastoreROConfig && Object.keys(datastoreROConfig).length > 0) {
7778
delete datastorePluginConfig.readOnly;
7879

7980
// Default datastore
80-
const datastore = new DatastorePlugin(hoek.applyToDefaults({ ecosystem },
81-
(datastorePluginConfig || {})));
81+
const datastore = new DatastorePlugin(hoek.applyToDefaults({ ecosystem }, datastorePluginConfig || {}));
8282

8383
// Source Code Plugin
8484
const scmConfig = { scms: config.get('scms') };
@@ -102,22 +102,25 @@ const coverage = new CoveragePlugin(coverageConfig);
102102

103103
// Plugins to run before/after a build
104104
const bookends = config.get('bookends');
105-
const bookend = new Bookend({
106-
scm,
107-
'screwdriver-artifact-bookend': artifact,
108-
'screwdriver-coverage-bookend': coverage,
109-
'screwdriver-cache-bookend': cache
110-
},
105+
const bookend = new Bookend(
106+
{
107+
scm,
108+
'screwdriver-artifact-bookend': artifact,
109+
'screwdriver-coverage-bookend': coverage,
110+
'screwdriver-cache-bookend': cache
111+
},
111112
bookends.setup || [], // plugins required for the setup- steps
112113
bookends.teardown || [] // plugins required for the teardown-steps
113114
);
114115

115116
// Setup Pipeline Factory for Executor
116117
const Models = require('screwdriver-models');
118+
const server = require('..');
117119
const pipelineFactory = Models.PipelineFactory.getInstance({
118120
datastore,
119121
scm,
120122
externalJoin,
123+
notificationsValidationErr,
121124
multiBuildClusterEnabled
122125
});
123126

@@ -126,10 +129,10 @@ const executorConfig = config.get('executor');
126129

127130
executorConfig[executorConfig.plugin].options.pipelineFactory = pipelineFactory;
128131
const ExecutorPlugin = require(`screwdriver-executor-${executorConfig.plugin}`);
129-
const executor = new ExecutorPlugin(Object.assign(
130-
{ ecosystem: hoek.clone(ecosystem) },
131-
executorConfig[executorConfig.plugin].options
132-
));
132+
const executor = new ExecutorPlugin({
133+
ecosystem: hoek.clone(ecosystem),
134+
...executorConfig[executorConfig.plugin].options
135+
});
133136

134137
// Setup Model Factories
135138
const commandFactory = Models.CommandFactory.getInstance({
@@ -210,8 +213,8 @@ const buildClusterFactory = Models.BuildClusterFactory.getInstance({
210213

211214
// @TODO run setup for SCM and Executor
212215
// datastoreConfig.ddlSync => sync datastore schema (ddl) via api (default: true)
213-
datastore.setup(datastoreConfig.ddlSyncEnabled)
214-
.then(() => server({
216+
datastore.setup(datastoreConfig.ddlSyncEnabled).then(() =>
217+
server({
215218
httpd: httpdConfig,
216219
auth: authConfig,
217220
webhooks: webhooksConfig,
@@ -242,12 +245,18 @@ datastore.setup(datastoreConfig.ddlSyncEnabled)
242245
externalJoin
243246
},
244247
stats: {
245-
executor, scm
248+
executor,
249+
scm
246250
},
247-
release
248-
}))
249-
.then(instance => logger.info('Server running at %s', instance.info.uri))
250-
.catch((err) => {
251-
logger.error(err);
252-
process.exit(1);
253-
});
251+
release,
252+
validator: {
253+
externalJoin,
254+
notificationsValidationErr
255+
}
256+
})
257+
.then(instance => logger.info('Server running at %s', instance.info.uri))
258+
.catch(err => {
259+
logger.error(err);
260+
process.exit(1);
261+
})
262+
);

‎config/default.yaml

+6-3
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,17 @@ bookends:
268268
- screwdriver-cache-bookend
269269

270270
notifications:
271-
# Email notification when a build finishes
271+
# options:
272+
# # Throw error when validation fails (default true); otherwise show warning
273+
# throwValidationErr: true
274+
# # Email notification when a build finishes
272275
# email:
273276
# host: email-host
274277
# port: email-port
275278
# from: email-address-to-send-from
276279
# username: optional-username
277280
# password: optional-password
278-
# Slack notification when build finishes
281+
# # Slack notification when build finishes
279282
# slack:
280283
# token: your-slack-bot-token
281284
ecosystem:
@@ -301,7 +304,7 @@ ecosystem:
301304
max_go_threads: 10000
302305

303306
# environment release information
304-
release:
307+
release:
305308
mode: stable
306309
cookieName: release
307310
cookieValue: stable

‎lib/registerPlugins.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,20 @@ function registerNotificationEvent(config, server) {
137137
const notificationConfig = config.notifications || {};
138138

139139
Object.keys(notificationConfig).forEach(plugin => {
140-
const Plugin = requireNotificationPlugin(notificationConfig[plugin], plugin);
141-
let notificationPlugin;
140+
if (plugin !== 'options') {
141+
const Plugin = requireNotificationPlugin(notificationConfig[plugin], plugin);
142+
let notificationPlugin;
143+
144+
if (notificationConfig[plugin].config) {
145+
notificationPlugin = new Plugin(notificationConfig[plugin].config);
146+
} else {
147+
notificationPlugin = new Plugin(notificationConfig[plugin]);
148+
}
142149

143-
if (notificationConfig[plugin].config) {
144-
notificationPlugin = new Plugin(notificationConfig[plugin].config);
145-
} else {
146-
notificationPlugin = new Plugin(notificationConfig[plugin]);
150+
notificationPlugin.events.forEach(event => {
151+
server.events.on(event, buildData => notificationPlugin.notify(buildData));
152+
});
147153
}
148-
149-
notificationPlugin.events.forEach(event => {
150-
server.events.on(event, buildData => notificationPlugin.notify(buildData));
151-
});
152154
});
153155
}
154156

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"screwdriver-build-bookend": "^2.3.2",
101101
"screwdriver-cache-bookend": "^2.0.0",
102102
"screwdriver-command-validator": "^2.0.0",
103-
"screwdriver-config-parser": "^6.3.0",
103+
"screwdriver-config-parser": "^7.0.0",
104104
"screwdriver-coverage-bookend": "^1.0.3",
105105
"screwdriver-coverage-sonar": "^3.0.0",
106106
"screwdriver-data-schema": "^21.0.0",

‎plugins/stats.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const schema = require('screwdriver-data-schema');
1212
const statsPlugin = {
1313
name: 'stats',
1414
async register(server, options) {
15-
const { executor } = options;
16-
const { scm } = options;
15+
const { executor, scm } = options;
1716

1817
server.route({
1918
method: 'GET',

‎plugins/validator.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const validatorSchema = schema.api.validator;
1414
*/
1515
const validatorTemplate = {
1616
name: 'validator',
17-
async register(server) {
17+
async register(server, options) {
1818
server.route({
1919
method: 'POST',
2020
path: '/validator',
@@ -27,12 +27,18 @@ const validatorTemplate = {
2727
enabled: false
2828
}
2929
},
30-
handler: async (request, h) =>
31-
parser(
32-
request.payload.yaml,
33-
request.server.app.templateFactory,
34-
request.server.app.buildClusterFactory
35-
).then(pipeline => h.response(pipeline)),
30+
handler: async (request, h) => {
31+
const { notificationsValidationErr } = options;
32+
const { buildClusterFactory, templateFactory } = request.server.app;
33+
34+
// TODO: Handle externalJoin case (pass in triggerFactory and pipelineId)
35+
return parser({
36+
yaml: request.payload.yaml,
37+
templateFactory,
38+
buildClusterFactory,
39+
notificationsValidationErr
40+
}).then(pipeline => h.response(pipeline));
41+
},
3642
validate: {
3743
payload: validatorSchema.input
3844
},

0 commit comments

Comments
 (0)
Please sign in to comment.