t-comm
Version:
专业、稳定、纯粹的工具库
133 lines (132 loc) • 5.66 kB
TypeScript
import type { CheckLintReportInfo, FileMap, JSErrorFile, SCSSErrorFile } from './types';
export declare function parseScssFiles(scssErrorFiles: SCSSErrorFile[], workspace: string): any[];
export declare function parseJSFiles(jsErrorFiles: JSErrorFile[], workspace: string): any[];
/**
* 生成修复分支名:feature/fix-eslint-{yyyyMMddhhmmss} 或 feature/fix-stylelint-{yyyyMMddhhmmss}
* @example
* ```ts
* genFixBranchName('eslint', new Date('2025-01-02 03:04:05').getTime());
* // 'feature/fix-eslint-20250102030405'
*
* genFixBranchName('stylelint');
* // 'feature/fix-stylelint-{当前时间}'
* ```
*/
export declare function genFixBranchName(type?: 'eslint' | 'stylelint', timestamp?: number): string;
/**
* 自动执行 --fix 并创建修复 MR
* eslint 和 stylelint 分别创建独立 MR,便于 review
* @returns 创建的修复 MR 信息列表,失败时返回 undefined
* @example
* ```ts
* const fixMRList = await autoFixAndCreateMR({
* fileMap,
* workspace: '/path/to/repo',
* sourceBranch: 'feature/foo',
* targetBranch: 'master',
* repo: 'group/project',
* privateToken: 'xxx',
* mrTitlePrefix: '[WIP]',
* });
* ```
*/
export declare function autoFixAndCreateMR({ fileMap, workspace, sourceBranch, targetBranch, repo, privateToken, gitApiPrefix, mrTitlePrefix, }: {
fileMap: FileMap;
workspace: string;
sourceBranch?: string;
targetBranch?: string;
repo: string;
privateToken: string;
gitApiPrefix?: string;
/** MR 标题前缀,如 '[WIP]' 可防止被自动合入 */
mrTitlePrefix?: string;
}): Promise<Array<{
fixBranch: string;
fixTargetBranch: string;
mrData?: any;
}> | undefined>;
/**
* 执行代码 lint 检查(支持 ESLint 和 StyleLint),并将结果通知到企业微信群、MR 评论等。
*
* 支持增量模式(仅检查 sourceBranch 与 targetBranch 之间的 diff 文件)和全量模式(checkAll=true)。
* 检查完成后可自动执行 --fix 并创建修复 MR(autoFixMR=true)。
*
* @param options - 配置参数
* @param options.privateToken - Git API 私有令牌,用于操作 MR
* @param options.gitApiPrefix - Git API 地址前缀
* @param options.workspace - 项目工作目录绝对路径
* @param options.mrUrl - MR 页面链接,用于消息通知中展示
* @param options.mrId - MR ID,传入后会在 MR 中添加评论和逐行批注
* @param options.buildUrl - 流水线构建链接,用于消息通知中展示
* @param options.repo - 仓库名称(如 group/project)
* @param options.repoUrl - 仓库页面链接
* @param options.sourceBranch - 源分支名(增量模式必填)
* @param options.targetBranch - 目标分支名(增量模式必填)
* @param options.docLink - 说明文档链接,用于消息通知中展示
* @param options.webhookUrl - 企业微信机器人 Webhook 地址
* @param options.chatId - 企业微信群聊 ID 列表,默认 ['ALL']
* @param options.checkAll - 是否全量检查,默认 false(增量模式)
* @param options.mentionList - 需要 @ 的企业微信用户列表
* @param options.lintFiles - 需要检查的文件类型列表,默认检查所有配置的类型
* @param options.throwError - 检查不通过时是否抛出异常,默认 true
* @param options.ignoreSubmodules - 是否在 lint 时忽略 git submodule,默认 true
* @param options.autoFixMR - 是否自动执行 --fix 并创建修复 MR,默认 false
* @param options.mrTitlePrefix - autoFixMR 创建 MR 时的标题前缀,如 '[WIP]' 可防止被自动合入
* @param options.onReport - 上报回调,lint 完成后调用,传入错误数量等关键信息
* @returns 返回 fileMap,包含各文件类型的 lint 结果
* @throws 当 throwError 为 true 且存在 lint 错误时抛出异常
*
* @example
* ```ts
* import { checkLint } from 't-comm';
*
* await checkLint({
* privateToken: 'your-token',
* gitApiPrefix: 'https://git.woa.com/api/v3',
* workspace: '/path/to/project',
* buildUrl: 'https://ci.example.com/build/123',
* repo: 'group/project',
* sourceBranch: 'feature/xxx',
* targetBranch: 'master',
* docLink: 'https://doc.example.com/lint',
* webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx',
* });
* ```
*/
export declare function checkLint({ privateToken, gitApiPrefix, workspace, mrUrl, mrId, buildUrl, repo, repoUrl, sourceBranch, targetBranch, docLink, webhookUrl, chatId, checkAll, mentionList, lintFiles, throwError, ignoreSubmodules, autoFixMR, mrTitlePrefix, onReport, }: {
privateToken: string;
gitApiPrefix?: string;
workspace: string;
mrUrl?: string;
mrId?: string;
buildUrl: string;
repo: string;
repoUrl?: string;
sourceBranch?: string;
targetBranch?: string;
docLink: string;
webhookUrl: string;
chatId?: string[];
checkAll?: boolean;
mentionList?: string[];
lintFiles?: string[];
throwError?: boolean;
ignoreSubmodules?: boolean;
/** 是否自动执行 --fix 并创建修复 MR */
autoFixMR?: boolean;
/** autoFixMR 创建 MR 时的标题前缀,如 '[WIP]' 可防止被自动合入 */
mrTitlePrefix?: string;
/**
* 上报回调。传入后会在 lint 完成、结果解析完毕时调用,
* 将错误数量、是否全量、mrId 等关键信息传给外部。
*/
onReport?: (info: CheckLintReportInfo) => void | Promise<void>;
}): Promise<FileMap>;
export declare function getErrorInfo(result: Array<{
errorCount?: number;
}>): {
total: number;
errorFiles: {
errorCount?: number | undefined;
}[];
};