'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var listener = function () {
    var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(event, context, callback) {
        var githubEvent, githubSignature, err, eventBody, response;
        return _regenerator2.default.wrap(function _callee$(_context) {
            while (1) {
                switch (_context.prev = _context.next) {
                    case 0:
                        githubEvent = event.headers['X-GitHub-Event'] || event.headers['x-github-event'];
                        githubSignature = event.headers['X-Hub-Signature'] || event.headers['x-hub-signature'];
                        err = githubUtils.invalidHook(event);

                        if (!err) {
                            _context.next = 5;
                            break;
                        }

                        return _context.abrupt('return', callback(err, {
                            statusCode: 401,
                            headers: { 'Content-Type': 'text/plain' },
                            body: err.message
                        }));

                    case 5:

                        // decode and parse event body
                        eventBody = decodeEventBody(event);

                        /* eslint-disable */

                        console.log('---------------------------------');
                        console.log('Github-Event: "' + githubEvent + '"');
                        console.log('---------------------------------');
                        console.log('Payload', json.plain(eventBody));
                        /* eslint-enable */

                        response = void 0;
                        _context.prev = 11;
                        _context.next = 14;
                        return handleEvent(githubEvent, eventBody, githubSignature);

                    case 14:
                        response = {
                            statusCode: 200,
                            body: (0, _stringify2.default)({
                                input: event
                            })
                        };
                        _context.next = 21;
                        break;

                    case 17:
                        _context.prev = 17;
                        _context.t0 = _context['catch'](11);

                        err = _context.t0;
                        response = {
                            statusCode: 400,
                            headers: { 'Content-Type': 'text/plain' },
                            body: err.message
                        };

                    case 21:
                        return _context.abrupt('return', callback(err, response));

                    case 22:
                    case 'end':
                        return _context.stop();
                }
            }
        }, _callee, this, [[11, 17]]);
    }));

    return function listener(_x, _x2, _x3) {
        return _ref.apply(this, arguments);
    };
}();

var handleEvent = function () {
    var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(type, body, signature) {
        var taskConfig, producer, bodyString, ghEventId, ghAgentGroup, event;
        return _regenerator2.default.wrap(function _callee2$(_context2) {
            while (1) {
                switch (_context2.prev = _context2.next) {
                    case 0:
                        _context2.next = 2;
                        return getTaskConfig(type, body);

                    case 2:
                        taskConfig = _context2.sent;

                        if (taskConfig) {
                            _context2.next = 5;
                            break;
                        }

                        throw new Error('No task config for event type ' + type);

                    case 5:
                        console.log(json.plain(taskConfig));

                        // add event body to SQS
                        console.log('adding event to SQS: ' + process.env.SQS_PENDING_QUEUE_URL);

                        // create simple producer
                        producer = Producer.create({
                            queueUrl: process.env.SQS_PENDING_QUEUE_URL,
                            region: process.env.GTM_AWS_REGION
                        });
                        bodyString = (0, _stringify2.default)(body);
                        ghEventId = UUID();
                        ghAgentGroup = taskConfig[type] && taskConfig[type].agentGroup ? taskConfig[type].agentGroup : 'default';
                        event = [{
                            id: ghEventId,
                            body: bodyString,
                            messageAttributes: {
                                ghEventId: { DataType: 'String', StringValue: ghEventId },
                                ghEventType: { DataType: 'String', StringValue: type },
                                ghTaskConfig: {
                                    DataType: 'String',
                                    StringValue: json.plain(taskConfig)
                                },
                                ghAgentGroup: { DataType: 'String', StringValue: ghAgentGroup }
                            }
                        }];


                        signature = githubUtils.signRequestBody(process.env.GTM_GITHUB_WEBHOOK_SECRET, (0, _stringify2.default)(event[0]));

                        event[0].messageAttributes.ghEventSignature = {
                            DataType: 'String',
                            StringValue: signature
                        };

                        console.log('Outgoing event payload: ' + json.plain(event));

                        producer.send(event, function (err) {
                            if (err) console.log(err);
                        });

                        // set pull request status to pending for each task
                        if (type === 'pull_request') {
                            setPullRequestEventStatus(ghEventId, body);
                        }

                    case 17:
                    case 'end':
                        return _context2.stop();
                }
            }
        }, _callee2, this);
    }));

    return function handleEvent(_x4, _x5, _x6) {
        return _ref2.apply(this, arguments);
    };
}();

