t-comm
Version:
专业、稳定、纯粹的工具库
262 lines (259 loc) • 9.13 kB
JavaScript
import { b as __awaiter, c as __generator, _ as __assign } from '../tslib.es6-48fa7a9b.js';
import { getDevopsAccessToken } from './token.js';
import { timeStampFormat } from '../time/time.js';
import { batchSendWxRobotMarkdown } from '../wecom-robot/batch-send.js';
import '../wecom-robot/send-img.js';
import '../wecom-robot/base.js';
import '../wecom-robot/helper.js';
import '../node-img/img.js';
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,
// },
];
/**
* 启动流水线
* @param {object} params 配置信息
* @param {string} params.projectId 项目ID
* @param {string} params.pipelineId 流水线ID
* @param {object} params.secretInfo 密钥信息
* @param {string} params.host 请求域名
* @param {object} params.data 请求数据
*/
function startDevopsPipeline(_a) {
var projectId = _a.projectId,
pipelineId = _a.pipelineId,
secretInfo = _a.secretInfo,
host = _a.host,
data = _a.data;
return __awaiter(this, void 0, void 0, function () {
var axios, appCode, appSecret, devopsUid, accessToken, resp;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
axios = require('axios');
appCode = secretInfo.appCode, appSecret = secretInfo.appSecret, devopsUid = secretInfo.devopsUid;
return [4 /*yield*/, getDevopsAccessToken({
secretInfo: secretInfo,
host: host
})];
case 1:
accessToken = _b.sent();
return [4 /*yield*/, axios({
url: "".concat(host, "/prod/v4/apigw-app/projects/").concat(projectId, "/build_start?pipelineId=").concat(pipelineId),
method: 'post',
headers: {
'X-DEVOPS-UID': devopsUid,
'Content-Type': 'application/json',
'X-Bkapi-Authorization': JSON.stringify({
bk_app_code: appCode,
bk_app_secret: appSecret,
access_token: accessToken
})
},
data: __assign({}, data || {})
})["catch"](function (err) {
console.log('[startDevopsPipeline] err: ', err);
})];
case 2:
resp = _b.sent();
return [2 /*return*/, resp.data];
}
});
});
}
/**
* 获取流水线列表
* @param {object} params 配置信息
* @param {string} params.projectId 项目ID
* @param {object} params.secretInfo 密钥信息
* @param {string} params.host 请求域名
* @param {number} params.page 第几页
* @param {number} params.pageSize 每页数据量
* @returns 流水线列表
*/
function getPipelineList(_a) {
var _b;
var projectId = _a.projectId,
secretInfo = _a.secretInfo,
host = _a.host,
_c = _a.page,
page = _c === void 0 ? 1 : _c,
_d = _a.pageSize,
pageSize = _d === void 0 ? 20 : _d;
return __awaiter(this, void 0, void 0, function () {
var axios, appCode, appSecret, devopsUid, accessToken, resp;
return __generator(this, function (_e) {
switch (_e.label) {
case 0:
axios = require('axios');
appCode = secretInfo.appCode, appSecret = secretInfo.appSecret, devopsUid = secretInfo.devopsUid;
return [4 /*yield*/, getDevopsAccessToken({
secretInfo: secretInfo,
host: host
})];
case 1:
accessToken = _e.sent();
return [4 /*yield*/, axios({
url: "".concat(host, "/prod/v4/apigw-app/projects/").concat(projectId, "/pipelines/pipeline_list?page=").concat(page, "&pageSize=").concat(pageSize),
method: 'GET',
headers: {
'X-DEVOPS-UID': devopsUid,
'Content-Type': 'application/json',
'X-Bkapi-Authorization': JSON.stringify({
bk_app_code: appCode,
bk_app_secret: appSecret,
access_token: accessToken
})
}
})["catch"](function (err) {
console.log('[getPipelineList] err: ', err);
})];
case 2:
resp = _e.sent();
return [2 /*return*/, ((_b = resp.data) === null || _b === void 0 ? void 0 : _b.data) || {}];
}
});
});
}
/**
* 获取全部流水线列表
* @param {object} params 配置信息
* @param {string} params.projectId 项目ID
* @param {object} params.secretInfo 密钥信息
* @param {string} params.host 请求域名
* @param {number} params.page 第几页
* @param {number} params.pageSize 每页数据量
* @param {Array} list 结果列表,可不传,用于迭代
* @returns 流水线列表
*/
function getAllPipelineList(args, list) {
if (list === void 0) {
list = [];
}
return __awaiter(this, void 0, void 0, function () {
var page, res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
page = args.page || 1;
return [4 /*yield*/, getPipelineList(__assign(__assign({}, args), {
page: page
}))];
case 1:
res = _a.sent();
list.push.apply(list, res.records || []);
if (!res.count || list.length >= res.count) {
return [2 /*return*/, list];
}
return [4 /*yield*/, getAllPipelineList(__assign(__assign({}, args), {
page: page + 1
}), list)];
case 2:
return [2 /*return*/, _a.sent()];
}
});
});
}
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(dataList, host, projectId, overTimeConfigList) {
var list = ["\u3010\u6D41\u6C34\u7EBF\u6267\u884C\u65F6\u95F4\u8FC7\u957F\u76D1\u63A7\u3011".concat(timeStampFormat(Date.now(), 'yyyy-MM-dd hh:mm:ss'), "<@guowangyang>")];
console.log('[All Pipeline Length]: ', dataList.length);
var maxTime = Number.MAX_VALUE;
var curIndex = 1;
var overtimePipelines = [];
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (var i = 0; i < overTimeConfigList.length; i++) {
var item = overTimeConfigList[i];
var pipelines = findRunningTooLongPipelines(dataList, item.value, maxTime);
overtimePipelines.push.apply(overtimePipelines, pipelines);
maxTime = item.value;
if (pipelines.length) {
console.log('[pipelines.length]', "".concat(item.label, ": ").concat(pipelines.length));
var pipelineStr = pipelines.map(function (item) {
return "[".concat(item.pipelineName, "](").concat(host, "/console/pipeline/").concat(projectId, "/").concat(item.pipelineId, "/history)");
}).join(', ');
list.push("".concat(curIndex, ". \u8D85\u8FC7").concat(item.label, "\u6709: ").concat(pipelineStr));
curIndex += 1;
}
}
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;
return __awaiter(this, void 0, void 0, function () {
var pipelineList, res, message, overtimePipelines;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
return [4 /*yield*/, getAllPipelineList(params)];
case 1:
pipelineList = _c.sent();
res = genRobotMessage(pipelineList, pipelineHost, params.projectId, overTimeConfigList);
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 2:
_c.sent();
return [2 /*return*/, overtimePipelines];
}
});
});
}
export { getAllPipelineList, getPipelineList, sendOverTimePipelineMessage, startDevopsPipeline };