|
| 1 | +/** |
| 2 | + * 알고리즘 패턴 태깅 핸들러 |
| 3 | + * |
| 4 | + * PR의 솔루션 파일들을 분석하여 사용된 알고리즘 패턴을 |
| 5 | + * 파일별 review comment로 남긴다. |
| 6 | + */ |
| 7 | + |
| 8 | +import { getGitHubHeaders } from "../utils/github.js"; |
| 9 | +import { hasMaintenanceLabel } from "../utils/validation.js"; |
| 10 | +import { generatePatternAnalysis } from "../utils/openai.js"; |
| 11 | + |
| 12 | +const COMMENT_MARKER = "<!-- dalestudy-pattern-tag -->"; |
| 13 | +const SOLUTION_PATH_REGEX = /^[^/]+\/[^/]+\.[^.]+$/; |
| 14 | +const MAX_FILE_SIZE = 20000; // 20K 문자 제한 (OpenAI 토큰 안전장치) |
| 15 | + |
| 16 | +/** |
| 17 | + * PR의 솔루션 파일들에 알고리즘 패턴 태그 달기 |
| 18 | + * |
| 19 | + * @param {string} repoOwner |
| 20 | + * @param {string} repoName |
| 21 | + * @param {number} prNumber |
| 22 | + * @param {string} headSha - PR head commit SHA |
| 23 | + * @param {object} prData - PR 객체 (draft, labels 포함) |
| 24 | + * @param {string} appToken - GitHub App installation token |
| 25 | + * @param {string} openaiApiKey |
| 26 | + */ |
| 27 | +export async function tagPatterns( |
| 28 | + repoOwner, |
| 29 | + repoName, |
| 30 | + prNumber, |
| 31 | + headSha, |
| 32 | + prData, |
| 33 | + appToken, |
| 34 | + openaiApiKey |
| 35 | +) { |
| 36 | + // 2-1. Skip 조건 |
| 37 | + if (prData.draft === true) { |
| 38 | + console.log(`[tagPatterns] Skipping PR #${prNumber}: draft`); |
| 39 | + return { skipped: "draft" }; |
| 40 | + } |
| 41 | + |
| 42 | + const labels = (prData.labels || []).map((l) => l.name); |
| 43 | + if (hasMaintenanceLabel(labels)) { |
| 44 | + console.log(`[tagPatterns] Skipping PR #${prNumber}: maintenance label`); |
| 45 | + return { skipped: "maintenance" }; |
| 46 | + } |
| 47 | + |
| 48 | + // 2-2. PR 변경 파일 목록 조회 + 필터링 |
| 49 | + const filesResponse = await fetch( |
| 50 | + `https://api.github.com/repos/${repoOwner}/${repoName}/pulls/${prNumber}/files?per_page=100`, |
| 51 | + { headers: getGitHubHeaders(appToken) } |
| 52 | + ); |
| 53 | + |
| 54 | + if (!filesResponse.ok) { |
| 55 | + throw new Error( |
| 56 | + `Failed to list PR files: ${filesResponse.status} ${filesResponse.statusText}` |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + const allFiles = await filesResponse.json(); |
| 61 | + const solutionFiles = allFiles.filter( |
| 62 | + (f) => |
| 63 | + (f.status === "added" || f.status === "modified") && |
| 64 | + SOLUTION_PATH_REGEX.test(f.filename) |
| 65 | + ); |
| 66 | + |
| 67 | + console.log( |
| 68 | + `[tagPatterns] PR #${prNumber}: ${allFiles.length} files, ${solutionFiles.length} solution files` |
| 69 | + ); |
| 70 | + |
| 71 | + if (solutionFiles.length === 0) { |
| 72 | + return { skipped: "no-solution-files" }; |
| 73 | + } |
| 74 | + |
| 75 | + // 2-3. 기존 Bot 패턴 태그 코멘트 삭제 |
| 76 | + await deletePreviousPatternComments(repoOwner, repoName, prNumber, appToken); |
| 77 | + |
| 78 | + // 2-4. 파일별 OpenAI 분석 + 코멘트 작성 (각 파일 try/catch 래핑) |
| 79 | + const results = []; |
| 80 | + for (const file of solutionFiles) { |
| 81 | + try { |
| 82 | + const result = await tagSingleFile( |
| 83 | + file, |
| 84 | + repoOwner, |
| 85 | + repoName, |
| 86 | + prNumber, |
| 87 | + headSha, |
| 88 | + appToken, |
| 89 | + openaiApiKey |
| 90 | + ); |
| 91 | + results.push({ path: file.filename, ...result }); |
| 92 | + } catch (error) { |
| 93 | + console.error( |
| 94 | + `[tagPatterns] Failed to tag ${file.filename}: ${error.message}` |
| 95 | + ); |
| 96 | + results.push({ path: file.filename, error: error.message }); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return { tagged: results.filter((r) => !r.error).length, results }; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * 기존 Bot 패턴 태그 코멘트 삭제 (다른 사용자 코멘트는 절대 건드리지 않음) |
| 105 | + */ |
| 106 | +async function deletePreviousPatternComments( |
| 107 | + repoOwner, |
| 108 | + repoName, |
| 109 | + prNumber, |
| 110 | + appToken |
| 111 | +) { |
| 112 | + const response = await fetch( |
| 113 | + `https://api.github.com/repos/${repoOwner}/${repoName}/pulls/${prNumber}/comments?per_page=100`, |
| 114 | + { headers: getGitHubHeaders(appToken) } |
| 115 | + ); |
| 116 | + |
| 117 | + if (!response.ok) { |
| 118 | + console.error( |
| 119 | + `[tagPatterns] Failed to fetch review comments: ${response.status}` |
| 120 | + ); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + const comments = await response.json(); |
| 125 | + const botPatternComments = comments.filter( |
| 126 | + (c) => c.user?.type === "Bot" && c.body?.includes(COMMENT_MARKER) |
| 127 | + ); |
| 128 | + |
| 129 | + for (const comment of botPatternComments) { |
| 130 | + try { |
| 131 | + const deleteResponse = await fetch( |
| 132 | + `https://api.github.com/repos/${repoOwner}/${repoName}/pulls/comments/${comment.id}`, |
| 133 | + { |
| 134 | + method: "DELETE", |
| 135 | + headers: getGitHubHeaders(appToken), |
| 136 | + } |
| 137 | + ); |
| 138 | + |
| 139 | + if (!deleteResponse.ok) { |
| 140 | + console.error( |
| 141 | + `[tagPatterns] Failed to delete comment ${comment.id}: ${deleteResponse.status}` |
| 142 | + ); |
| 143 | + } |
| 144 | + } catch (error) { |
| 145 | + console.error( |
| 146 | + `[tagPatterns] Error deleting comment ${comment.id}: ${error.message}` |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + console.log( |
| 152 | + `[tagPatterns] Deleted ${botPatternComments.length} previous pattern comments` |
| 153 | + ); |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * 단일 파일 분석 + 코멘트 작성 |
| 158 | + */ |
| 159 | +async function tagSingleFile( |
| 160 | + file, |
| 161 | + repoOwner, |
| 162 | + repoName, |
| 163 | + prNumber, |
| 164 | + headSha, |
| 165 | + appToken, |
| 166 | + openaiApiKey |
| 167 | +) { |
| 168 | + // 파일 내용 가져오기 |
| 169 | + const contentResponse = await fetch(file.raw_url); |
| 170 | + if (!contentResponse.ok) { |
| 171 | + throw new Error(`Failed to fetch raw content: ${contentResponse.status}`); |
| 172 | + } |
| 173 | + |
| 174 | + let fileContent = await contentResponse.text(); |
| 175 | + if (fileContent.length > MAX_FILE_SIZE) { |
| 176 | + fileContent = fileContent.slice(0, MAX_FILE_SIZE); |
| 177 | + console.log( |
| 178 | + `[tagPatterns] Truncated ${file.filename} to ${MAX_FILE_SIZE} chars` |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + // 폴더명(=문제 이름) 추출 |
| 183 | + const problemName = file.filename.split("/")[0]; |
| 184 | + |
| 185 | + // OpenAI 패턴 분석 |
| 186 | + const analysis = await generatePatternAnalysis( |
| 187 | + fileContent, |
| 188 | + problemName, |
| 189 | + openaiApiKey |
| 190 | + ); |
| 191 | + |
| 192 | + // 코멘트 본문 작성 |
| 193 | + const patternsText = |
| 194 | + analysis.patterns.length > 0 ? analysis.patterns.join(", ") : "감지된 패턴 없음"; |
| 195 | + const body = `${COMMENT_MARKER} |
| 196 | +### 🏷️ 알고리즘 패턴 분석 |
| 197 | +
|
| 198 | +- **패턴**: ${patternsText} |
| 199 | +- **설명**: ${analysis.description || "(설명 없음)"}`; |
| 200 | + |
| 201 | + // 파일 단위 review comment 작성 |
| 202 | + const commentResponse = await fetch( |
| 203 | + `https://api.github.com/repos/${repoOwner}/${repoName}/pulls/${prNumber}/comments`, |
| 204 | + { |
| 205 | + method: "POST", |
| 206 | + headers: { |
| 207 | + ...getGitHubHeaders(appToken), |
| 208 | + "Content-Type": "application/json", |
| 209 | + }, |
| 210 | + body: JSON.stringify({ |
| 211 | + body, |
| 212 | + commit_id: headSha, |
| 213 | + path: file.filename, |
| 214 | + subject_type: "file", |
| 215 | + }), |
| 216 | + } |
| 217 | + ); |
| 218 | + |
| 219 | + if (!commentResponse.ok) { |
| 220 | + const errorText = await commentResponse.text(); |
| 221 | + throw new Error( |
| 222 | + `Failed to post review comment: ${commentResponse.status} ${errorText}` |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | + return { patterns: analysis.patterns }; |
| 227 | +} |
0 commit comments