|
| 1 | +/** |
| 2 | + * GenAI Detection Logic |
| 3 | + * |
| 4 | + * Detects AI assistance in PRs by checking: |
| 5 | + * - Existing labels |
| 6 | + * - PR author (bot detection) |
| 7 | + * - PR body/description |
| 8 | + * - Commit messages and co-authors |
| 9 | + * - PR comments |
| 10 | + */ |
| 11 | + |
| 12 | +const { |
| 13 | + BOT_PATTERNS, |
| 14 | + COMMIT_PATTERNS, |
| 15 | + PR_BODY_PATTERNS, |
| 16 | + COMMENT_PATTERNS, |
| 17 | + AI_LABELS, |
| 18 | + GENAI_LABEL, |
| 19 | +} = require("./genaiPatterns"); |
| 20 | + |
| 21 | +/** |
| 22 | + * Check if a string matches any pattern in an array |
| 23 | + * @param {string} text - Text to check |
| 24 | + * @param {RegExp[]} patterns - Array of regex patterns |
| 25 | + * @returns {RegExp|null} - The matching pattern or null |
| 26 | + */ |
| 27 | +function matchesAnyPattern(text, patterns) { |
| 28 | + if (!text) return null; |
| 29 | + for (const pattern of patterns) { |
| 30 | + if (pattern.test(text)) { |
| 31 | + return pattern; |
| 32 | + } |
| 33 | + } |
| 34 | + return null; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Check if author is a bot |
| 39 | + * @param {string} author - Author login or name |
| 40 | + * @returns {RegExp|null} - The matching pattern or null |
| 41 | + */ |
| 42 | +function isBot(author) { |
| 43 | + return matchesAnyPattern(author, BOT_PATTERNS); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Check if any existing label indicates AI assistance |
| 48 | + * @param {Array<{name: string}>} labels - Array of label objects |
| 49 | + * @returns {string|null} - The matching label or null |
| 50 | + */ |
| 51 | +function hasAILabel(labels) { |
| 52 | + if (!labels || !Array.isArray(labels)) return null; |
| 53 | + for (const label of labels) { |
| 54 | + const labelName = (label.name || label).toLowerCase(); |
| 55 | + if (AI_LABELS.includes(labelName)) { |
| 56 | + return labelName; |
| 57 | + } |
| 58 | + } |
| 59 | + return null; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Check if the genai-assisted label already exists |
| 64 | + * @param {Array<{name: string}>} labels - Array of label objects |
| 65 | + * @returns {boolean} |
| 66 | + */ |
| 67 | +function hasGenAILabel(labels) { |
| 68 | + if (!labels || !Array.isArray(labels)) return false; |
| 69 | + return labels.some( |
| 70 | + (label) => (label.name || label).toLowerCase() === GENAI_LABEL |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Detect GenAI assistance in a PR |
| 76 | + * @param {Object} octokit - GitHub API client |
| 77 | + * @param {Object} params - Detection parameters |
| 78 | + * @param {string} params.owner - Repository owner |
| 79 | + * @param {string} params.repo - Repository name |
| 80 | + * @param {number} params.pullNumber - PR number |
| 81 | + * @param {Object} params.pullRequest - PR object from GitHub context |
| 82 | + * @returns {Promise<{detected: boolean, reason: string|null}>} |
| 83 | + */ |
| 84 | +async function detectGenAI(octokit, { owner, repo, pullNumber, pullRequest }) { |
| 85 | + // 1. Check if already labeled with genai-assisted |
| 86 | + if (hasGenAILabel(pullRequest.labels)) { |
| 87 | + console.log("PR already has genai-assisted label, skipping detection"); |
| 88 | + return { detected: false, reason: null, alreadyLabeled: true }; |
| 89 | + } |
| 90 | + |
| 91 | + // 2. Check PR author (bot detection) |
| 92 | + const prAuthor = pullRequest.user?.login || pullRequest.user?.name; |
| 93 | + const botMatch = isBot(prAuthor); |
| 94 | + if (botMatch) { |
| 95 | + return { |
| 96 | + detected: true, |
| 97 | + reason: `PR author "${prAuthor}" matches bot pattern: ${botMatch}`, |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + // 3. Check PR body/description |
| 102 | + const bodyMatch = matchesAnyPattern(pullRequest.body, PR_BODY_PATTERNS); |
| 103 | + if (bodyMatch) { |
| 104 | + return { |
| 105 | + detected: true, |
| 106 | + reason: `PR body matches GenAI pattern: ${bodyMatch}`, |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + // 4. Check existing labels for AI-related labels |
| 111 | + const aiLabel = hasAILabel(pullRequest.labels); |
| 112 | + if (aiLabel) { |
| 113 | + return { |
| 114 | + detected: true, |
| 115 | + reason: `PR has AI-related label: "${aiLabel}"`, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + // 5. Fetch and check commits |
| 120 | + try { |
| 121 | + const commits = await octokit.paginate( |
| 122 | + "GET /repos/{owner}/{repo}/pulls/{pull_number}/commits", |
| 123 | + { owner, repo, pull_number: pullNumber } |
| 124 | + ); |
| 125 | + |
| 126 | + for (const commit of commits) { |
| 127 | + // Check commit message |
| 128 | + const message = commit.commit?.message || ""; |
| 129 | + const messageMatch = matchesAnyPattern(message, COMMIT_PATTERNS); |
| 130 | + if (messageMatch) { |
| 131 | + return { |
| 132 | + detected: true, |
| 133 | + reason: `Commit "${commit.sha.slice(0, 7)}" message matches GenAI pattern: ${messageMatch}`, |
| 134 | + }; |
| 135 | + } |
| 136 | + |
| 137 | + // Check commit author |
| 138 | + const commitAuthor = |
| 139 | + commit.author?.login || commit.commit?.author?.name || ""; |
| 140 | + const commitAuthorMatch = isBot(commitAuthor); |
| 141 | + if (commitAuthorMatch) { |
| 142 | + return { |
| 143 | + detected: true, |
| 144 | + reason: `Commit "${commit.sha.slice(0, 7)}" author "${commitAuthor}" matches bot pattern: ${commitAuthorMatch}`, |
| 145 | + }; |
| 146 | + } |
| 147 | + |
| 148 | + // Check committer |
| 149 | + const committer = |
| 150 | + commit.committer?.login || commit.commit?.committer?.name || ""; |
| 151 | + const committerMatch = isBot(committer); |
| 152 | + if (committerMatch) { |
| 153 | + return { |
| 154 | + detected: true, |
| 155 | + reason: `Commit "${commit.sha.slice(0, 7)}" committer "${committer}" matches bot pattern: ${committerMatch}`, |
| 156 | + }; |
| 157 | + } |
| 158 | + } |
| 159 | + } catch (error) { |
| 160 | + console.log(`Warning: Could not fetch commits: ${error.message}`); |
| 161 | + } |
| 162 | + |
| 163 | + // 6. Fetch and check PR comments |
| 164 | + try { |
| 165 | + const comments = await octokit.paginate( |
| 166 | + "GET /repos/{owner}/{repo}/issues/{issue_number}/comments", |
| 167 | + { owner, repo, issue_number: pullNumber } |
| 168 | + ); |
| 169 | + |
| 170 | + for (const comment of comments) { |
| 171 | + // Check comment author |
| 172 | + const commentAuthor = comment.user?.login || ""; |
| 173 | + const authorMatch = isBot(commentAuthor); |
| 174 | + if (authorMatch) { |
| 175 | + return { |
| 176 | + detected: true, |
| 177 | + reason: `Comment author "${commentAuthor}" matches bot pattern: ${authorMatch}`, |
| 178 | + }; |
| 179 | + } |
| 180 | + |
| 181 | + // Check comment body |
| 182 | + const commentMatch = matchesAnyPattern(comment.body, COMMENT_PATTERNS); |
| 183 | + if (commentMatch) { |
| 184 | + return { |
| 185 | + detected: true, |
| 186 | + reason: `Comment by "${commentAuthor}" matches GenAI pattern: ${commentMatch}`, |
| 187 | + }; |
| 188 | + } |
| 189 | + } |
| 190 | + } catch (error) { |
| 191 | + console.log(`Warning: Could not fetch comments: ${error.message}`); |
| 192 | + } |
| 193 | + |
| 194 | + // No GenAI detected |
| 195 | + return { detected: false, reason: null }; |
| 196 | +} |
| 197 | + |
| 198 | +module.exports = { |
| 199 | + detectGenAI, |
| 200 | + matchesAnyPattern, |
| 201 | + isBot, |
| 202 | + hasAILabel, |
| 203 | + hasGenAILabel, |
| 204 | + GENAI_LABEL, |
| 205 | +}; |
0 commit comments