UNPKG

@elsikora/commitizen-plugin-commitlint-ai

Version:
77 lines (73 loc) 3.52 kB
'use strict'; var numeric_constant = require('../../domain/constant/numeric.constant.js'); /** * Use case for validating and fixing commit messages */ class ValidateCommitMessageUseCase { DEFAULT_MAX_RETRIES = numeric_constant.DEFAULT_VALIDATION_MAX_RETRIES; VALIDATOR; constructor(validator, defaultMaxRetries = numeric_constant.DEFAULT_VALIDATION_MAX_RETRIES) { this.VALIDATOR = validator; this.DEFAULT_MAX_RETRIES = defaultMaxRetries; } /** * Execute the validation use case * @param {CommitMessage} message - The commit message to validate * @param {boolean} shouldAttemptFix - Whether to attempt fixing validation errors * @param {number | undefined} maxRetries - Maximum number of retry attempts (optional, defaults to DEFAULT_MAX_RETRIES) * @param {ILlmPromptContext} context - The LLM prompt context * @returns {Promise<CommitMessage | null>} Promise resolving to the validated message or null if validation fails */ async execute(message, shouldAttemptFix = false, maxRetries, context) { const retryLimit = maxRetries ?? this.DEFAULT_MAX_RETRIES; let currentMessage = message; let attempts = 0; while (attempts <= retryLimit) { const validationResult = await this.validate(currentMessage); if (validationResult.isValid) { if (attempts > 0) { process.stdout.write(`✓ Commit message fixed after ${attempts} attempt${attempts > 1 ? "s" : ""}\n`); } return currentMessage; } // If we shouldn't attempt fix or we've exhausted all retries if (!shouldAttemptFix || attempts >= retryLimit) { if (validationResult.errors && validationResult.errors.length > 0) { process.stdout.write(`✗ Commit message validation failed after ${attempts} attempts:\n`); for (const error of validationResult.errors) { process.stdout.write(` - ${error}\n`); } } return null; } // Attempt to fix attempts++; process.stdout.write(`Attempting to fix commit message (attempt ${attempts}/${retryLimit})...\n`); try { const fixedMessage = await this.VALIDATOR.fix(currentMessage, validationResult, context); if (!fixedMessage) { process.stdout.write("Unable to automatically fix the commit message\n"); return null; } process.stdout.write("Fixed commit message generated\n"); currentMessage = fixedMessage; } catch (error) { process.stdout.write(`Error during fix attempt: ${error instanceof Error ? error.message : String(error)}\n`); return null; } } process.stdout.write(`Unable to generate valid commit message after ${retryLimit} attempts\n`); return null; } /** * Validate a commit message * @param {CommitMessage} message - The commit message to validate * @returns {Promise<ICommitValidationResult>} Promise resolving to the validation result */ async validate(message) { return this.VALIDATOR.validate(message); } } exports.ValidateCommitMessageUseCase = ValidateCommitMessageUseCase; //# sourceMappingURL=validate-commit-message.use-case.js.map