t-comm
Version:
专业、稳定、纯粹的工具库
196 lines (193 loc) • 6.94 kB
JavaScript
import { b as __awaiter, c as __generator } from '../tslib.es6-096fffdd.js';
import { timeStampFormat } from '../time/time.mjs';
import { batchSendWxRobotMarkdown } from '../wecom-robot/batch-send.mjs';
import { getAllPipelineList } from './pipeline.mjs';
import { getReviewingReviewers, getPipelineBuildDetail } from './pipeline-build-detail.mjs';
import '../wecom-robot/base.mjs';
import '../wecom-robot/helper.mjs';
import 'axios';
import '../wecom-robot/send-img.mjs';
import '../node-img/img.mjs';
import './token.mjs';
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(_a) {
var pipelineList = _a.pipelineList,
pipelineHost = _a.pipelineHost,
projectId = _a.projectId,
secretInfo = _a.secretInfo,
host = _a.host,
overTimeConfigList = _a.overTimeConfigList,
_b = _a.mentionList,
mentionList = _b === void 0 ? ['novlan1'] : _b;
return __awaiter(this, void 0, void 0, function () {
var mentionStr, list, maxTime, curIndex, overtimePipelines, i, item, pipelines, _i, pipelines_1, pipeline, reviewers, detail, pipelineStr;
return __generator(this, function (_c) {
switch (_c.label) {
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(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 = [];
i = 0;
_c.label = 1;
case 1:
if (!(i < overTimeConfigList.length)) return [3 /*break*/, 10];
item = overTimeConfigList[i];
pipelines = findRunningTooLongPipelines(pipelineList, item.value, maxTime);
_i = 0, pipelines_1 = pipelines;
_c.label = 2;
case 2:
if (!(_i < pipelines_1.length)) return [3 /*break*/, 8];
pipeline = pipelines_1[_i];
reviewers = [];
_c.label = 3;
case 3:
_c.trys.push([3, 5,, 6]);
return [4 /*yield*/, getPipelineBuildDetail({
projectId: projectId,
pipelineId: pipeline.pipelineId,
buildId: pipeline.latestBuildId,
secretInfo: secretInfo,
host: host
})];
case 4:
detail = _c.sent();
reviewers = getReviewingReviewers(detail);
return [3 /*break*/, 6];
case 5:
_c.sent();
return [3 /*break*/, 6];
case 6:
pipeline.computedReviewers = reviewers;
_c.label = 7;
case 7:
_i++;
return [3 /*break*/, 2];
case 8:
overtimePipelines.push.apply(overtimePipelines, 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;
}
_c.label = 9;
case 9:
i++;
return [3 /*break*/, 1];
case 10:
return [2 /*return*/, {
overtimePipelines: overtimePipelines,
message: list.join('\n')
}];
}
});
});
}
/**
* 获取超时的流水线列表,并发送机器人消息
* @param {object} params 参数
* @param {object} params.params 获取流水线列表参数
* @param {string} params.pipelineHost 流水线 host 地址
* @param {string} params.webhookUrl 回调地址
* @param {string} params.chatId 会话id
*/
function sendOverTimePipelineMessage(_a) {
var params = _a.params,
pipelineHost = _a.pipelineHost,
webhookUrl = _a.webhookUrl,
chatId = _a.chatId,
_b = _a.overTimeConfigList,
overTimeConfigList = _b === void 0 ? OVER_TIME_CONFIG_LIST : _b,
_c = _a.mentionList,
mentionList = _c === void 0 ? ['novlan1'] : _c;
return __awaiter(this, void 0, void 0, function () {
var pipelineList, projectId, host, secretInfo, res, message, overtimePipelines;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
return [4 /*yield*/, getAllPipelineList(params)];
case 1:
pipelineList = _d.sent();
projectId = params.projectId, host = params.host, secretInfo = params.secretInfo;
return [4 /*yield*/, genRobotMessage({
pipelineList: pipelineList,
pipelineHost: pipelineHost,
projectId: projectId,
secretInfo: secretInfo,
host: host,
overTimeConfigList: overTimeConfigList,
mentionList: mentionList
})];
case 2:
res = _d.sent();
message = res.message;
overtimePipelines = res.overtimePipelines;
if (!overtimePipelines.length) {
console.log('[Not Found OverTime Pipeline]');
return [2 /*return*/];
}
if (message.length > 4096) {
message = "".concat(message.slice(0, 4090), "...");
}
return [4 /*yield*/, batchSendWxRobotMarkdown({
content: message,
chatId: chatId,
webhookUrl: webhookUrl
})];
case 3:
_d.sent();
return [2 /*return*/, overtimePipelines];
}
});
});
}
export { sendOverTimePipelineMessage };