From 2b99dce66cc62efa0bb333d56afaacff15f4e8bc Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Mon, 13 Nov 2023 17:10:09 -0500 Subject: [PATCH 1/8] Move const prompt --- utils/actions/detectConsoleLogs.ts | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 0523731a..3431ce8b 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -18,6 +18,21 @@ const app = new App({ privateKey: process.env.GITHUB_PRIVATE_KEY!, }); +const consoleLogDetectionPrompt = `This is a list of code additions. Identify +if there's a console log or its equivalent in another programming language +such as Java, Golang, Python, C, Rust, C++, Ruby, etc. +(console.log(), println(), println!(), System.out.println(), print(), fmt.Println(), puts, and cout << "Print a String" << endl; are some examples). +If the console log or its equivalent in another language is in a code comment, don't +count it as a detected console log. For example JavaScript comments start with // or /*, +Python comments start with #. +Other console functions such as console.info() shouldn't be counted as console logs. +Ignore code comments from this analysis. +Something like 'input[type="email"]' is fine and should not be counted as a console log. +If there is a console log, return "true", else return "false". +If you return true, return a string that that has 2 values: result (true) and the line of code. +The line value, is the actual line in the file that contains the console log. +For example: true,console.log("hello world");`; + function getAdditions(filePatch: string) { const additions: string[] = []; @@ -116,21 +131,6 @@ export default async function detectConsoleLogs({ const commentPromises = diffFiles.map(async (file) => { const additions = getAdditions(file.patch ?? ""); - const consoleLogDetectionPrompt = `This is a list of code additions. Identify - if there's a console log or its equivalent in another programming language - such as Java, Golang, Python, C, Rust, C++, Ruby, etc. - (console.log(), println(), println!(), System.out.println(), print(), fmt.Println(), puts, and cout << "Print a String" << endl; are some examples). - If the console log or its equivalent in another language is in a code comment, don't - count it as a detected console log. For example JavaScript comments start with // or /*, - Python comments start with #. - Other console functions such as console.info() shouldn't be counted as console logs. - Ignore code comments from this analysis. - Something like 'input[type="email"]' is fine and should not be counted as a console log. - If there is a console log, return "true", else return "false". - If you return true, return a string that that has 2 values: result (true) and the line of code. - The line value, is the actual line in the file that contains the console log. - For example: true,console.log("hello world");`; - // detect if the additions contain console logs or not try { return await openai From 15e13cdf680df570c76d323b0e2e724805499456 Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Mon, 13 Nov 2023 19:32:16 -0500 Subject: [PATCH 2/8] Extract hash getter --- utils/actions/detectConsoleLogs.ts | 71 ++++++++++++++++-------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 3431ce8b..a76843d1 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -128,6 +128,22 @@ export default async function detectConsoleLogs({ } ); + function getLatestCommitHash() { + return octokit + .request("GET /repos/{owner}/{repo}/pulls/{pull_number}", { + owner, + repo, + pull_number: issue_number, + }) + .then((result) => { + return result.data.head.sha; + }) + .catch((err) => { + console.log(err); + }); + } + const latestCommitHash = getLatestCommitHash(); + const commentPromises = diffFiles.map(async (file) => { const additions = getAdditions(file.patch ?? ""); @@ -152,43 +168,30 @@ export default async function detectConsoleLogs({ if (addtionsHaveConsoleLog === "true") { const commentFileDiff = () => { + const consoleLogPosition = getConsoleLogPosition({ + filePatch: file.patch ?? "", + individualLine, + }); + return octokit - .request("GET /repos/{owner}/{repo}/pulls/{pull_number}", { - owner, - repo, - pull_number: issue_number, - }) - .then((result) => { - const latestCommitHash = result.data.head.sha; - - const consoleLogPosition = getConsoleLogPosition({ - filePatch: file.patch ?? "", - individualLine, - }); - - return octokit - .request( - "POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", + .request( + "POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", + { + owner, + repo, + pull_number: issue_number, + commit_id: latestCommitHash, + event: "COMMENT", + path: file.filename, + comments: [ { - owner, - repo, - pull_number: issue_number, - commit_id: latestCommitHash, - event: "COMMENT", path: file.filename, - comments: [ - { - path: file.filename, - position: consoleLogPosition || 1, // comment at the beggining of the file by default - body: "This file contains at least one console log. Please remove any present.", - }, - ], - } - ) - .catch((err) => { - console.log(err); - }); - }) + position: consoleLogPosition || 1, // comment at the beggining of the file by default + body: "This file contains at least one console log. Please remove any present.", + }, + ], + } + ) .catch((err) => { console.log(err); }); From 515142ff9f395381078bb16bb2ed08d09c16c043 Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Mon, 13 Nov 2023 19:33:30 -0500 Subject: [PATCH 3/8] Await result, fix type --- utils/actions/detectConsoleLogs.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index a76843d1..4a0e9935 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -142,7 +142,7 @@ export default async function detectConsoleLogs({ console.log(err); }); } - const latestCommitHash = getLatestCommitHash(); + const latestCommitHash = await getLatestCommitHash(); const commentPromises = diffFiles.map(async (file) => { const additions = getAdditions(file.patch ?? ""); @@ -180,7 +180,10 @@ export default async function detectConsoleLogs({ owner, repo, pull_number: issue_number, - commit_id: latestCommitHash, + commit_id: + typeof latestCommitHash === "string" + ? latestCommitHash + : undefined, event: "COMMENT", path: file.filename, comments: [ From e39fcbc3a4cc642e47838a274bb1eb3a80497d9d Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Mon, 13 Nov 2023 19:34:16 -0500 Subject: [PATCH 4/8] Extract text --- utils/actions/detectConsoleLogs.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 4a0e9935..fa2dba7c 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -17,7 +17,7 @@ const app = new App({ appId: process.env.GITHUB_APP_ID!, privateKey: process.env.GITHUB_PRIVATE_KEY!, }); - +const commentBody = `This PR contains console logs. Please review or remove them.`; const consoleLogDetectionPrompt = `This is a list of code additions. Identify if there's a console log or its equivalent in another programming language such as Java, Golang, Python, C, Rust, C++, Ruby, etc. @@ -190,7 +190,7 @@ export default async function detectConsoleLogs({ { path: file.filename, position: consoleLogPosition || 1, // comment at the beggining of the file by default - body: "This file contains at least one console log. Please remove any present.", + body: commentBody, }, ], } From 8f5f1886cc9badc35be631b0fb6b93636c2a2602 Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Tue, 14 Nov 2023 07:22:18 -0500 Subject: [PATCH 5/8] Remove unused imports --- utils/actions/detectConsoleLogs.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index fa2dba7c..3094a35e 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -1,12 +1,5 @@ const { Configuration, OpenAIApi } = require("openai"); import { App } from "@octokit/app"; -import { successPosthogTracking } from "../../utils/api/posthogTracking"; -import { - failedToFetchResponse, - missingParamsResponse, -} from "../../utils/api/responses"; -import validateParams from "../../utils/api/validateParams"; -import { Octokit } from "octokit"; const configuration = new Configuration({ apiKey: process.env.OPEN_AI_KEY, From b8a14b5b5b29e9a0e256f2708ab6ee0c5635d423 Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Tue, 14 Nov 2023 15:12:45 -0500 Subject: [PATCH 6/8] Rewrite diffing fucnction --- utils/actions/detectConsoleLogs.ts | 40 ++++++------------------------ 1 file changed, 7 insertions(+), 33 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 3094a35e..a0f9a276 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -26,54 +26,28 @@ If you return true, return a string that that has 2 values: result (true) and th The line value, is the actual line in the file that contains the console log. For example: true,console.log("hello world");`; -function getAdditions(filePatch: string) { +function getLineDiffs(filePatch: string) { const additions: string[] = []; + const removals: string[] = []; // Split the patch into lines const lines = filePatch.split("\n"); - // Track if we are in a deletion block - let inDeletionBlock = false; - // Loop through lines for (let i = 0; i < lines.length; i++) { const line = lines[i]; // Check if entering a deletion block if (line.startsWith("-")) { - inDeletionBlock = true; - continue; + removals.push(line.replace("-", "").trim()); } // Check if exiting a deletion block - if (line.startsWith("+") && inDeletionBlock) { - inDeletionBlock = false; - continue; - } - - // If not in a deletion block, add lines starting with + - if (!inDeletionBlock && line.startsWith("+")) { - additions.push(line); - } - - // Delete the pluses - lines[i] = line.replace("+", ""); - - // Trim the line to delete leading spaces - lines[i] = lines[i].trim(); - - // If the line is a comment, remove it - if ( - lines[i].startsWith("#") || - lines[i].startsWith("//") || - lines[i].startsWith("/*") - ) { - lines.splice(i, 1); - i--; + if (line.startsWith("+")) { + additions.push(line.replace("+", "").trim()); } } - - return lines.join("\n"); + return { additions: additions.join("\n"), removals: removals.join("\n") }; } function getConsoleLogPosition(filePatchAndIndividualLine: any) { @@ -138,7 +112,7 @@ export default async function detectConsoleLogs({ const latestCommitHash = await getLatestCommitHash(); const commentPromises = diffFiles.map(async (file) => { - const additions = getAdditions(file.patch ?? ""); + const { additions } = getLineDiffs(file.patch ?? ""); // detect if the additions contain console logs or not try { From 1b3f90f5e54463c1ddf4e8d4ae109abe6a7f4d2f Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Tue, 14 Nov 2023 18:01:04 -0500 Subject: [PATCH 7/8] Fix function --- utils/actions/detectConsoleLogs.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index a0f9a276..6f7b3847 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -50,20 +50,15 @@ function getLineDiffs(filePatch: string) { return { additions: additions.join("\n"), removals: removals.join("\n") }; } -function getConsoleLogPosition(filePatchAndIndividualLine: any) { - let positionInDiff = 1; - const { filePatch, individualLine } = filePatchAndIndividualLine; - - // get the position of the indiviudalLine in th filePatch +function getConsoleLogPosition({ filePatch, individualLine }) { + // Split the filePatch into lines and find the index of the line that includes individualLine const lines = filePatch.split("\n"); - for (let i = 1; i < lines.length; i++) { - if (lines[i].includes(individualLine)) { - positionInDiff = i; - break; - } - } + const zeroBasedIndex = lines.findIndex((line) => + line.includes(individualLine) + ); - return positionInDiff; + // Convert to one-based index, or return -1 if not found + return zeroBasedIndex === -1 ? -1 : zeroBasedIndex + 1; } export default async function detectConsoleLogs({ From f4700211fcdb187e2a869390c726427c4fcfd562 Mon Sep 17 00:00:00 2001 From: Esteban Dalel R Date: Tue, 14 Nov 2023 18:03:03 -0500 Subject: [PATCH 8/8] Use falsy values to return --- utils/actions/detectConsoleLogs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 6f7b3847..22dc94ee 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -58,7 +58,7 @@ function getConsoleLogPosition({ filePatch, individualLine }) { ); // Convert to one-based index, or return -1 if not found - return zeroBasedIndex === -1 ? -1 : zeroBasedIndex + 1; + return zeroBasedIndex === -1 ? 0 : zeroBasedIndex + 1; } export default async function detectConsoleLogs({