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

Don't react to PR comments #110

Merged
merged 1 commit into from
Nov 27, 2019
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
44 changes: 8 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ var express = require("express"),
labelModel = require('./lib/label-model'),
metadata = require('./lib/metadata'),
comment = require('./lib/comment'),
github = require('./lib/github'),
checkRequest = require('./lib/check-request'),
filter = require('./lib/filter'),
q = require('q');

function promise(value) {
var deferred = q.defer();
deferred.resolve(value);
return deferred.promise;
}

function waitFor(ms) {
var deferred = q.defer();
setTimeout(function() { deferred.resolve(); }, ms);
Expand All @@ -43,13 +36,6 @@ function funkLogErr(num, msg) {
return function(err) { logArgs("#" + num + ": " + msg + "\n", err); };
}

function getPullRequest(n, body) {
if (body.pull_request) {
return promise(body.pull_request);
}
return github.get("/repos/:owner/:repo/pulls/:number", { number: n });
}

var currentlyRunning = {};

app.post('/github-hook', function (req, res) {
Expand All @@ -59,7 +45,6 @@ app.post('/github-hook', function (req, res) {
} else if (process.env.NODE_ENV != 'production' || checkRequest(body, req.headers["x-hub-signature"], process.env.GITHUB_SECRET)) {
res.send(new Date().toISOString());

// FILTER ALL THE THINGS
try {
body = JSON.parse(body);
} catch(e) {
Expand All @@ -68,38 +53,25 @@ app.post('/github-hook', function (req, res) {
if (!filter.event(body, logArgs)) {
return;
}
if (!body.pull_request && (body.issue && !body.issue.pull_request)) {
logArgs("Ignoring event: not a pull request");
return;
}
if (body.pull_request && !filter.pullRequest(body.pull_request, logArgs)) {
if (!filter.pullRequest(body.pull_request, logArgs)) {
return;
}
// Note: `filter.pullRequest` is checked again after a timeout.
// END FILTERNG //

var action = body.action;
var isComment = !!body.comment;
var issue = body.pull_request || body.issue;
var n = issue.number;
var u = (issue.user && issue.user.login) || null;
var content = issue.body || "";
var pr = body.pull_request;
var n = pr.number;
var u = (pr.user && pr.user.login) || null;
var content = pr.body || "";
if (action == "opened" || action == "synchronize" ||
action == "ready_for_review" ||
(isComment && action == "created")) {
action == "ready_for_review") {
if (n in currentlyRunning) {
logArgs("#" + n + " is already being processed.");
return;
}
currentlyRunning[n] = true;
logArgs("#" + n, isComment ? "comment" : "pull request", action);
logArgs("#" + n, action);

waitFor(5 * 1000).then(function() { // Avoid race condition
return getPullRequest(n, body);
}).then(function(pull_request) {
if (!filter.pullRequest(pull_request, logArgs)) {
return;
}
return metadata(n, u, content).then(function(metadata) {
logArgs(metadata);
return labelModel.post(n, metadata.labels).then(
Expand All @@ -119,7 +91,7 @@ app.post('/github-hook', function (req, res) {
funkLogErr(n, "THIS SHOULDN'T EVER HAPPEN")(err);
});
} else {
logArgs("#" + n + ": not handled.", "action:", action, "isComment:", isComment);
logArgs("#" + n + ": not handled.", "action:", action);
}
} else {
logArgs("Unverified request", req);
Expand Down
4 changes: 4 additions & 0 deletions lib/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function filterEvent(body, log) {
log("Ignoring event: no body");
return false;
}
if (!body.pull_request) {
log("Ignoring event: not a pull request");
return false;
}
if (body.sender && body.sender.login == "wpt-pr-bot") {
log("Ignoring event: sender is wpt-pr-bot");
return false;
Expand Down
7 changes: 6 additions & 1 deletion test/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ suite('Event filtering', function() {
assert.strictEqual(filter.event(null, nullLog), false);
});
test('empty body', function() {
assert.strictEqual(filter.event({}, nullLog), true);
assert.strictEqual(filter.event({}, nullLog), false);
});
test('pull request', function() {
assert.strictEqual(filter.event({pull_request: {}}, nullLog), true);
});
test('wpt-pr-bot sender', function() {
assert.strictEqual(filter.event({
pull_request: {},
sender: {
login: 'wpt-pr-bot'
}
}, nullLog), false);
});
test('other sender', function() {
assert.strictEqual(filter.event({
pull_request: {},
sender: {
login: 'some-other-user'
}
Expand Down