From 7a0e1a27a6240943e124b78a1ee5c7c2653c964e Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Wed, 18 Oct 2023 15:55:56 -0500 Subject: [PATCH 01/13] Add python and C++ hello world for testing purposes --- test_hello_worlds/hello.cpp | 8 ++++++++ test_hello_worlds/python.cpp | 1 + 2 files changed, 9 insertions(+) create mode 100644 test_hello_worlds/hello.cpp create mode 100644 test_hello_worlds/python.cpp diff --git a/test_hello_worlds/hello.cpp b/test_hello_worlds/hello.cpp new file mode 100644 index 00000000..e4cc4e17 --- /dev/null +++ b/test_hello_worlds/hello.cpp @@ -0,0 +1,8 @@ +// Your First C++ Program + +#include + +int main() { + std::cout << "Hello World!"; + return 0; +} \ No newline at end of file diff --git a/test_hello_worlds/python.cpp b/test_hello_worlds/python.cpp new file mode 100644 index 00000000..5f3948ab --- /dev/null +++ b/test_hello_worlds/python.cpp @@ -0,0 +1 @@ +print("This line will be printed.") \ No newline at end of file From c1a315275dbe7e1734febc2c5386aaaf8a9d37c0 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Wed, 18 Oct 2023 15:57:01 -0500 Subject: [PATCH 02/13] Add Rust hello world for testing purposes --- test_hello_worlds/hello.rs | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 test_hello_worlds/hello.rs diff --git a/test_hello_worlds/hello.rs b/test_hello_worlds/hello.rs new file mode 100644 index 00000000..f0847900 --- /dev/null +++ b/test_hello_worlds/hello.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello World!"); +} \ No newline at end of file From bae6af8892d4af5a047d83f04c0cab7ae1318383 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Fri, 20 Oct 2023 11:06:25 -0500 Subject: [PATCH 03/13] Add a new hello world --- test_hello_worlds/hello.ts | 1 + utils/actions/detectConsoleLogs.ts | 1 + 2 files changed, 2 insertions(+) create mode 100644 test_hello_worlds/hello.ts diff --git a/test_hello_worlds/hello.ts b/test_hello_worlds/hello.ts new file mode 100644 index 00000000..6b2b3db0 --- /dev/null +++ b/test_hello_worlds/hello.ts @@ -0,0 +1 @@ +console.log("hello world"); diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 72cc3f27..1d1d208d 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -125,6 +125,7 @@ export default async function detectConsoleLogs({ const addtionsHaveConsoleLog = result.data.choices[0].message.content; if (addtionsHaveConsoleLog === "true") { + console.log("CONSOLE LOG DETECTED ") // comment detailing which file has the console log detected return octokit.request( "POST /repos/{owner}/{repo}/issues/{issue_number}/comments", From 96b341bee9691618dc2eb03612335d7679ea51d6 Mon Sep 17 00:00:00 2001 From: Esteban Vargas Date: Fri, 20 Oct 2023 17:07:05 -0500 Subject: [PATCH 04/13] Update hello.cpp --- test_hello_worlds/hello.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test_hello_worlds/hello.cpp b/test_hello_worlds/hello.cpp index e4cc4e17..7867d625 100644 --- a/test_hello_worlds/hello.cpp +++ b/test_hello_worlds/hello.cpp @@ -1,8 +1,6 @@ -// Your First C++ Program - #include int main() { std::cout << "Hello World!"; return 0; -} \ No newline at end of file +} From 4104c959e4c162ab8400ca9dba01be3721ca245e Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Fri, 20 Oct 2023 18:34:10 -0500 Subject: [PATCH 05/13] Detect console logs per file and comment per file diff with the PAT --- utils/actions/detectConsoleLogs.ts | 52 ++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 1d1d208d..5040ed9a 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -6,6 +6,7 @@ import { 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, @@ -125,17 +126,48 @@ export default async function detectConsoleLogs({ const addtionsHaveConsoleLog = result.data.choices[0].message.content; if (addtionsHaveConsoleLog === "true") { - console.log("CONSOLE LOG DETECTED ") // comment detailing which file has the console log detected - return octokit.request( - "POST /repos/{owner}/{repo}/issues/{issue_number}/comments", - { - owner, - repo, - issue_number, - body: `This PR contains console logs in file ${file.filename}. Please remove them.`, - } - ); + + // return octokit.request( + // "POST /repos/{owner}/{repo}/issues/{issue_number}/comments/", + // { + // // owner, + // // repo, + // // issue_number, + // // body: `This PR contains console logs in file ${file.filename}. Please remove them.`, + // owner, + // repo, + // issue_number, + // body: `Remove console log - todays sha file.filenmae , ${file.filename}`, + // commit_id: "c1a315275dbe7e1734febc2c5386aaaf8a9d37c0", + // path: file.filename + // } + // ); + + const normaloctokit = new Octokit({ + auth: // Insert your GitHub Personal Access Token here for testing purposes + }); + + const commenetFileDiff = () => { + return normaloctokit.rest.pulls + .get({ + owner, + repo, + pull_number: issue_number, + }) + .then((response) => { + return normaloctokit.rest.pulls.createReviewComment({ + owner, + repo, + pull_number: issue_number, + line: 1, + body: "This file contains at least one console log. Please remove any present.", + commit_id: response.data.head.sha, + path: file.filename.toString(), + }); + }); + }; + } }); } catch {} From 9712d3a751c877781dd29eeebc7a39479e6dbb08 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Fri, 20 Oct 2023 18:45:08 -0500 Subject: [PATCH 06/13] Fix typo on commentFileDiff function --- 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 5040ed9a..71c5f7ca 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -148,7 +148,7 @@ export default async function detectConsoleLogs({ auth: // Insert your GitHub Personal Access Token here for testing purposes }); - const commenetFileDiff = () => { + const commentFileDiff = () => { return normaloctokit.rest.pulls .get({ owner, From 86c6f71aa776b889065c5dc57d6f2b224f1584d0 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Mon, 23 Oct 2023 12:29:20 -0500 Subject: [PATCH 07/13] Comment using the Octokit route that allows us to use the app token --- utils/actions/detectConsoleLogs.ts | 58 +++++++++++++----------------- 1 file changed, 24 insertions(+), 34 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 71c5f7ca..9275f4bc 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -126,48 +126,38 @@ export default async function detectConsoleLogs({ const addtionsHaveConsoleLog = result.data.choices[0].message.content; if (addtionsHaveConsoleLog === "true") { - // comment detailing which file has the console log detected - - // return octokit.request( - // "POST /repos/{owner}/{repo}/issues/{issue_number}/comments/", - // { - // // owner, - // // repo, - // // issue_number, - // // body: `This PR contains console logs in file ${file.filename}. Please remove them.`, - // owner, - // repo, - // issue_number, - // body: `Remove console log - todays sha file.filenmae , ${file.filename}`, - // commit_id: "c1a315275dbe7e1734febc2c5386aaaf8a9d37c0", - // path: file.filename - // } - // ); - - const normaloctokit = new Octokit({ - auth: // Insert your GitHub Personal Access Token here for testing purposes - }); - const commentFileDiff = () => { - return normaloctokit.rest.pulls - .get({ + return octokit + .request("GET /repos/{owner}/{repo}/pulls/{pull_number}", { owner, repo, pull_number: issue_number, }) - .then((response) => { - return normaloctokit.rest.pulls.createReviewComment({ - owner, - repo, - pull_number: issue_number, - line: 1, - body: "This file contains at least one console log. Please remove any present.", - commit_id: response.data.head.sha, - path: file.filename.toString(), - }); + .then((result) => { + const latestCommitHash = result.data.head.sha; + + return octokit.request( + "POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", + { + owner, + repo, + pull_number: issue_number, + commit_id: result.data.head.sha, + event: "REQUEST_CHANGES", + path: file.filename, + comments: [ + { + path: file.filename, + position: 1, + body: "head.sha - This file contains at least one console log. Please remove any present.", + }, + ], + } + ); }); }; + commentFileDiff(); } }); } catch {} From 7473a1a9b9e798957a6671f2c01252854279f874 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Tue, 24 Oct 2023 11:24:07 -0500 Subject: [PATCH 08/13] Start building getconsolelog position in diff ufnction --- utils/actions/detectConsoleLogs.ts | 39 ++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 9275f4bc..8765d441 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -68,6 +68,22 @@ function getAdditions(filePatch: string) { return lines.join("\n"); } +function getConsoleLogPosition(filePatchAndIndividualLine: any) { + let positionInDiff = 1; + const { filePatch, individualLine } = filePatchAndIndividualLine; + + // get the position of the indiviudalLine in th filePatch + const lines = filePatch.split("\n"); + for (let i = 0; i < lines.length; i++) { + if (lines[i].includes(individualLine)) { + positionInDiff = i + 1; + break; + } + } + + return positionInDiff; +} + export default async function detectConsoleLogs({ installationId, owner, @@ -108,7 +124,10 @@ export default async function detectConsoleLogs({ Python comments start with #. Other console functions such as console.info() shouldn't be counted as console logs. Ignore code comments from this analysis. - If there is a console log, print "true", else print "false"`; + If there is a console log, return "true", else return "false". + If you return true, return an object that that has 2 props: result and line. + The line value, is the actual line in the file that contains the console log. + For example: { result: true, line: console.log("hello world");}`; // detect if the additions contain console logs or not try { @@ -123,7 +142,12 @@ export default async function detectConsoleLogs({ ], }) .then((result) => { - const addtionsHaveConsoleLog = result.data.choices[0].message.content; + const openAIResult = result.data.choices[0].message.content; + const addtionsHaveConsoleLog = openAIResult.result; + const indiviudalLine = openAIResult.line; + + console.log("additionsHaveConsoleLog", addtionsHaveConsoleLog); + console.log("indiviudalLine", indiviudalLine); if (addtionsHaveConsoleLog === "true") { const commentFileDiff = () => { @@ -135,7 +159,9 @@ export default async function detectConsoleLogs({ }) .then((result) => { const latestCommitHash = result.data.head.sha; - + + let individualLine = ""; + return octokit.request( "POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", { @@ -148,8 +174,11 @@ export default async function detectConsoleLogs({ comments: [ { path: file.filename, - position: 1, - body: "head.sha - This file contains at least one console log. Please remove any present.", + position: getConsoleLogPosition({ + filePatch: file.patch ?? "", + individualLine + }) || 1, // comment at the beggining of the file by default + body: "getConsoleLogPosition - This file contains at least one console log. Please remove any present.", }, ], } From b50c65db88f639ce0c0568ed339fbcd93a6ffda9 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Tue, 24 Oct 2023 16:04:15 -0500 Subject: [PATCH 09/13] Comment position in diff but send PR with console logs intentionally for testing purposes --- utils/actions/detectConsoleLogs.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 8765d441..3a094990 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -81,6 +81,7 @@ function getConsoleLogPosition(filePatchAndIndividualLine: any) { } } + console.log("Console log position latest v: ", positionInDiff); return positionInDiff; } @@ -125,9 +126,9 @@ export default async function detectConsoleLogs({ Other console functions such as console.info() shouldn't be counted as console logs. Ignore code comments from this analysis. If there is a console log, return "true", else return "false". - If you return true, return an object that that has 2 props: result and line. + 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: { result: true, line: console.log("hello world");}`; + For example: true,console.log("hello world");`; // detect if the additions contain console logs or not try { @@ -142,12 +143,14 @@ export default async function detectConsoleLogs({ ], }) .then((result) => { - const openAIResult = result.data.choices[0].message.content; - const addtionsHaveConsoleLog = openAIResult.result; - const indiviudalLine = openAIResult.line; + const openAIResult = result.data.choices[0].message.content.split(","); + console.log("openAIResult parsed", openAIResult); + + const addtionsHaveConsoleLog = openAIResult[0]; + const individualLine = openAIResult[1]; console.log("additionsHaveConsoleLog", addtionsHaveConsoleLog); - console.log("indiviudalLine", indiviudalLine); + console.log("indiviudalLine", individualLine); if (addtionsHaveConsoleLog === "true") { const commentFileDiff = () => { @@ -160,7 +163,7 @@ export default async function detectConsoleLogs({ .then((result) => { const latestCommitHash = result.data.head.sha; - let individualLine = ""; + // let individualLine = ""; return octokit.request( "POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", @@ -168,7 +171,7 @@ export default async function detectConsoleLogs({ owner, repo, pull_number: issue_number, - commit_id: result.data.head.sha, + commit_id: latestCommitHash, event: "REQUEST_CHANGES", path: file.filename, comments: [ From b229e091bc36ff3927974d04aa3f2804509e08e6 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Tue, 24 Oct 2023 16:11:32 -0500 Subject: [PATCH 10/13] Remove console logs --- utils/actions/detectConsoleLogs.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/utils/actions/detectConsoleLogs.ts b/utils/actions/detectConsoleLogs.ts index 3a094990..c05989c9 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -81,7 +81,6 @@ function getConsoleLogPosition(filePatchAndIndividualLine: any) { } } - console.log("Console log position latest v: ", positionInDiff); return positionInDiff; } @@ -144,14 +143,10 @@ export default async function detectConsoleLogs({ }) .then((result) => { const openAIResult = result.data.choices[0].message.content.split(","); - console.log("openAIResult parsed", openAIResult); const addtionsHaveConsoleLog = openAIResult[0]; const individualLine = openAIResult[1]; - console.log("additionsHaveConsoleLog", addtionsHaveConsoleLog); - console.log("indiviudalLine", individualLine); - if (addtionsHaveConsoleLog === "true") { const commentFileDiff = () => { return octokit @@ -163,8 +158,6 @@ export default async function detectConsoleLogs({ .then((result) => { const latestCommitHash = result.data.head.sha; - // let individualLine = ""; - return octokit.request( "POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", { From 753f96b3f8703f63dac7781d7c5281afdf4d84bf Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Tue, 24 Oct 2023 16:12:02 -0500 Subject: [PATCH 11/13] Remove test hello worlds --- test_hello_worlds/hello.cpp | 6 ------ test_hello_worlds/hello.rs | 3 --- test_hello_worlds/hello.ts | 1 - test_hello_worlds/python.cpp | 1 - 4 files changed, 11 deletions(-) delete mode 100644 test_hello_worlds/hello.cpp delete mode 100644 test_hello_worlds/hello.rs delete mode 100644 test_hello_worlds/hello.ts delete mode 100644 test_hello_worlds/python.cpp diff --git a/test_hello_worlds/hello.cpp b/test_hello_worlds/hello.cpp deleted file mode 100644 index 7867d625..00000000 --- a/test_hello_worlds/hello.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() { - std::cout << "Hello World!"; - return 0; -} diff --git a/test_hello_worlds/hello.rs b/test_hello_worlds/hello.rs deleted file mode 100644 index f0847900..00000000 --- a/test_hello_worlds/hello.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello World!"); -} \ No newline at end of file diff --git a/test_hello_worlds/hello.ts b/test_hello_worlds/hello.ts deleted file mode 100644 index 6b2b3db0..00000000 --- a/test_hello_worlds/hello.ts +++ /dev/null @@ -1 +0,0 @@ -console.log("hello world"); diff --git a/test_hello_worlds/python.cpp b/test_hello_worlds/python.cpp deleted file mode 100644 index 5f3948ab..00000000 --- a/test_hello_worlds/python.cpp +++ /dev/null @@ -1 +0,0 @@ -print("This line will be printed.") \ No newline at end of file From 8a164cb4e8d3ed8aee80fee646e5f89ffac52cd1 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Tue, 24 Oct 2023 16:13:08 -0500 Subject: [PATCH 12/13] Remove testing text --- 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 c05989c9..0b0db651 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -174,7 +174,7 @@ export default async function detectConsoleLogs({ filePatch: file.patch ?? "", individualLine }) || 1, // comment at the beggining of the file by default - body: "getConsoleLogPosition - This file contains at least one console log. Please remove any present.", + body: "This file contains at least one console log. Please remove any present.", }, ], } From 268cf9ebd36a43aecbe984a00971500a32239cd8 Mon Sep 17 00:00:00 2001 From: baristaGeek Date: Thu, 26 Oct 2023 17:25:36 -0500 Subject: [PATCH 13/13] Change event from REQUEST_CHANGES to COMMENT --- 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 0b0db651..7ab41dbe 100644 --- a/utils/actions/detectConsoleLogs.ts +++ b/utils/actions/detectConsoleLogs.ts @@ -165,7 +165,7 @@ export default async function detectConsoleLogs({ repo, pull_number: issue_number, commit_id: latestCommitHash, - event: "REQUEST_CHANGES", + event: "COMMENT", path: file.filename, comments: [ {