t-comm
Version:
专业、稳定、纯粹的工具库
289 lines (282 loc) • 10.2 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib_es6 = require('../tslib.es6-01322ba9.js');
var axios = require('axios');
var time_time = require('../time/time.js');
var wecomRobot_batchSend = require('../wecom-robot/batch-send.js');
var devops_token = require('./token.js');
require('../wecom-robot/base.js');
require('../wecom-robot/helper.js');
require('../wecom-robot/send-img.js');
require('../node-img/img.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
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 tslib_es6.__awaiter(this, void 0, void 0, function () {
var appCode, appSecret, devopsUid, accessToken, resp;
return tslib_es6.__generator(this, function (_b) {
switch (_b.label) {
case 0:
appCode = secretInfo.appCode, appSecret = secretInfo.appSecret, devopsUid = secretInfo.devopsUid;
return [4 /*yield*/, devops_token.getDevopsAccessToken({
secretInfo: secretInfo,
host: host
})];
case 1:
accessToken = _b.sent();
return [4 /*yield*/, axios__default["default"]({
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: tslib_es6.__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 tslib_es6.__awaiter(this, void 0, void 0, function () {
var appCode, appSecret, devopsUid, accessToken, resp;
return tslib_es6.__generator(this, function (_e) {
switch (_e.label) {
case 0:
appCode = secretInfo.appCode, appSecret = secretInfo.appSecret, devopsUid = secretInfo.devopsUid;
return [4 /*yield*/, devops_token.getDevopsAccessToken({
secretInfo: secretInfo,
host: host
})];
case 1:
accessToken = _e.sent();
return [4 /*yield*/, axios__default["default"]({
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 tslib_es6.__awaiter(this, void 0, void 0, function () {
var page, res;
return tslib_es6.__generator(this, function (_a) {
switch (_a.label) {
case 0:
page = args.page || 1;
return [4 /*yield*/, getPipelineList(tslib_es6.__assign(tslib_es6.__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(tslib_es6.__assign(tslib_es6.__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(_a) {
var pipelineList = _a.pipelineList,
pipelineHost = _a.pipelineHost,
projectId = _a.projectId,
overTimeConfigList = _a.overTimeConfigList,
_b = _a.mentionList,
mentionList = _b === void 0 ? ['guowangyang'] : _b;
var mentionStr = mentionList.map(function (mention) {
return "<@".concat(mention, ">");
}).join('');
var 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);
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(pipelineList, 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(pipelineHost, "/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,
_c = _a.mentionList,
mentionList = _c === void 0 ? ['guowangyang'] : _c;
return tslib_es6.__awaiter(this, void 0, void 0, function () {
var pipelineList, res, message, overtimePipelines;
return tslib_es6.__generator(this, function (_d) {
switch (_d.label) {
case 0:
return [4 /*yield*/, getAllPipelineList(params)];
case 1:
pipelineList = _d.sent();
res = genRobotMessage({
pipelineList: pipelineList,
pipelineHost: pipelineHost,
projectId: params.projectId,
overTimeConfigList: overTimeConfigList,
mentionList: mentionList
});
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*/, wecomRobot_batchSend.batchSendWxRobotMarkdown({
content: message,
chatId: chatId,
webhookUrl: webhookUrl
})];
case 2:
_d.sent();
return [2 /*return*/, overtimePipelines];
}
});
});
}
exports.getAllPipelineList = getAllPipelineList;
exports.getPipelineList = getPipelineList;
exports.sendOverTimePipelineMessage = sendOverTimePipelineMessage;
exports.startDevopsPipeline = startDevopsPipeline;