'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');

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;
        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:
                                task.options = this.mergeTaskOptions(task);
                                return _context.abrupt('return', (0, _get3.default)(ExecutorDockerSonar.prototype.__proto__ || (0, _getPrototypeOf2.default)(ExecutorDockerSonar.prototype), 'executeTask', this).call(this, task));

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

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

            return executeTask;
        }()
    }, {
        key: 'mergeTaskOptions',
        value: function mergeTaskOptions(task) {
            var 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_SONAR_LOGIN##',
                    SONAR_PROJECTNAME_PREFIX: '##GTM_SONAR_PROJECTNAME_PREFIX##',
                    SONAR_ANALYSIS_MODE: '##GTM_SONAR_ANALYSIS_MODE##',
                    SONAR_GITHUB_OAUTH: '##GTM_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##'
                },
                validator: {
                    type: 'outputRegex',
                    regex: '.*ANALYSIS SUCCESSFUL.*'
                }
            };

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

            task.options = _AgentUtils.AgentUtils.applyTransforms(_AgentUtils.AgentUtils.templateReplace(_AgentUtils.AgentUtils.createBasicTemplate(this.eventData, {}, this.log), task.options, this.log));

            // 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 task.options;
        }
    }]);
    return ExecutorDockerSonar;
}(_ExecutorDocker2.ExecutorDocker);

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