'use strict';

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

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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 _get2 = require('babel-runtime/helpers/get');

var _get3 = _interopRequireDefault(_get2);

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

var _inherits3 = _interopRequireDefault(_inherits2);

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

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

var _ExecutorDocker2 = require('./ExecutorDocker');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _AgentUtils = require('../agent/AgentUtils');

var _KmsUtils = require('../KmsUtils');

var _KmsUtils2 = _interopRequireDefault(_KmsUtils);

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

/**
 * Sample .githubTaskManager.json task config
 *
 * see: https://github.com/zotoio/github-task-manager/wiki/Structure-of-.githubTaskManager.json
 *
{
  "pull_request": {
    "agentGroup": "K8S",
    "tasks": [
      {
        "executor": "DockerSonar",
        "context": "Scan PR",
        "options": {
           "env": {"BUILD_TYPE": "nodejs"}
        }
      }
    ]
  }
}

 *
 */

var ExecutorDockerSonar = exports.ExecutorDockerSonar = function (_ExecutorDocker) {
    (0, _inherits3.default)(ExecutorDockerSonar, _ExecutorDocker);

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

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

        _this.eventData = eventData;
        _this.log = log;
        _KmsUtils2.default.logger = log;
        return _this;
    }

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

                            case 2:
                                task.options = _context.sent;
                                return _context.abrupt('return', (0, _get3.default)(ExecutorDockerSonar.prototype.__proto__ || (0, _getPrototypeOf2.default)(ExecutorDockerSonar.prototype), 'executeTask', this).call(this, task));

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

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

            return executeTask;
        }()
    }, {
        key: 'mergeTaskOptions',
        value: function () {
            var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(task) {
                var options;
                return _regenerator2.default.wrap(function _callee2$(_context2) {
                    while (1) {
                        switch (_context2.prev = _context2.next) {
                            case 0:
                                options = {
                                    image: process.env.GTM_DOCKER_DEFAULT_WORKER_IMAGE || 'zotoio/gtm-worker:latest',
                                    command: '/usr/workspace/sonar-pullrequest.sh',
                                    env: {
                                        GIT_CLONE: '##GH_CLONE_URL##',
                                        GIT_PR_ID: '##GHPRNUM##',
                                        GIT_PR_BRANCHNAME: '##GH_PR_BRANCHNAME##',
                                        SONAR_GITHUB_REPOSITORY: '##GH_REPOSITORY_FULLNAME##',
                                        SONAR_HOST_URL: '##GTM_SONAR_HOST_URL##',
                                        SONAR_LOGIN: '##GTM_CRYPT_SONAR_LOGIN##',
                                        SONAR_PROJECTNAME_PREFIX: '##GTM_SONAR_PROJECTNAME_PREFIX##',
                                        SONAR_ANALYSIS_MODE: '##GTM_SONAR_ANALYSIS_MODE##',
                                        SONAR_GITHUB_OAUTH: '##GTM_CRYPT_SONAR_GITHUB_OAUTH##',
                                        SONAR_SOURCES: '##GTM_SONAR_SOURCES##',
                                        SONAR_JAVA_BINARIES: '##GTM_SONAR_JAVA_BINARIES##',
                                        SONAR_MODULES: '##GTM_SONAR_MODULES##',
                                        SONAR_GITHUB_ENDPOINT: '##GTM_SONAR_GITHUB_ENDPOINT##',
                                        S3_DEPENDENCY_BUCKET: '##GTM_S3_DEPENDENCY_BUCKET##',
                                        AWS_S3_PROXY: '##GTM_AWS_S3_PROXY##',
                                        IAM_ENABLED: process.env.IAM_ENABLED
                                    },
                                    validator: {
                                        type: 'outputRegex',
                                        regex: '.*ANALYSIS SUCCESSFUL.*'
                                    }
                                };

                                if (process.env.IAM_ENABLED) {
                                    _context2.next = 9;
                                    break;
                                }

                                _context2.next = 4;
                                return _KmsUtils2.default.getDecrypted(process.env.GTM_CRYPT_AGENT_AWS_ACCESS_KEY_ID);

                            case 4:
                                options.env['GTM_AWS_ACCESS_KEY_ID'] = _context2.sent;
                                _context2.next = 7;
                                return _KmsUtils2.default.getDecrypted(process.env.GTM_CRYPT_AGENT_AWS_SECRET_ACCESS_KEY);

                            case 7:
                                options.env['GTM_AWS_SECRET_ACCESS_KEY'] = _context2.sent;

                                options.env['GTM_AWS_REGION'] = process.env.GTM_AWS_REGION;

                            case 9:

                                // options defined above can be overidden by options in .githubTaskManager.json
                                task.options = _lodash2.default.merge(options, task.options);
                                console.log(task.options);

                                _context2.t0 = _AgentUtils.AgentUtils;
                                _context2.t1 = _AgentUtils.AgentUtils;
                                _context2.next = 15;
                                return _AgentUtils.AgentUtils.createBasicTemplate(this.eventData, {}, this.log);

                            case 15:
                                _context2.t2 = _context2.sent;
                                _context2.t3 = task.options;
                                _context2.t4 = this.log;
                                _context2.t5 = _context2.t1.templateReplace.call(_context2.t1, _context2.t2, _context2.t3, _context2.t4);
                                task.options = _context2.t0.applyTransforms.call(_context2.t0, _context2.t5);


                                // add token into clone url
                                task.options.env.GIT_CLONE = task.options.env.GIT_CLONE.replace('https://', 'https://' + task.options.env.SONAR_GITHUB_OAUTH + '@');

                                return _context2.abrupt('return', task.options);

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

            function mergeTaskOptions(_x2) {
                return _ref2.apply(this, arguments);
            }

            return mergeTaskOptions;
        }()
    }]);
    return ExecutorDockerSonar;
}(_ExecutorDocker2.ExecutorDocker);

_Executor.Executor.register('DockerSonar', ExecutorDockerSonar);