gitea_compliance_check_branch
Validate branch names against naming conventions to ensure compliance with repository standards. Returns status and improvement suggestions.
Instructions
Check if branch name complies with naming conventions. Returns compliance status and suggestions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | Yes | Branch name to check | |
| config_path | No | Path to compliance config file (.gitea/compliance.yaml) |
Implementation Reference
- src/tools/compliance.ts:173-221 (handler)Core implementation of the branch compliance check. Loads config, matches branch name against allowed regex patterns, generates issues and suggestions if non-compliant.export async function checkBranch( ctx: ComplianceToolsContext, params: CheckBranchParams ): Promise<BranchCheckResult> { const config = loadComplianceConfig(params.config_path); const branch = params.branch; logger.info({ branch }, 'Checking branch naming'); const issues: string[] = []; const suggestions: string[] = []; let matchedPattern: string | undefined; // 检查分支名是否匹配任何允许的模式 for (const pattern of config.branch.patterns) { const regex = new RegExp(pattern); if (regex.test(branch)) { matchedPattern = pattern; break; } } if (!matchedPattern) { issues.push(`分支名 "${branch}" 不符合任何允许的命名模式`); // 提供建议 if (branch.includes('feat') || branch.includes('feature')) { suggestions.push('功能分支建议格式: feat/issue-{id}-{描述}'); } else if (branch.includes('fix') || branch.includes('bug')) { suggestions.push('修复分支建议格式: fix/issue-{id}-{描述}'); } else if (branch.includes('doc')) { suggestions.push('文档分支建议格式: docs/{描述}'); } else { suggestions.push('建议的分支格式:'); suggestions.push(' - feat/issue-{id}-{描述} (新功能)'); suggestions.push(' - fix/issue-{id}-{描述} (修复)'); suggestions.push(' - docs/{描述} (文档)'); suggestions.push(' - refactor/{描述} (重构)'); } } return { branch, compliant: issues.length === 0, issues, suggestions, matched_pattern: matchedPattern, }; }
- src/tools-registry/compliance-registry.ts:17-36 (registration)Tool registration for 'gitea_compliance_check_branch' including title, description, input schema, and wrapper handler delegating to checkBranch implementation.mcpServer.registerTool( 'gitea_compliance_check_branch', { title: '检查分支命名规范', description: 'Check if branch name complies with naming conventions. Returns compliance status and suggestions.', inputSchema: z.object({ branch: z.string().min(1).describe('Branch name to check'), config_path: z.string().optional().describe('Path to compliance config file (.gitea/compliance.yaml)'), }), }, async (args) => { try { const result = await ComplianceTools.checkBranch(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 input schema defining parameters for the tool: required branch name and optional config path.inputSchema: z.object({ branch: z.string().min(1).describe('Branch name to check'), config_path: z.string().optional().describe('Path to compliance config file (.gitea/compliance.yaml)'), }),
- src/tools/compliance.ts:79-101 (helper)Helper function to load compliance configuration from .gitea/compliance.yaml or fall back to defaults, used by the checkBranch handler.export function loadComplianceConfig(configPath?: string): ComplianceConfig { const searchPaths = [ configPath, path.join(process.cwd(), '.gitea', 'compliance.yaml'), path.join(process.cwd(), '.gitea', 'compliance.yml'), ].filter(Boolean) as string[]; for (const p of searchPaths) { if (fs.existsSync(p)) { try { const content = fs.readFileSync(p, 'utf-8'); const parsed = yaml.parse(content); logger.info({ path: p }, 'Loaded compliance config'); return mergeConfig(DEFAULT_CONFIG, parsed); } catch (err) { logger.warn({ path: p, error: err }, 'Failed to parse compliance config'); } } } logger.info('Using default compliance config'); return DEFAULT_CONFIG; }
- src/tools/compliance.ts:51-74 (schema)Default compliance configuration including branch naming patterns used when no config file is provided.const DEFAULT_CONFIG: ComplianceConfig = { branch: { patterns: [ '^feat/issue-\\d+-.*$', '^fix/issue-\\d+-.*$', '^docs/.*$', '^refactor/.*$', '^test/.*$', '^chore/.*$', '^main$', '^dev$', '^release/.*$', ], }, commit: { types: ['feat', 'fix', 'docs', 'refactor', 'test', 'chore', 'style', 'perf', 'ci', 'build', 'revert'], scope_required: false, max_subject_length: 72, }, pr: { required_sections: ['Summary', 'Test Plan'], require_issue_link: true, }, };