'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var rdPlatformReport_rdPlatformReport = require('../rd-platform-report/rd-platform-report.js');
var mpCi_helper = require('./helper.js');
var mpCi_mpCi = require('./mp-ci.js');
var mpCi_writeEnvAndPrivateKey = require('./write-env-and-private-key.js');
require('axios');
require('../bite/format-bite.js');
require('../git/git.js');
require('../node/node-command.js');
require('@babel/runtime/helpers/typeof');
require('../nodejs/child_process.js');
require('../validate/validate.js');
require('../nodejs/path.js');
require('../wecom-robot/message.js');
require('./config.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('../canvas/img-text.js');
require('../third-party/image-size.js');
require('../canvas/get-canvas.js');
require('../third-party/canvas.js');
require('../node-img/img.js');
require('../nodejs/crypto.js');
require('../nodejs/fs.js');
require('../nodejs/request.js');
require('../tencent-cloud/cos/cos.js');
require('../tencent-cloud/cos/helper.js');
require('../third-party/cos-nodejs-sdk-v5.js');
require('../tencent-cloud/cos/link.js');
require('../third-party/miniprogram-ci.js');
require('../time/time.js');
require('../wecom-robot/base.js');
require('../wecom-robot/helper.js');
require('../fs/fs.js');
require('../nodejs/os.js');
require('../rainbow/rainbow-user.js');
require('../rainbow/helper/helper.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

function flattenSubPackages(result) {
  var _result$subPackageInf = result.subPackageInfo,
    subPackageInfo = _result$subPackageInf === void 0 ? [] : _result$subPackageInf;
  return subPackageInfo.reduce(function (acc, item) {
    acc[item.name] = item;
    return acc;
  }, {});
}
function reportToRd(_ref) {
  var bundleInfo = _ref.bundleInfo,
    rdProjectId = _ref.rdProjectId,
    subProject = _ref.subProject,
    robot = _ref.robot,
    lastCommit = _ref.lastCommit,
    repo = _ref.repo,
    env = _ref.env,
    branch = _ref.branch,
    rdHost = _ref.rdHost,
    bkStartUserName = _ref.bkStartUserName,
    bkStartType = _ref.bkStartType,
    bkBuildUrl = _ref.bkBuildUrl,
    bkPipelineId = _ref.bkPipelineId;
  var _a, _b;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var mainBundleSize, totalBundleSize, reportToRdRes;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (bundleInfo === null || bundleInfo === void 0 ? void 0 : bundleInfo.__APP__) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return");
        case 1:
          mainBundleSize = parseInt("".concat((((_a = bundleInfo.__APP__) === null || _a === void 0 ? void 0 : _a.size) || 0) / 1024), 10);
          totalBundleSize = parseInt("".concat((((_b = bundleInfo.__FULL__) === null || _b === void 0 ? void 0 : _b.size) || 0) / 1024), 10);
          _context.next = 2;
          return rdPlatformReport_rdPlatformReport.reportToRdPlatform({
            data: {
              projectId: rdProjectId,
              project: repo || '',
              subProject: subProject,
              bkStartUserName: bkStartUserName || '',
              bkStartType: bkStartType || '',
              bkBuildUrl: bkBuildUrl || '',
              bkPipelineId: bkPipelineId || '',
              env: env || '',
              branch: branch || '',
              mainBundleSize: mainBundleSize,
              totalBundleSize: totalBundleSize,
              createTime: Date.now()
            },
            host: rdHost || '',
            type: 2,
            platform: 2
          });
        case 2:
          reportToRdRes = _context.sent;
          _context.next = 3;
          return rdPlatformReport_rdPlatformReport.reportToRdPlatform({
            data: {
              projectId: rdProjectId,
              projectName: repo || '',
              subProjectName: subProject,
              env: env || '',
              branch: branch || '',
              robot: robot,
              lastCommit: lastCommit,
              mainBundleSize: mainBundleSize,
              totalBundleSize: totalBundleSize,
              bkBuildUrl: bkBuildUrl || '',
              time: Date.now(),
              isWx: true
            },
            host: rdHost || '',
            type: 4
          })["catch"](function (err) {
            console.log('[reportToRdRecordRes] err', err);
          });
        case 3:
          console.log('[reportToRdRes]', reportToRdRes);
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function mpUploadAndReport(_ref2) {
  var branch = _ref2.branch,
    env = _ref2.env,
    root = _ref2.root,
    rainbowConfigKey = _ref2.rainbowConfigKey,
    rainbowAppId = _ref2.rainbowAppId,
    rainbowEnvName = _ref2.rainbowEnvName,
    rainbowGroupName = _ref2.rainbowGroupName,
    rdHost = _ref2.rdHost,
    bkStartType = _ref2.bkStartType,
    bkBuildUrl = _ref2.bkBuildUrl,
    bkStartUserName = _ref2.bkStartUserName,
    bkPipelineId = _ref2.bkPipelineId,
    commitInfo = _ref2.commitInfo,
    version = _ref2.version,
    buildDesc = _ref2.buildDesc;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var rainbowSecretInfo, config, robot, appName, appId, webhookUrl, chatId, cosInfo, rdProjectId, subProject, ciConfig, mpCISetting, ci, _ci$commitInfo, ciCommitInfo, bundleInfo, _t;
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          rainbowSecretInfo = {
            appId: rainbowAppId,
            envName: rainbowEnvName,
            groupName: rainbowGroupName
          };
          _context2.next = 1;
          return mpCi_writeEnvAndPrivateKey.getCIConfig(rainbowConfigKey, rainbowSecretInfo);
        case 1:
          config = _context2.sent;
          console.log('[CI] config: ', config);
          robot = mpCi_writeEnvAndPrivateKey.getRobot({
            config: config,
            branch: branch,
            env: env
          });
          appName = config.appName, appId = config.appId, webhookUrl = config.webhookUrl, chatId = config.chatId, cosInfo = config.cosInfo, rdProjectId = config.rdProjectId, subProject = config.subProject, ciConfig = config.ci, mpCISetting = config.mpCISetting;
          ci = new mpCi_mpCi.MpCI({
            appName: appName,
            appId: appId,
            root: root,
            env: env,
            robotNumber: robot,
            webhookUrl: webhookUrl,
            chatId: chatId,
            cosInfo: cosInfo,
            errorLink: bkBuildUrl,
            commitInfo: commitInfo,
            version: version,
            buildDesc: buildDesc,
            buildSetting: mpCISetting || {}
          });
          _context2.next = 2;
          return ci.uploadAndPreview();
        case 2:
          _ci$commitInfo = ci.commitInfo, ciCommitInfo = _ci$commitInfo === void 0 ? {} : _ci$commitInfo;
          bundleInfo = flattenSubPackages(ci.previewResult || {});
          _context2.prev = 3;
          _context2.next = 4;
          return reportToRd({
            bundleInfo: bundleInfo,
            rdProjectId: rdProjectId,
            subProject: subProject,
            robot: robot,
            lastCommit: "".concat(ciCommitInfo.author, " - ").concat(ciCommitInfo.message, " - ").concat(ciCommitInfo.hash),
            repo: ciConfig.repo || '',
            env: env,
            branch: branch,
            rdHost: rdHost,
            bkStartUserName: bkStartUserName,
            bkStartType: bkStartType,
            bkBuildUrl: bkBuildUrl,
            bkPipelineId: bkPipelineId
          });
        case 4:
          _context2.next = 6;
          break;
        case 5:
          _context2.prev = 5;
          _t = _context2["catch"](3);
          console.log('[reportToRd] err: ', _t);
        case 6:
          if (!(bkStartType !== 'TIME_TRIGGER')) {
            _context2.next = 7;
            break;
          }
          _context2.next = 7;
          return ci.sendRobotMsg();
        case 7:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[3, 5]]);
  }));
}
function mpUploadAndReportByOptions(options) {
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var branch, env, root, rainbowConfigKey, rainbowAppId, rainbowEnvName, rainbowGroupName, rdHost, bkStartType, bkBuildUrl, bkStartUserName, bkPipelineId, commitAuthor, commitMessage, commitHash, mpVersion;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          console.log('[options] ', options);
          branch = options.branch, env = options.env, root = options.root, rainbowConfigKey = options.configKey, rainbowAppId = options.appid, rainbowEnvName = options.envName, rainbowGroupName = options.groupName, rdHost = options.rdHost, bkStartType = options.bkStartType, bkBuildUrl = options.bkBuildUrl, bkStartUserName = options.bkStartUserName, bkPipelineId = options.bkPipelineId, commitAuthor = options.commitAuthor, commitMessage = options.commitMessage, commitHash = options.commitHash, mpVersion = options.mpVersion;
          if (!(!branch || !env || !root || !rainbowConfigKey || !rainbowAppId || !rainbowEnvName || !rainbowGroupName || !rdHost || !bkStartType || !bkBuildUrl || !bkStartUserName || !bkPipelineId || !commitAuthor || !commitMessage || !commitHash || !mpVersion)) {
            _context3.next = 1;
            break;
          }
          console.error('缺少必要参数，请检查！');
          return _context3.abrupt("return");
        case 1:
          mpUploadAndReport({
            branch: branch,
            env: env,
            root: root,
            rainbowConfigKey: rainbowConfigKey,
            rainbowAppId: rainbowAppId,
            rainbowEnvName: rainbowEnvName,
            rainbowGroupName: rainbowGroupName,
            rdHost: rdHost,
            bkStartType: bkStartType,
            bkBuildUrl: bkBuildUrl,
            bkStartUserName: bkStartUserName,
            bkPipelineId: bkPipelineId,
            commitInfo: {
              author: commitAuthor,
              message: commitMessage,
              hash: commitHash,
              branch: branch
            },
            buildDesc: mpCi_helper.getInnerBundleBuildDesc({
              env: env,
              branch: branch,
              author: commitAuthor,
              message: commitMessage
            }),
            version: mpVersion
          });
        case 2:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

exports.mpUploadAndReport = mpUploadAndReport;
exports.mpUploadAndReportByOptions = mpUploadAndReportByOptions;
