Skip to content

Commit

Permalink
fix: add sendWebhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
wahapo committed Dec 9, 2021
1 parent 31ad5cf commit a35c512
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
25 changes: 24 additions & 1 deletion plugins/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,34 @@ async function updateBuild(updateConfig, retryStrategyFn) {
);
}

/**
* Execute the process received from the webhook
* @method processHooks
* @param {String} apiUri
* @param {String} token
* @param {String} webhookConfig as JSON format
* @param {Function} retryStrategyFn
* @return {Promise} response or error
*/
async function processHooks(apiUri, token, webhookConfig, retryStrategyFn) {
return request(formatOptions('POST', `${apiUri}/v4/processHooks`, token, webhookConfig, retryStrategyFn)).then(
res => {
logger.info(`POST /v4/processHooks completed, ${res.statusCode}, ${JSON.stringify(res.body)}`);
if ([200, 201, 204].includes(res.statusCode)) {
return res;
}

throw new Error(`Failed to process webhook with ${res.statusCode} code and ${res.body}`);
}
);
}

module.exports = {
updateBuildStatus,
updateStepStop,
getCurrentStep,
createBuildEvent,
getPipelineAdmin,
updateBuild
updateBuild,
processHooks
};
26 changes: 26 additions & 0 deletions plugins/worker/lib/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const hoek = require('@hapi/hoek');
const ExecutorRouter = require('screwdriver-executor-router');
const logger = require('screwdriver-logger');
const AWSProducer = require('screwdriver-aws-producer-service');
const helper = require('../../helper');
const { BlockedBy } = require('./BlockedBy');
const { Filter } = require('./Filter');
const { CacheFilter } = require('./CacheFilter');
Expand Down Expand Up @@ -273,6 +274,24 @@ async function clear(cacheConfig) {
return null;
}

/**
* Send message to processHooks API
* @param {Object} webhookConfig
*/
async function sendWebhook(webhookConfig) {
const parsedConfig = JSON.parse(webhookConfig);
const apiUri = ecosystem.api;
const token = executor.tokenGen({
service: 'queue',
scope: ['webhook_worker']
});
const retryFn = null; // executor.requestRetryStrategyPostEvent;

await helper.processHooks(apiUri, token, parsedConfig, retryFn);

return null;
}

module.exports = {
start: {
plugins: [Filter, 'Retry', BlockedBy],
Expand All @@ -295,5 +314,12 @@ module.exports = {
Retry: retryOptions
},
perform: clear
},
sendWebhook: {
plugins: ['Retry'],
pluginOptions: {
Retry: retryOptions
},
perform: sendWebhook
}
};
6 changes: 6 additions & 0 deletions test/plugins/worker/lib/jobs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ describe('Jobs Unit Test', () => {
let mockConfig;
let mockProducerSvc;
let mockEcosystemConfig;
let helperMock;

before(() => {
mockery.enable({
Expand Down Expand Up @@ -154,6 +155,10 @@ describe('Jobs Unit Test', () => {
sendMessage: sinon.stub().resolves({})
};

helperMock = {
processHooks: sinon.stub()
};

mockery.registerMock('config', mockConfig);
mockery.registerMock('screwdriver-executor-router', mockExecutorRouter);
mockery.registerMock('amqp-connection-manager', mockAmqp);
Expand All @@ -162,6 +167,7 @@ describe('Jobs Unit Test', () => {
mockery.registerMock('ioredis', mockRedis);
mockery.registerMock('../../../config/rabbitmq', mockRabbitmqConfig);
mockery.registerMock('../../../config/redis', mockRedisConfig);
mockery.registerMock('../../helper', helperMock);
mockery.registerMock('screwdriver-aws-producer-service', mockProducerSvc);

mockBlockedBy = {
Expand Down

0 comments on commit a35c512

Please sign in to comment.