var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
require('dotenv').load();
var url = require('url');

var querystring = require('querystring');

var eventTypesRaw = process.env.BAMBOO_EVENT_NOTIFIER_TYPES;

var eventTypes = [];

if (eventTypesRaw != null) {
    eventTypes = eventTypesRaw.split(',');
} else {
    console.warn("github-repo-event-notifier is not setup to receive any events (BAMBOO_EVENT_NOTIFIER_TYPES is empty).");
}

var express = require('express');
var fetch = require('node-fetch');
var FormData = require('form-data');
var router = express.Router();

router.get("/", function(req, res) {
    return res.end("GitHub Bamboo Proxy");
});

console.info("registered GET /bamboo");

router.post("/", function(req, res) {
    var auth, auto_trigger, bamboo_uri, bamboo_url, build_key, data, error, error1, eventType, pass, query, room, user;
    user = process.env.BAMBOO_USERNAME;
    pass = process.env.BAMBOO_PASSWORD;
    auth = new Buffer(user + ':' + pass).toString('base64');
    query = querystring.parse(url.parse(req.url).query);
    bamboo_url = query.bamboo;
    build_key = query.buildKey;
    room = query.room;
    auto_trigger = query.auto;
    if (!bamboo_url) {
        return res.end("");
    }
    bamboo_url = bamboo_url.replace(/\/$/, "");

    data = req.body;
    eventType = req.headers["x-github-event"];
    console.log("Processing event type " + eventType + "...");
    try {
        if (indexOf.call(eventTypes, eventType) >= 0) {
            if (auto_trigger) {
                bamboo_uri = bamboo_url + "/rest/api/latest/queue/" + build_key;


                var form = new FormData();
                form.append('bamboo.variable.pull_ref', data.pull_request.head.ref);
                form.append('bamboo.variable.pull_sha', data.pull_request.head.sha);
                form.append('bamboo.variable.pull_num', data.number);

                var headers = { Authorization: "Basic " + auth};
                console.log("Invoking " + bamboo_uri);
                console.log("Headers: " + headers);
                fetch(bamboo_uri, { method: 'POST', body: form, headers: form.getHeaders(headers) })
                    .catch(function(rejection) {
                        return console.log("Encountered an error sending to bamboo " + rejection);
                    })
                    .then(function(res) {
                        return res.text();
                    }).then(function(body) {
                    console.log(body);
                });
            }
        } else {
            console.log("Ignoring " + eventType + " event as it's not allowed.");
        }
    } catch (error1) {
        error = error1;
        console.log("github repo event notifier error: " + error + ". Request: " + req.body);
        console.log(error.stack);
    }
    console.log("Finished processing event type " + eventType);
    return res.end("");
});

module.exports = router

@@ -2003,17 +2003,38 @@
 ders = %7B
- 
+%0A                    '
 Authoriz
@@ -2038,16 +2038,17 @@
 rization
+'
 : %22Basic
@@ -2056,16 +2056,106 @@
 %22 + auth
+,%0A                    'Content-Type': 'application/x-www-form-urlencoded'%0A                
 %7D;%0A     

Include content-type