UNPKG

t-comm

Version:

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

298 lines (293 loc) 11.2 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('../tslib.es6-01322ba9.js'); var git_git = require('../git/git.js'); var node_nodeCommand = require('../node/node-command.js'); var tgit_branch = require('../tgit/branch.js'); var tgit_project = require('../tgit/project.js'); var wecomRobot_batchSend = require('../wecom-robot/batch-send.js'); var dailyMerge_helper = require('./helper.js'); require('@babel/runtime/helpers/typeof'); require('../tgit/helper.js'); require('axios'); require('../wecom-robot/base.js'); require('../wecom-robot/helper.js'); require('../wecom-robot/send-img.js'); require('../node-img/img.js'); var CHAT_ID = ['ALL']; var DOC_URL = 'https://docs.qq.com/doc/DSGN2Uk1hTllydkRh'; function getStartTime() { var date = new Date(); date.setHours(0, 0, 0, 0); var startTime = parseInt("".concat(date.getTime()), 10) - 24 * 60 * 60 * 1000; return startTime; } function getBranches(_a) { var baseUrl = _a.baseUrl, repoName = _a.repoName, privateToken = _a.privateToken, whiteBranchReg = _a.whiteBranchReg; return tslib_es6.__awaiter(this, void 0, void 0, function () { var res, list, branches; return tslib_es6.__generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, tgit_branch.getBranchesByProjectName({ projectName: repoName, privateToken: privateToken, baseUrl: baseUrl })["catch"](function (err) { console.log('[getBranches] err', err); })]; case 1: res = _b.sent(); list = (res || []).filter(function (item) { var commit = item.commit; var date = commit === null || commit === void 0 ? void 0 : commit.committed_date; var time = new Date(date).getTime(); if (time > getStartTime()) return true; return false; }); console.log('[Origin Branches]', list.map(function (item) { return item === null || item === void 0 ? void 0 : item.name; })); branches = list.filter(function (branch) { return dailyMerge_helper.shouldInclude(branch.name, whiteBranchReg); }); console.log('[Filter Branches]', branches.map(function (item) { return item === null || item === void 0 ? void 0 : item.name; })); return [2 /*return*/, branches]; } }); }); } function mergeMainBranch(_a) { var _b; var _c = _a.branch, branch = _c === void 0 ? {} : _c, _d = _a.successBranches, successBranches = _d === void 0 ? [] : _d, _e = _a.conflictBranches, conflictBranches = _e === void 0 ? [] : _e, _f = _a.noMergeBranches, noMergeBranches = _f === void 0 ? [] : _f, _g = _a.devRoot, devRoot = _g === void 0 ? '' : _g, _h = _a.mainBranch, mainBranch = _h === void 0 ? 'develop' : _h, _j = _a.isDryRun, isDryRun = _j === void 0 ? false : _j; if (!devRoot) return; function tExecCommand(command, stdio) { if (stdio === void 0) { stdio = 'inherit'; } console.log('[Exec] ', command); node_nodeCommand.execCommand(command, devRoot, stdio); } var branchName = branch.name; var robotBranchStr = "".concat(branchName, "<@").concat(((_b = branch === null || branch === void 0 ? void 0 : branch.author) === null || _b === void 0 ? void 0 : _b.username) || '', ">"); console.log('[Merging] ', branchName); tExecCommand('git clean -df'); tExecCommand('git reset --hard HEAD'); tExecCommand("git checkout ".concat(mainBranch, " && git pull && git submodule update --init"), 'inherit'); tExecCommand("git checkout \"".concat(branchName, "\" && git reset --hard \"origin/").concat(branchName, "\" && git pull")); var originCommitInfo = git_git.getGitCommitInfo(devRoot, true, false, false); console.log('[originCommitInfo]', originCommitInfo); try { var out = tExecCommand("git merge ".concat(mainBranch, " -m \"Merge branch '").concat(mainBranch, "' into '").concat(branchName, "' by robot\""), 'inherit'); console.log('[out]', out); } catch (err) { console.log('[Merge Error]', err); conflictBranches.push(robotBranchStr); return; } var nowCommitInfo = git_git.getGitCommitInfo(devRoot, true, false, false); console.log('[nowCommitInfo]', nowCommitInfo); if (nowCommitInfo.hash !== originCommitInfo.hash) { try { if (isDryRun) { successBranches.push(branchName); } else { tExecCommand('git push'); successBranches.push(branchName); } } catch (err) { console.log('[Push Error]', err); } } else { noMergeBranches.push(branchName); } } function getMessageContent(_a) { var appName = _a.appName, projectId = _a.projectId, baseUrl = _a.baseUrl, _b = _a.successBranches, successBranches = _b === void 0 ? [] : _b, _c = _a.conflictBranches, conflictBranches = _c === void 0 ? [] : _c, _d = _a.noMergeBranches, noMergeBranches = _d === void 0 ? [] : _d, _e = _a.toConfirmBranches, toConfirmBranches = _e === void 0 ? [] : _e; var list = [">\u3010".concat(appName || '', "\u81EA\u52A8\u5408\u5E76\u3011[\u67E5\u770B](").concat(baseUrl, "/search?tab=11&search=by%20robot&order=time&es_sort=desc&project_id=").concat(projectId || '', ")"), "- \u6210\u529F\u5408\u5E76\uFF1A".concat(successBranches.join('、')), "- \u5B58\u5728\u51B2\u7A81\uFF1A".concat(conflictBranches.join('、')), "- \u65E0\u9700\u5408\u5E76\uFF1A".concat(noMergeBranches.join('、')), "- [\u8BF4\u660E\u6587\u6863](".concat(DOC_URL, ")<@novlan1>")]; if (toConfirmBranches.length) { list.push("- \u9700\u8981\u786E\u8BA4\uFF1A".concat(toConfirmBranches.join('、'))); } var content = list.join('\n'); return content; } function sendSendMsg(content, webhookUrl) { return tslib_es6.__awaiter(this, void 0, void 0, function () { return tslib_es6.__generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, wecomRobot_batchSend.batchSendWxRobotMarkdown({ content: content, chatId: CHAT_ID, webhookUrl: webhookUrl })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } /** * 每日合并 * 1. 获取昨天有活跃的分支 * 2. 对于每个分支,进行合并并推送 * - 清理 Git 环境 * - 切到主分支,并拉最新代码 * - 切到当前分支,拉最新代码 * - 尝试执行 git merge * - 对比 merge 前后的 commit 信息是否相同,作为判断 merge 是否成功的依据 * 3. 发送机器人消息 * * * @export * @async * @param {object} param0 参数 * @param {string} param0.webhookUrl 机器人地址 * @param {string} param0.appName 项目名称 * @param {string} param0.devRoot 项目根路径 * @param {string} param0.baseUrl 基础请求 url * @param {string} param0.repoName 仓库名称 * @param {string} param0.privateToken 密钥 * @param {boolean} [param0.isDryRun=false] 是否演练 * @param {string} [param0.mainBranch='develop'] 主分支 * @param {Regexp} [param0.whiteBranchReg=/^release\|develop\|hotfix\\/.+$/] 不处理的分支正则 * @returns {*} * @example * * ```ts * dailyMerge({ * webhookUrl: 'xx', * appName: 'xx', * devRoot: 'xx', * * baseUrl: 'xx', * repoName: 'xx', * privateToken: 'xx', * * isDryRun: false, * }) * ``` */ function dailyMerge(_a) { var webhookUrl = _a.webhookUrl, appName = _a.appName, devRoot = _a.devRoot, baseUrl = _a.baseUrl, repoName = _a.repoName, privateToken = _a.privateToken, _b = _a.isDryRun, isDryRun = _b === void 0 ? false : _b, _c = _a.mainBranch, mainBranch = _c === void 0 ? 'develop' : _c, _d = _a.whiteBranchReg, whiteBranchReg = _d === void 0 ? dailyMerge_helper.DEFAULT_WHITE_REG : _d; return tslib_es6.__awaiter(this, void 0, void 0, function () { var branches, successBranches, conflictBranches, noMergeBranches, toConfirmBranches, projectDetail, projectId, content; return tslib_es6.__generator(this, function (_e) { switch (_e.label) { case 0: console.log('\nSTART Daily Merge =====>'); if (!baseUrl) { console.error('Please Input Base Url'); return [2 /*return*/]; } if (!repoName) { console.error('Please Input Repo Name'); return [2 /*return*/]; } return [4 /*yield*/, getBranches({ baseUrl: baseUrl, repoName: repoName, privateToken: privateToken, whiteBranchReg: whiteBranchReg })]; case 1: branches = _e.sent(); if (!(branches === null || branches === void 0 ? void 0 : branches.length)) { console.log('[no branch to merge]'); sendSendMsg(">\u3010".concat(appName || '', "\u81EA\u52A8\u5408\u5E76\u3011\u672A\u627E\u5230\u9700\u8981\u5408\u5E76\u7684\u5206\u652F"), webhookUrl); return [2 /*return*/]; } successBranches = []; conflictBranches = []; noMergeBranches = []; branches.forEach(function (branch) { mergeMainBranch({ branch: branch, successBranches: successBranches, conflictBranches: conflictBranches, noMergeBranches: noMergeBranches, mainBranch: mainBranch, devRoot: devRoot, isDryRun: isDryRun }); }); toConfirmBranches = branches.find(function (branch) { var all = tslib_es6.__spreadArray(tslib_es6.__spreadArray(tslib_es6.__spreadArray([], successBranches, true), conflictBranches, true), noMergeBranches, true); return !all.includes(branch); }); return [4 /*yield*/, tgit_project.getOneProjectDetail({ baseUrl: baseUrl, projectName: repoName, privateToken: privateToken })]; case 2: projectDetail = _e.sent(); projectId = projectDetail.id || ''; console.log('[projectDetail]', projectDetail); content = getMessageContent({ appName: appName, projectId: projectId, baseUrl: baseUrl, successBranches: successBranches, conflictBranches: conflictBranches, noMergeBranches: noMergeBranches, toConfirmBranches: toConfirmBranches }); console.log('[successBranches]', JSON.stringify(successBranches)); console.log('[conflictBranches]', JSON.stringify(conflictBranches)); console.log('[noMergeBranches]', JSON.stringify(noMergeBranches)); console.log('[robot message]', content); if (!webhookUrl) { console.log('[No webhookUrl]'); return [2 /*return*/]; } return [4 /*yield*/, sendSendMsg(content, webhookUrl)]; case 3: _e.sent(); console.log('<===== END '); return [2 /*return*/]; } }); }); } exports.dailyMerge = dailyMerge;