/**
    Webhook listener.
*/
"use strict";
const GitHubApi = require("github");
const program = require("commander");
const http = require('http');
const main = require('./pull_request');
const seqqueue = require('seq-queue');

const config = require('./config');

const taskQueue = seqqueue.createQueue(30000);


var addToQueue = (() => {
    let isRunning = false;
    let queue = [];
    
    const processNext = (k) => {
        const first = queue.shift();
        isRunning = true;
        first(() => {
            isRunning = false;
            k();
        });
    };
    
    const process = () => {
        if (!isRunning && queue.length > 0)
            processNext(process);
    };
    
    return (k) => {
        queue.push(k);
        process();
    };
})();

program
    .version('0.0.0')
    .option('--port <port>', 'Port to listen on.')

    .option('--token <token>', 'Github user token')
    .option('--secret <secret>', 'Github hook secret')
    .parse(process.argv);

const port = (program.port || 3000);

const github = new GitHubApi({
	version: "3.0.0",
	headers: {
	    "user-agent": "80x40-client"
	}
});

github.authenticate({
    type: "oauth",
    token: program.token
});

const webhookHandler = require('github-webhook-handler')({
    path: '/',
    secret: program.secret
});

webhookHandler.on('pull_request', (event) => {
    const action = event.payload.action;
    if (action !== 'opened' && action !== 'synchronize') {
        console.log('ignoring action', action);
        return;
    }
    taskQueue.push((task) => {
        main.handlePullRequest(github, config.user, config.repo,
            event.payload.number,
            (err) => {
                if (err) {
                    console.error("ERROR", err);
                } else {
                    console.log("OK");
                }
                task.done();
            })
        },
        () => {
            console.error("Timeout");
        });
});

console.log("Listening for webhook events on port", port);
http.createServer(function (req, res) {
    webhookHandler(req, res, (err) => {
        res.statusCode = 404
        res.end('');
    });
}).listen(port);

@@ -262,515 +262,142 @@
 );%0A%0A
-const taskQueue = seqqueue.createQueue(30000);%0A%0A%0Avar addToQueue = (() =%3E %7B%0A    let isRunning = false;%0A    let queue = %5B%5D;%0A    %0A    const processNext = (k) =%3E %7B%0A        const first = queue.shift();%0A        isRunning = true;%0A        first(() =%3E %7B%0A            isRunning = false;%0A            k();%0A        %7D);%0A    %7D;%0A    %0A    const process = () =%3E %7B%0A        if (!isRunning && queue.length %3E 0)%0A            processNext(process);%0A    %7D;%0A    %0A    return (k) =%3E %7B%0A        queue.push(k);%0A        process();%0A    %7D;%0A%7D)(
+// Pull request actions to process%0Aconst pullRequestActions = %5B'opened', 'synchronize'%5D;%0A%0Aconst taskQueue = seqqueue.createQueue(30000
 );%0A%0A
@@ -1031,55 +1031,49 @@
 if (
-action !== 'opened' && action !== 'synchronize'
+pullRequestActions.indexOf(action) === -1
 ) %7B%0A

Delete dead code and move pull request actions to constant