Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(1769): [3] Modify webhook plugin to use queue webhook #2627

Merged
merged 5 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bin/server
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ const notificationConfig = config.get('notifications');
// Multiple build cluster feature flag
const multiBuildClusterEnabled = convertToBool(config.get('multiBuildCluster').enabled);

// Queue Webhook feature flag
const queueWebhookEnabled = convertToBool(config.get('queueWebhook').enabled);

// Default cluster environment variable
const clusterEnvConfig = config.get('build').environment; // readonly
const clusterEnv = { ...clusterEnvConfig };
Expand Down Expand Up @@ -254,6 +257,10 @@ datastore.setup(datastoreConfig.ddlSyncEnabled).then(() =>
validator: {
externalJoin: true,
notificationsValidationErr
},
queueWebhook: {
executor,
queueWebhookEnabled
}
})
.then(instance => logger.info('Server running at %s', instance.info.uri))
Expand Down
5 changes: 5 additions & 0 deletions config/custom-environment-variables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ executor:
tls: QUEUE_REDIS_TLS_ENABLED
database: QUEUE_REDIS_DATABASE


queueWebhook:
# Enabled events from webhook queue or not
enabled: QUEUE_WEBHOOK_ENABLED

scms:
__name: SCM_SETTINGS
__format: json
Expand Down
4 changes: 4 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ executor:
tls: false
database: 0

queueWebhook:
# Enabled events from webhook queue or not
enabled: false

scms: {}
# github:
# plugin: github
Expand Down
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ module.exports = async config => {
collectionFactory: config.collectionFactory,
buildClusterFactory: config.buildClusterFactory,
ecosystem: config.ecosystem,
release: config.release
release: config.release,
queueWebhook: config.queueWebhook
};

const bellConfigs = await config.auth.scm.getBellConfiguration();
Expand Down
22 changes: 20 additions & 2 deletions plugins/webhooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ const webhooksPlugin = {
maxBytes: parseInt(pluginOptions.maxBytes, 10) || DEFAULT_MAX_BYTES
},
handler: async (request, h) => {
const { pipelineFactory } = request.server.app;
const { pipelineFactory, queueWebhook } = request.server.app;
const { scm } = pipelineFactory;
const { executor, queueWebhookEnabled } = queueWebhook;
let message = 'Unable to process this kind of event';
let hookId;
yk634 marked this conversation as resolved.
Show resolved Hide resolved

try {
const parsed = await scm.parseHook(request.headers, request.payload);
Expand All @@ -76,10 +78,26 @@ const webhooksPlugin = {

parsed.pluginOptions = pluginOptions;

const { type, hookId } = parsed;
const { type } = parsed;
hookId = parsed.hookId;

request.log(['webhook', hookId], `Received event type ${type}`);

if (queueWebhookEnabled) {
yk634 marked this conversation as resolved.
Show resolved Hide resolved
parsed.token = request.server.plugins.auth.generateToken({
scope: ['sdapi']
});

try {
return await executor.enqueueWebhook(parsed);
} catch (err) {
// if enqueueWebhook is not implemented, an event starts without enqueuing
if (err.message != 'Not implemented') {
throw err;
}
}
}

return await startHookEvent(request, h, parsed);

} catch (err) {
Expand Down
47 changes: 43 additions & 4 deletions test/plugins/webhooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('webhooks plugin test', () => {
let pipelineFactoryMock;
let userFactoryMock;
let eventFactoryMock;
let queueWebhookMock;
let plugin;
let server;
const apiUri = 'http://foo.bar:12345';
Expand Down Expand Up @@ -72,6 +73,12 @@ describe('webhooks plugin test', () => {
},
create: sinon.stub()
};
queueWebhookMock = {
executor: {
enqueueWebhook: sinon.stub()
},
queueWebhookEnabled: false
}

plugin = rewire('../../plugins/webhooks');

Expand All @@ -85,8 +92,14 @@ describe('webhooks plugin test', () => {
buildFactory: buildFactoryMock,
pipelineFactory: pipelineFactoryMock,
userFactory: userFactoryMock,
eventFactory: eventFactoryMock
eventFactory: eventFactoryMock,
queueWebhook: queueWebhookMock
};
server.plugins = {
auth: {
generateToken: () => 'iamtoken'
}
}

await server.register({
plugin,
Expand Down Expand Up @@ -132,7 +145,8 @@ describe('webhooks plugin test', () => {
buildFactory: buildFactoryMock,
pipelineFactory: pipelineFactoryMock,
userFactory: userFactoryMock,
eventFactory: eventFactoryMock
eventFactory: eventFactoryMock,
queueWebhook: queueWebhookMock
};

assert.isRejected(
Expand Down Expand Up @@ -1334,6 +1348,29 @@ describe('webhooks plugin test', () => {
});
});


it('calls enqueueWebhook with webhookConfig when queueWebhookEnabled is true', () => {
queueWebhookMock.queueWebhookEnabled = true;
queueWebhookMock.executor.enqueueWebhook.resolves(null);

return server.inject(options).then(() => {
assert.calledWith(queueWebhookMock.executor.enqueueWebhook, {
...parsed,
token: 'iamtoken'
});
});
});

it('returns 201 when executor.enqueueWebhook is not implemented', () => {
const err = new Error('Not implemented');
queueWebhookMock.queueWebhookEnabled = true;
queueWebhookMock.executor.enqueueWebhook.rejects(err);

return server.inject(options).then(reply => {
assert.equal(reply.statusCode, 201);
});
});

it('returns 500 when failed', () => {
eventFactoryMock.create.rejects(new Error('Failed to start'));

Expand Down Expand Up @@ -1747,7 +1784,8 @@ describe('webhooks plugin test', () => {
buildFactory: buildFactoryMock,
pipelineFactory: pipelineFactoryMock,
userFactory: userFactoryMock,
eventFactory: eventFactoryMock
eventFactory: eventFactoryMock,
queueWebhook: queueWebhookMock
};

testServer.register([
Expand Down Expand Up @@ -2289,7 +2327,8 @@ describe('webhooks plugin test', () => {
buildFactory: buildFactoryMock,
pipelineFactory: pipelineFactoryMock,
userFactory: userFactoryMock,
eventFactory: eventFactoryMock
eventFactory: eventFactoryMock,
queueWebhook: queueWebhookMock
};

testServer.register([
Expand Down