gitea_workflow_sync_labels
Synchronize repository labels from workflow configuration to create standardized status, priority, and type labels in Gitea repositories.
Instructions
Sync repository labels based on workflow configuration. Creates status/, priority/, type/* and other labels defined in the config.
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 | |
| dry_run | No | Preview changes without applying (default: false) |
Implementation Reference
- src/tools/workflow.ts:158-257 (handler)Core handler function for 'gitea_workflow_sync_labels' that synchronizes repository labels with the workflow configuration. Loads config if not provided, fetches existing labels, creates new ones, updates colors/descriptions, supports dry-run, and returns sync report./** * 同步标签系统 */ export async function workflowSyncLabels( ctx: WorkflowToolsContext, args: { owner?: string; repo?: string; config?: WorkflowConfig; dry_run?: boolean; } ): Promise<{ success: boolean; created: string[]; updated: string[]; skipped: string[]; errors: string[]; }> { logger.debug({ args: { ...args, config: args.config ? '[provided]' : undefined } }, 'Syncing labels'); const { owner, repo } = ctx.contextManager.resolveOwnerRepo(args.owner, args.repo); const dryRun = args.dry_run ?? false; // 获取配置 let config = args.config; if (!config) { const loadResult = await workflowLoadConfig(ctx, { owner, repo }); if (!loadResult.success || !loadResult.config) { return { success: false, created: [], updated: [], skipped: [], errors: [loadResult.error || '无法加载配置'], }; } config = loadResult.config; } const created: string[] = []; const updated: string[] = []; const skipped: string[] = []; const errors: string[] = []; // 获取现有标签 const existingLabels = await ctx.client.get<Array<{ id: number; name: string; color: string }>>( `/repos/${owner}/${repo}/labels` ); const existingLabelMap = new Map(existingLabels.map((l) => [l.name, l])); // 获取配置中的所有标签 const configLabels = getAllLabels(config); for (const { name, config: labelConfig } of configLabels) { try { const existing = existingLabelMap.get(name); if (existing) { // 检查是否需要更新 if (existing.color !== labelConfig.color) { if (!dryRun) { await ctx.client.patch(`/repos/${owner}/${repo}/labels/${existing.id}`, { color: labelConfig.color, description: labelConfig.description, }); } updated.push(name); } else { skipped.push(name); } } else { // 创建新标签 if (!dryRun) { await ctx.client.post(`/repos/${owner}/${repo}/labels`, { name, color: labelConfig.color, description: labelConfig.description, }); } created.push(name); } } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); errors.push(`标签 ${name}: ${errorMessage}`); } } logger.info( { owner, repo, created: created.length, updated: updated.length, skipped: skipped.length }, 'Labels synced' ); return { success: errors.length === 0, created, updated, skipped, errors, }; }
- src/tools-registry/workflow-registry.ts:87-117 (registration)Registers the 'gitea_workflow_sync_labels' tool with the MCP server, defining title, description, Zod input schema, and a wrapper handler that calls the core workflowSyncLabels function and formats the response.mcpServer.registerTool( 'gitea_workflow_sync_labels', { title: '同步标签系统', description: 'Sync repository labels based on workflow configuration. Creates status/*, priority/*, type/* and other labels defined in the config.', 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'), dry_run: z.boolean().optional().describe('Preview changes without applying (default: false)'), }), }, async (args) => { try { const result = await WorkflowTools.workflowSyncLabels( { client: ctx.client, contextManager: ctx.contextManager }, args ); return { content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }], isError: !result.success, }; } 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 for the tool defining parameters: owner, repo (optional), dry_run (optional boolean).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'), dry_run: z.boolean().optional().describe('Preview changes without applying (default: false)'), }),
- src/tools/workflow.ts:21-34 (helper)Imports helper functions like getAllLabels used in the handler to retrieve all labels from config.import { type WorkflowConfig, type ProjectType, generateDefaultConfig, serializeConfig, parseConfig, validateConfig, getAllLabels, getSLAHours, getLabelPrefixes, buildLabel, matchLabel, resolveRulePlaceholders, } from '../utils/workflow-config.js';