var getTaskConfig = function () {
    var _ref3 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3(type, body) {
        var fileParams, fileResponse, taskConfig;
        return _regenerator2.default.wrap(function _callee3$(_context3) {
            while (1) {
                switch (_context3.prev = _context3.next) {
                    case 0:
                        fileParams = getFileParams(type, body);


                        console.log('file request params for ' + type + ' = ' + json.plain(fileParams));

                        _context3.next = 4;
                        return githubUtils.getFile(fileParams).catch(function () {
                            return rp({
                                proxy: process.env.https_proxy || process.env.http_proxy || null,
                                json: true,
                                uri: process.env.GTM_TASK_CONFIG_DEFAULT_URL || 'https://raw.githubusercontent.com/zotoio/github-task-manager/master/.githubTaskManager.json'
                            }).then(function (config) {
                                config.pull_request.isDefaultConfig = true;
                                return {
                                    data: {
                                        content: Buffer.from((0, _stringify2.default)(config)).toString('base64')
                                    }
                                };
                            });
                        });

                    case 4:
                        fileResponse = _context3.sent;
                        taskConfig = githubUtils.decodeFileResponse(fileResponse);

                        if (!(!taskConfig[type] || !taskConfig[type].tasks || !taskConfig[type].tasks.length > 0)) {
                            _context3.next = 9;
                            break;
                        }

                        console.error('repository config not found for event type \'' + type + '\' in config ' + json.plain(taskConfig));
                        return _context3.abrupt('return', false);

                    case 9:

                        console.log('task config for ' + type + ' = ' + json.plain(taskConfig[type]));

                        return _context3.abrupt('return', taskConfig);

                    case 11:
                    case 'end':
                        return _context3.stop();
                }
            }
        }, _callee3, this);
    }));

    return function getTaskConfig(_x7, _x8) {
        return _ref3.apply(this, arguments);
    };
}();

require('source-map-support/register');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

require('source-map-support').install();
var rp = require('request-promise-native');
var json = require('format-json');
var UUID = require('uuid/v4');
var Producer = require('sqs-producer');

var githubUtils = require('../gtmGithubUtils.js');

function getFileParams(type, body) {
    switch (type) {
        case 'pull_request':
            return {
                owner: body.pull_request.head.repo.owner.login,
                repo: body.pull_request.head.repo.name,
                path: process.env.GTM_TASK_CONFIG_FILENAME || '.githubTaskManager.json',
                ref: body.pull_request.head.ref
            };
        case 'push':
            return {
                owner: body.repository.owner.login,
                repo: body.repository.name,
                path: process.env.GTM_TASK_CONFIG_FILENAME || '.githubTaskManager.json',
                ref: body.ref
            };
        default:
            return {
                owner: body.repository.owner.login,
                repo: body.repository.name,
                path: process.env.GTM_TASK_CONFIG_FILENAME || '.githubTaskManager.json',
                ref: 'master'
            };
    }
}

function decodeEventBody(event) {
    return JSON.parse(decodeURIComponent(event.body.replace(/\+/g, ' ')).replace('payload={', '{'));
}

function setPullRequestEventStatus(ghEventId, eventBody) {
    var url = void 0;
    if (process.env.GTM_ELASTIC_HOST && process.env.GTM_ELASTIC_PORT) {
        var baseUrl = process.env.GTM_BASE_URL || 'http://localhost:9091';
        url = baseUrl + '/metrics/log/gtm-' + ghEventId + '.txt';
    }

    var status = githubUtils.createPullRequestStatus(eventBody, 'pending', 'GitHub Task Manager', 'Queued ' + ghEventId, url);
    githubUtils.updateGitHubPullRequestStatus(status, function () {});
}

module.exports = {
    listener: listener,
    getTaskConfig: getTaskConfig,
    decodeEventBody: decodeEventBody,
    handleEvent: handleEvent
};