'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.ExecutorTravis = undefined;

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

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

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

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

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

var _travisCi = require('travis-ci');

var _travisCi2 = _interopRequireDefault(_travisCi);

var _Executor2 = require('../agent/Executor');

var _formatJson = require('format-json');

var _formatJson2 = _interopRequireDefault(_formatJson);

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

/**
 * Sample .githubTaskManager.json task config - NOT READY FOR USE
 *
 * see: https://github.com/zotoio/github-task-manager/wiki/Structure-of-.githubTaskManager.json
 *
 {
   "executor": "Travis",
   "context": "release",
   "options": {
     "target": "npm"
   }
 }
 */

var ExecutorTravis = exports.ExecutorTravis = function (_Executor) {
    (0, _inherits3.default)(ExecutorTravis, _Executor);

    function ExecutorTravis(eventData, log) {
        (0, _classCallCheck3.default)(this, ExecutorTravis);

        var _this = (0, _possibleConstructorReturn3.default)(this, (ExecutorTravis.__proto__ || (0, _getPrototypeOf2.default)(ExecutorTravis)).call(this, eventData, log));

        _this.log = log;
        _this.options = _this.getOptions();

        _this.travis = new _travisCi2.default({
            version: '2.0.0'
        });
        return _this;
    }

    (0, _createClass3.default)(ExecutorTravis, [{
        key: 'executeTask',
        value: function () {
            var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(task) {
                var log, result;
                return _regenerator2.default.wrap(function _callee$(_context) {
                    while (1) {
                        switch (_context.prev = _context.next) {
                            case 0:
                                log = this.log;

                                log.info('travis options: ' + _formatJson2.default.plain(task.options));

                                this.travis.authenticate({
                                    github_token: process.env.GTM_GITHUB_TOKEN
                                }, function (err) {
                                    if (err) {
                                        log.error(err);
                                        return;
                                    }

                                    log.info('logged in to travis');
                                });

                                result = true;

                                log.info('Build Finished: ' + result);

                                task.results = { passed: result, url: 'https://travis-ci.org' };
                                return _context.abrupt('return', result ? _promise2.default.resolve(task) : _promise2.default.reject(task));

                            case 7:
                            case 'end':
                                return _context.stop();
                        }
                    }
                }, _callee, this);
            }));

            function executeTask(_x) {
                return _ref.apply(this, arguments);
            }

            return executeTask;
        }()
    }]);
    return ExecutorTravis;
}(_Executor2.Executor);

_Executor2.Executor.register('Travis', ExecutorTravis);