This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Fix/prevent double console log commenting #377
Merged
EstebanDalelR
merged 10 commits into
dev
from
fix/prevent-double-console-log-commenting
Nov 15, 2023
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2b99dce
Move const prompt
EstebanDalelR 15e13cd
Extract hash getter
EstebanDalelR 515142f
Await result, fix type
EstebanDalelR e39fcbc
Extract text
EstebanDalelR 8f5f188
Remove unused imports
EstebanDalelR b8a14b5
Rewrite diffing fucnction
EstebanDalelR 1b3f90f
Fix function
EstebanDalelR f470021
Use falsy values to return
EstebanDalelR f012b50
Merge branch 'dev' into fix/prevent-double-console-log-commenting
EstebanDalelR 9fdda40
Merge branch 'dev' into fix/prevent-double-console-log-commenting
EstebanDalelR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,21 @@ 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. | ||
(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[] = []; | ||
|
@@ -113,24 +128,25 @@ 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file contains at least one console log. Please remove any present. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file contains at least one console log. Please remove any present. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file contains at least one console log. Please remove any present. |
||
}); | ||
} | ||
const latestCommitHash = await getLatestCommitHash(); | ||
|
||
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 | ||
|
@@ -152,43 +168,33 @@ 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: | ||
typeof latestCommitHash === "string" | ||
? latestCommitHash | ||
: undefined, | ||
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: commentBody, | ||
}, | ||
], | ||
} | ||
) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file contains at least one console log. Please remove any present.