All files / src parse-title-options.ts

98.7% Statements 76/77
92.06% Branches 58/63
100% Functions 6/6
98.63% Lines 72/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                                            35x     35x 35x 4x     31x 31x   31x 39x   39x     10x 10x 9x   10x         9x 9x 7x 7x 7x 7x         2x   9x         5x 5x       4x 4x 4x 3x   4x   4x       1x 1x     2x 2x     6x 6x 5x 5x   1x   6x       1x 1x     1x 1x 1x   1x         31x               31x 31x 31x 31x   31x 449x   449x 1x 1x 448x 1x 1x 447x 35x 35x 35x     412x       31x 31x     31x       10x       1x       5x       5x 4x 3x    
import type { LLMMode, VerboseLevel } from "@spaceflow/core";
import type { AnalyzeDeletionsMode } from "./review.config";
import { normalizeVerbose } from "@spaceflow/core";
 
/**
 * 从 PR 标题中解析的命令参数
 */
export interface TitleOptions {
  llmMode?: LLMMode;
  verbose?: VerboseLevel;
  dryRun?: boolean;
  includes?: string[];
  verifyFixes?: boolean;
  analyzeDeletions?: AnalyzeDeletionsMode;
  deletionOnly?: boolean;
  deletionAnalysisMode?: LLMMode;
}
 
/**
 * 从 PR 标题中解析命令参数
 *
 * 支持的格式:标题末尾 [/review -l openai -v 2]
 *
 * 支持的参数:
 * - `-l, --llm-mode <mode>`: LLM 模式 (claude-code, openai, gemini)
 * - `-v, --verbose [level]`: 详细输出级别 (1 或 2)
 * - `-d, --dry-run`: 仅打印不执行
 * - `-i, --includes <pattern>`: 文件过滤模式
 * - `--verify-fixes`: 验证历史问题
 * - `--no-verify-fixes`: 禁用历史问题验证
 * - `--analyze-deletions`: 分析删除代码
 * - `--deletion-only`: 仅执行删除代码分析
 * - `--deletion-analysis-mode <mode>`: 删除分析模式
 *
 * @param title PR 标题
 * @returns 解析出的命令参数,如果没有找到命令则返回空对象
 */
export function parseTitleOptions(title: string): TitleOptions {
  const options: TitleOptions = {};
 
  // 匹配 [/review ...] 或 [/ai-review ...] (保持向后兼容) 格式
  const match = title.match(/\[\/(review|ai-review)\s+([^\]]+)\]/i);
  if (!match) {
    return options;
  }
 
  const argsString = match[2].trim();
  const args = parseArgs(argsString);
 
  for (let i = 0; i < args.length; i++) {
    const arg = args[i];
 
    switch (arg) {
      case "-l":
      case "--llm-mode": {
        const value = args[++i];
        if (value && isValidLLMMode(value)) {
          options.llmMode = value;
        }
        break;
      }
 
      case "-v":
      case "--verbose": {
        const nextArg = args[i + 1];
        if (nextArg && !nextArg.startsWith("-")) {
          const level = parseInt(nextArg, 10);
          if (level === 1 || level === 2) {
            options.verbose = level;
            i++;
          } else E{
            options.verbose = normalizeVerbose(1);
          }
        } else {
          options.verbose = normalizeVerbose(1);
        }
        break;
      }
 
      case "-d":
      case "--dry-run":
        options.dryRun = true;
        break;
 
      case "-i":
      case "--includes": {
        const value = args[++i];
        Eif (value && !value.startsWith("-")) {
          if (!options.includes) {
            options.includes = [];
          }
          options.includes.push(value);
        }
        break;
      }
 
      case "--verify-fixes":
        options.verifyFixes = true;
        break;
 
      case "--no-verify-fixes":
        options.verifyFixes = false;
        break;
 
      case "--analyze-deletions": {
        const nextArg = args[i + 1];
        if (nextArg && !nextArg.startsWith("-") && isValidAnalyzeDeletionsMode(nextArg)) {
          options.analyzeDeletions = parseAnalyzeDeletionsValue(nextArg);
          i++;
        } else {
          options.analyzeDeletions = true;
        }
        break;
      }
 
      case "--deletion-only":
        options.deletionOnly = true;
        break;
 
      case "--deletion-analysis-mode": {
        const value = args[++i];
        Eif (value && isValidDeletionAnalysisMode(value)) {
          options.deletionAnalysisMode = value;
        }
        break;
      }
    }
  }
 
  return options;
}
 
/**
 * 解析参数字符串为数组
 * 支持引号包裹的参数值
 */
function parseArgs(argsString: string): string[] {
  const args: string[] = [];
  let current = "";
  let inQuote = false;
  let quoteChar = "";
 
  for (let i = 0; i < argsString.length; i++) {
    const char = argsString[i];
 
    if ((char === '"' || char === "'") && !inQuote) {
      inQuote = true;
      quoteChar = char;
    } else if (char === quoteChar && inQuote) {
      inQuote = false;
      quoteChar = "";
    } else if (char === " " && !inQuote) {
      Eif (current) {
        args.push(current);
        current = "";
      }
    } else {
      current += char;
    }
  }
 
  Eif (current) {
    args.push(current);
  }
 
  return args;
}
 
function isValidLLMMode(value: string): value is LLMMode {
  return ["claude-code", "openai", "gemini"].includes(value);
}
 
function isValidDeletionAnalysisMode(value: string): value is LLMMode {
  return ["openai", "claude-code"].includes(value);
}
 
function isValidAnalyzeDeletionsMode(value: string): boolean {
  return ["true", "false", "ci", "pr", "terminal"].includes(value);
}
 
function parseAnalyzeDeletionsValue(value: string): AnalyzeDeletionsMode {
  if (value === "true") return true;
  if (value === "false") return false;
  return value as "ci" | "pr" | "terminal";
}