gitea_compliance_check_commit
Validate commit messages against Conventional Commit format to ensure compliance with project standards. Check by SHA or directly input message.
Instructions
Check if commit message complies with Conventional Commit format. Can check by SHA or message directly.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | No | Repository owner. Uses context if not provided | |
| repo | No | Repository name. Uses context if not provided | |
| sha | No | Commit SHA to check (will fetch message from API) | |
| message | No | Commit message to check directly | |
| config_path | No | Path to compliance config file | |
| token | No | Optional API token to override default authentication |
Implementation Reference
- src/tools/compliance.ts:260-354 (handler)Main handler function that performs the compliance check on commit messages. Supports checking by SHA (fetches from Gitea API) or direct message. Validates against Conventional Commits format using configurable rules.export async function checkCommit( ctx: ComplianceToolsContext, params: CheckCommitParams ): Promise<CommitCheckResult> { const config = loadComplianceConfig(params.config_path); const owner = ctx.contextManager.resolveOwner(params.owner); const repo = ctx.contextManager.resolveRepo(params.repo); let message = params.message; let sha = params.sha || ''; // 如果提供了 SHA,从 API 获取提交信息 if (params.sha && !params.message) { try { const response = await ctx.client.request({ method: 'GET', path: `/repos/${owner}/${repo}/git/commits/${params.sha}`, token: params.token, }); message = (response.data as any)?.commit?.message || (response.data as any)?.message || ''; sha = params.sha; } catch (err) { logger.warn({ sha: params.sha, error: err }, 'Failed to fetch commit'); return { sha: params.sha, message: '', compliant: false, issues: [`无法获取提交 ${params.sha} 的信息`], suggestions: [], }; } } if (!message) { return { sha, message: '', compliant: false, issues: ['未提供提交信息'], suggestions: [], }; } logger.info({ sha, message: message.substring(0, 50) }, 'Checking commit message'); const issues: string[] = []; const suggestions: string[] = []; const parsed = parseConventionalCommit(message); if (!parsed.valid) { issues.push('提交信息不符合 Conventional Commit 格式'); suggestions.push('正确格式: <type>(<scope>): <subject>'); suggestions.push('示例: feat(cli): add new command'); } else { // 检查 type 是否有效 if (parsed.type && !config.commit.types.includes(parsed.type)) { issues.push(`无效的提交类型: ${parsed.type}`); suggestions.push(`允许的类型: ${config.commit.types.join(', ')}`); } // 检查是否需要 scope if (config.commit.scope_required && !parsed.scope) { issues.push('缺少作用域 (scope)'); suggestions.push('请在类型后添加作用域,如: feat(cli): ...'); } // 检查 subject 长度 if (parsed.subject && parsed.subject.length > config.commit.max_subject_length) { issues.push(`主题行过长: ${parsed.subject.length} 字符 (最大 ${config.commit.max_subject_length})`); suggestions.push('请缩短主题行,详细信息放在正文中'); } // 检查主题行是否以大写开头 if (parsed.subject && /^[A-Z]/.test(parsed.subject)) { issues.push('主题行不应以大写字母开头'); suggestions.push('主题行应以小写字母开头'); } // 检查主题行是否以句号结尾 if (parsed.subject && parsed.subject.endsWith('.')) { issues.push('主题行不应以句号结尾'); } } return { sha, message, compliant: issues.length === 0, issues, suggestions, type: parsed.type, scope: parsed.scope, subject: parsed.subject, }; }
- src/tools/compliance.ts:225-229 (schema)TypeScript interface defining the input parameters for the checkCommit handler.export interface CheckCommitParams extends ComplianceParams { sha?: string; message?: string; config_path?: string; }
- src/tools-registry/compliance-registry.ts:39-62 (registration)MCP tool registration for 'gitea_compliance_check_commit', including Zod input schema and wrapper handler that invokes the checkCommit function.mcpServer.registerTool( 'gitea_compliance_check_commit', { title: '检查提交信息规范', description: 'Check if commit message complies with Conventional Commit format. Can check by SHA or message directly.', inputSchema: z.object({ owner: z.string().optional().describe('Repository owner. Uses context if not provided'), repo: z.string().optional().describe('Repository name. Uses context if not provided'), sha: z.string().optional().describe('Commit SHA to check (will fetch message from API)'), message: z.string().optional().describe('Commit message to check directly'), config_path: z.string().optional().describe('Path to compliance config file'), token: tokenSchema, }), }, async (args) => { try { const result = await ComplianceTools.checkCommit(toolsContext, args as any); return { content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }] }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [{ type: 'text' as const, text: `Error: ${errorMessage}` }], isError: true }; } } );
- Zod schema for input validation in the tool registration.inputSchema: z.object({ owner: z.string().optional().describe('Repository owner. Uses context if not provided'), repo: z.string().optional().describe('Repository name. Uses context if not provided'), sha: z.string().optional().describe('Commit SHA to check (will fetch message from API)'), message: z.string().optional().describe('Commit message to check directly'), config_path: z.string().optional().describe('Path to compliance config file'), token: tokenSchema, }),