Skip to content

Commit

Permalink
[feat] Add process interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
decompil3d committed Dec 18, 2018
1 parent e30d104 commit 185f98a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 53 deletions.
107 changes: 54 additions & 53 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,67 +40,68 @@ class Processor {
return done();
}

this.app.log.info('Processing PR', { repository: data.repository.full_name, number: data.number, requestId });

this.getRepoConfig(data.repository, github, (getRepoConfigErr, config) => {
if (getRepoConfigErr) {
this.app.log.error('Error getting repository config', { error: getRepoConfigErr, requestId });
return done(getRepoConfigErr);
}

if (!config || !Array.isArray(config.plugins) || config.plugins.length === 0) {
// No config specified for this repo, nothing to do
this.app.log.info('No config specified for repo, nothing to do',
{ repository: data.repository.full_name, requestId });
return done();
}

const apis = {
commenter: new Commenter(),
github
};

async.each(config.plugins, (pluginConfig, next) => {
const pluginName = typeof pluginConfig === 'string' ? pluginConfig : pluginConfig.plugin;
const plugin = this.app.plugins[pluginName];
if (!plugin) {
this.app.log.error('Invalid plugin specified in config',
{ repository: data.repository.full_name, plugin: pluginName, requestId });
return next(new Error(`Invalid config: no plugin named '${pluginName}' exists.`));
const logData = { repository: data.repository.full_name, number: data.number, requestId };
this.app.perform('process', logData, doneProcessing => {
this.app.log.info('Processing PR', logData);

this.getRepoConfig(data.repository, github, (getRepoConfigErr, config) => {
if (getRepoConfigErr) {
this.app.log.error('Error getting repository config', { error: getRepoConfigErr, requestId });
return doneProcessing(getRepoConfigErr);
}

if (data.action === 'edited' && !plugin.processesEdits) {
return next();
if (!config || !Array.isArray(config.plugins) || config.plugins.length === 0) {
// No config specified for this repo, nothing to do
this.app.log.info('No config specified for repo, nothing to do', logData);
return doneProcessing();
}

plugin.processRequest(data, pluginConfig.config || {}, apis, pluginProcessRequestErr => {
if (pluginProcessRequestErr) {
this.app.log.error('Error running plugin',
{
error: pluginProcessRequestErr,
repository: data.repository.full_name,
number: data.number,
plugin: pluginName,
requestId
});
return next(pluginProcessRequestErr);
const apis = {
commenter: new Commenter(),
github
};

async.each(config.plugins, (pluginConfig, next) => {
const pluginName = typeof pluginConfig === 'string' ? pluginConfig : pluginConfig.plugin;
const plugin = this.app.plugins[pluginName];
if (!plugin) {
this.app.log.error('Invalid plugin specified in config',
{ repository: data.repository.full_name, plugin: pluginName, requestId });
return next(new Error(`Invalid config: no plugin named '${pluginName}' exists.`));
}
next();
});
}, eachErr => {
if (eachErr) return done(eachErr);

this.app.log.info('Finished processing PR',
{ repository: data.repository.full_name, number: data.number, requestId });
if (data.action === 'edited' && !plugin.processesEdits) {
return next();
}

const comment = apis.commenter.flushToString();
if (comment) {
github.createIssueComment(data.repository.full_name, data.pull_request.number, comment, done);
} else {
return done();
}
plugin.processRequest(data, pluginConfig.config || {}, apis, pluginProcessRequestErr => {
if (pluginProcessRequestErr) {
this.app.log.error('Error running plugin',
{
error: pluginProcessRequestErr,
repository: data.repository.full_name,
number: data.number,
plugin: pluginName,
requestId
});
return next(pluginProcessRequestErr);
}
next();
});
}, eachErr => {
if (eachErr) return doneProcessing(eachErr);

this.app.log.info('Finished processing PR', logData);

const comment = apis.commenter.flushToString();
if (comment) {
github.createIssueComment(data.repository.full_name, data.pull_request.number, comment, doneProcessing);
} else {
return doneProcessing();
}
});
});
});
}, done);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions test/integration/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function nockFile(scope, path, contents) {
describe('Pullie (integration)', function () {
let pullie;
let baseUrl;
const requestsProcessed = [];

before(function (done) {
const github = nock('https://github.test.fake')
Expand Down Expand Up @@ -64,6 +65,12 @@ describe('Pullie (integration)', function () {
baseUrl = base;
pullie = server;

// Subscribe to `process` interceptor to test it
pullie.before('process', (data, next) => {
requestsProcessed.push(data);
next();
});

done();
});
});
Expand All @@ -79,6 +86,7 @@ describe('Pullie (integration)', function () {
assume(res).hasOwn('statusCode', 200);
assume(body).hasOwn('status', 'ok');
assume(nock.isDone()).is.true();
assume(requestsProcessed.find(req => req.repository === 'org/repo' && req.number === 165)).is.truthy();

done();
});
Expand Down
5 changes: 5 additions & 0 deletions test/unit/processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ describe('Processor', () => { // eslint-disable-line max-statements
log: {
info: sandbox.stub(),
error: sandbox.stub()
},
perform: function () {
const workFn = arguments[arguments.length - 2];
const callback = arguments[arguments.length - 1];
workFn(callback);
}
};
const mockGithub = {
Expand Down

0 comments on commit 185f98a

Please sign in to comment.