|
1 | | -const PROVIDER_ATTRIBUTION_MARKERS = [ |
2 | | - "claude code", |
3 | | - "anthropic", |
4 | | - "codex", |
5 | | - "openai codex", |
6 | | - "openai", |
7 | | - "github copilot", |
8 | | - "copilot", |
9 | | - "cursor", |
10 | | - "gemini", |
11 | | - "noreply@anthropic.com", |
12 | | - "noreply@openai.com", |
13 | | - "copilot@github.com", |
14 | | -] as const; |
15 | | - |
16 | | -const TRAILER_LINE_PATTERN = /^(?:co-authored-by|signed-off-by):/i; |
17 | | -const ATTRIBUTION_LINE_PATTERN = |
18 | | - /^(?:this (?:commit|pull request|pr) was\s+)?(?:generated|created|authored|written)\s+(?:with|by)\s+(.+?)(?:[.!])?$/i; |
19 | | - |
20 | | -function normalizeAttributionLine(line: string): string { |
21 | | - return line |
22 | | - .trim() |
23 | | - .replace(/\[([^\]]+)\]\((?:[^)]+)\)/g, "$1") |
24 | | - .replace(/^[-*]\s+/, "") |
25 | | - .replace(/^🤖\s*/, "") |
26 | | - .replace(/\s+/g, " "); |
27 | | -} |
28 | | - |
29 | | -function containsProviderAttributionMarker(value: string): boolean { |
30 | | - const lower = value.toLowerCase(); |
31 | | - return PROVIDER_ATTRIBUTION_MARKERS.some((marker) => lower.includes(marker)); |
32 | | -} |
33 | | - |
34 | | -function isLikelyProviderLabel(value: string): boolean { |
35 | | - const normalized = value |
36 | | - .trim() |
37 | | - .replace(/[()[\]{}"'`]/g, "") |
38 | | - .replace(/\s+/g, " "); |
39 | | - if (!containsProviderAttributionMarker(normalized)) { |
40 | | - return false; |
41 | | - } |
42 | | - return normalized.split(" ").filter(Boolean).length <= 4; |
43 | | -} |
44 | | - |
45 | | -function isProviderAttributionLine(line: string): boolean { |
46 | | - const normalized = normalizeAttributionLine(line); |
47 | | - if (normalized.length === 0) { |
48 | | - return false; |
49 | | - } |
50 | | - |
51 | | - if (TRAILER_LINE_PATTERN.test(normalized) && containsProviderAttributionMarker(normalized)) { |
52 | | - return true; |
53 | | - } |
54 | | - |
55 | | - const attributionMatch = normalized.match(ATTRIBUTION_LINE_PATTERN); |
56 | | - if (!attributionMatch) { |
57 | | - return false; |
58 | | - } |
59 | | - |
60 | | - const tail = attributionMatch[1] ?? ""; |
61 | | - return isLikelyProviderLabel(tail); |
62 | | -} |
63 | | - |
64 | | -function trimBlankLines(lines: readonly string[]): string[] { |
65 | | - let start = 0; |
66 | | - let end = lines.length; |
67 | | - |
68 | | - while (start < end && lines[start]?.trim().length === 0) { |
69 | | - start += 1; |
70 | | - } |
71 | | - while (end > start && lines[end - 1]?.trim().length === 0) { |
72 | | - end -= 1; |
73 | | - } |
74 | | - |
75 | | - const trimmed = lines.slice(start, end); |
76 | | - const compacted: string[] = []; |
77 | | - let previousWasBlank = false; |
78 | | - |
79 | | - for (const line of trimmed) { |
80 | | - const normalizedLine = line.trimEnd(); |
81 | | - const isBlank = normalizedLine.length === 0; |
82 | | - if (isBlank) { |
83 | | - if (previousWasBlank) { |
84 | | - continue; |
85 | | - } |
86 | | - previousWasBlank = true; |
87 | | - compacted.push(""); |
88 | | - continue; |
89 | | - } |
90 | | - |
91 | | - previousWasBlank = false; |
92 | | - compacted.push(normalizedLine); |
93 | | - } |
94 | | - |
95 | | - return compacted; |
96 | | -} |
97 | | - |
98 | | -export function stripProviderAttribution(raw: string): string { |
99 | | - const normalized = raw.replace(/\r\n?/g, "\n"); |
100 | | - const keptLines = normalized.split("\n").filter((line) => !isProviderAttributionLine(line)); |
101 | | - return trimBlankLines(keptLines).join("\n").trim(); |
102 | | -} |
103 | | - |
104 | | -export function sanitizeGeneratedCommitSubject(raw: string): string { |
105 | | - const sanitized = stripProviderAttribution(raw); |
106 | | - const singleLine = sanitized.split("\n")[0]?.trim() ?? ""; |
107 | | - const withoutTrailingPeriod = singleLine.replace(/[.]+$/g, "").trim(); |
108 | | - if (withoutTrailingPeriod.length === 0) { |
109 | | - return "Update project files"; |
110 | | - } |
111 | | - |
112 | | - if (withoutTrailingPeriod.length <= 72) { |
113 | | - return withoutTrailingPeriod; |
114 | | - } |
115 | | - return withoutTrailingPeriod.slice(0, 72).trimEnd(); |
116 | | -} |
117 | | - |
118 | | -export function sanitizeGeneratedCommitBody(raw: string): string { |
119 | | - return stripProviderAttribution(raw); |
120 | | -} |
121 | | - |
122 | | -export function sanitizeGeneratedPrTitle(raw: string): string { |
123 | | - const sanitized = stripProviderAttribution(raw); |
124 | | - const singleLine = sanitized.split("\n")[0]?.trim() ?? ""; |
125 | | - if (singleLine.length > 0) { |
126 | | - return singleLine; |
127 | | - } |
128 | | - return "Update project changes"; |
129 | | -} |
130 | | - |
131 | | -export function sanitizeGeneratedPrBody(raw: string): string { |
132 | | - return stripProviderAttribution(raw); |
133 | | -} |
| 1 | +export { |
| 2 | + sanitizeGeneratedCommitBody, |
| 3 | + sanitizeGeneratedCommitSubject, |
| 4 | + sanitizeGeneratedPrBody, |
| 5 | + sanitizeGeneratedPrTitle, |
| 6 | + stripProviderAttribution, |
| 7 | +} from "@okcode/shared/generatedTextSanitization"; |
0 commit comments