UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

256 lines (249 loc) 10.7 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray'); var _regeneratorRuntime = require('@babel/runtime/regenerator'); var tslib_es6 = require('../tslib.es6-0d92ef81.js'); var time_time = require('../time/time.js'); var wecomRobot_batchSend = require('../wecom-robot/batch-send.js'); var devops_pipeline = require('./pipeline.js'); var devops_pipelineBuildDetail = require('./pipeline-build-detail.js'); require('../wecom-robot/base.js'); require('../wecom-robot/helper.js'); require('axios'); require('../wecom-robot/send-img.js'); require('../nodejs/path.js'); require('../node-img/img.js'); require('../nodejs/crypto.js'); require('../nodejs/fs.js'); require('../nodejs/request.js'); require('./token.js'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray); var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime); function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } var OVER_TIME_CONFIG_LIST = [{ label: '5小时', value: 5 * 60 * 60 * 1000 }, { label: '2小时', value: 2 * 60 * 60 * 1000 }, { label: '1小时', value: 1 * 60 * 60 * 1000 }, { label: '半小时', value: 30 * 60 * 1000 }, { label: '20分钟', value: 20 * 60 * 1000 } // { // label: '10分钟', // value: 10 * 60 * 1000, // }, // for test // { // label: '1分钟', // value: 1 * 10 * 1000, // }, ]; function findRunningTooLongPipelines(list, time, maxTime) { var res = list.filter(function (item) { return item.latestBuildStatus === 'RUNNING'; }).filter(function (item) { var duration = item.currentTimestamp - item.latestBuildStartTime; return item.latestBuildStartTime && duration > time && duration <= maxTime; }); return res; } function genRobotMessage(_ref) { var pipelineList = _ref.pipelineList, pipelineHost = _ref.pipelineHost, projectId = _ref.projectId, secretInfo = _ref.secretInfo, host = _ref.host, overTimeConfigList = _ref.overTimeConfigList, _ref$mentionList = _ref.mentionList, mentionList = _ref$mentionList === void 0 ? ['novlan1'] : _ref$mentionList; return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() { var mentionStr, list, maxTime, curIndex, overtimePipelines, i, item, pipelines, _iterator, _step, pipeline, reviewers, detail, pipelineStr, _t2; return _regeneratorRuntime__default["default"].wrap(function (_context) { while (1) switch (_context.prev = _context.next) { case 0: mentionStr = mentionList.map(function (mention) { return "<@".concat(mention, ">"); }).join(''); list = ["\u3010\u6D41\u6C34\u7EBF\u6267\u884C\u65F6\u95F4\u8FC7\u957F\u76D1\u63A7\u3011".concat(time_time.timeStampFormat(Date.now(), 'yyyy-MM-dd hh:mm:ss')).concat(mentionStr)]; console.log('[All Pipeline Length]: ', pipelineList.length); maxTime = Number.MAX_VALUE; curIndex = 1; overtimePipelines = []; // eslint-disable-next-line @typescript-eslint/prefer-for-of i = 0; case 1: if (!(i < overTimeConfigList.length)) { _context.next = 14; break; } item = overTimeConfigList[i]; pipelines = findRunningTooLongPipelines(pipelineList, item.value, maxTime); _iterator = _createForOfIteratorHelper(pipelines); _context.prev = 2; _iterator.s(); case 3: if ((_step = _iterator.n()).done) { _context.next = 9; break; } pipeline = _step.value; reviewers = []; _context.prev = 4; _context.next = 5; return devops_pipelineBuildDetail.getPipelineBuildDetail({ projectId: projectId, pipelineId: pipeline.pipelineId, buildId: pipeline.latestBuildId, secretInfo: secretInfo, host: host }); case 5: detail = _context.sent; reviewers = devops_pipelineBuildDetail.getReviewingReviewers(detail); _context.next = 7; break; case 6: _context.prev = 6; _context["catch"](4); case 7: pipeline.computedReviewers = reviewers; case 8: _context.next = 3; break; case 9: _context.next = 11; break; case 10: _context.prev = 10; _t2 = _context["catch"](2); _iterator.e(_t2); case 11: _context.prev = 11; _iterator.f(); return _context.finish(11); case 12: overtimePipelines.push.apply(overtimePipelines, _toConsumableArray__default["default"](pipelines)); maxTime = item.value; if (pipelines.length) { console.log('[pipelines.length]', "".concat(item.label, ": ").concat(pipelines.length)); pipelineStr = pipelines.map(function (item) { var _a; var result = "[".concat(item.pipelineName, "](").concat(pipelineHost, "/console/pipeline/").concat(projectId, "/").concat(item.pipelineId, "/history)"); if ((_a = item.computedReviewers) === null || _a === void 0 ? void 0 : _a.length) { result += " \u5BA1\u6838\u4EBA: ".concat(item.computedReviewers.map(function (mention) { return "<@".concat(mention, ">"); }).join('')); } return result; }).join(','); list.push("".concat(curIndex, ". \u8D85\u8FC7").concat(item.label, "\u6709: ").concat(pipelineStr)); curIndex += 1; } case 13: i++; _context.next = 1; break; case 14: return _context.abrupt("return", { overtimePipelines: overtimePipelines, message: list.join('\n') }); case 15: case "end": return _context.stop(); } }, _callee, null, [[2, 10, 11, 12], [4, 6]]); })); } /** * 获取超时的流水线列表,并发送机器人消息 * @param {object} params 参数 * @param {object} params.params 获取流水线列表参数 * @param {string} params.pipelineHost 流水线 host 地址 * @param {string} params.webhookUrl 回调地址 * @param {string} params.chatId 会话id * @example * ```ts * await sendOverTimePipelineMessage({ * params: { * projectId: 'my-project', * host: 'https://devops.woa.com', * secretInfo: { appCode, appSecret, devopsUid }, * }, * pipelineHost: 'https://devops.woa.com', * webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx', * chatId: ['chat-id-1'], * mentionList: ['novlan1'], * }); * ``` */ function sendOverTimePipelineMessage(_ref2) { var params = _ref2.params, pipelineHost = _ref2.pipelineHost, webhookUrl = _ref2.webhookUrl, chatId = _ref2.chatId, _ref2$overTimeConfigL = _ref2.overTimeConfigList, overTimeConfigList = _ref2$overTimeConfigL === void 0 ? OVER_TIME_CONFIG_LIST : _ref2$overTimeConfigL, _ref2$mentionList = _ref2.mentionList, mentionList = _ref2$mentionList === void 0 ? ['novlan1'] : _ref2$mentionList; return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() { var pipelineList, projectId, host, secretInfo, res, message, overtimePipelines; return _regeneratorRuntime__default["default"].wrap(function (_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 1; return devops_pipeline.getAllPipelineList(params); case 1: pipelineList = _context2.sent; projectId = params.projectId, host = params.host, secretInfo = params.secretInfo; _context2.next = 2; return genRobotMessage({ pipelineList: pipelineList, pipelineHost: pipelineHost, projectId: projectId, secretInfo: secretInfo, host: host, overTimeConfigList: overTimeConfigList, mentionList: mentionList }); case 2: res = _context2.sent; message = res.message; overtimePipelines = res.overtimePipelines; if (overtimePipelines.length) { _context2.next = 3; break; } console.log('[Not Found OverTime Pipeline]'); return _context2.abrupt("return"); case 3: if (message.length > 4096) { message = "".concat(message.slice(0, 4090), "..."); } _context2.next = 4; return wecomRobot_batchSend.batchSendWxRobotMarkdown({ content: message, chatId: chatId, webhookUrl: webhookUrl }); case 4: return _context2.abrupt("return", overtimePipelines); case 5: case "end": return _context2.stop(); } }, _callee2); })); } exports.findRunningTooLongPipelines = findRunningTooLongPipelines; exports.sendOverTimePipelineMessage = sendOverTimePipelineMessage;