Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

v2.1.7 #428

Merged
merged 185 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
185 commits
Select commit Hold shift + click to select a range
fc8b365
fix: upgrade airtable from 0.11.6 to 0.12.1
snyk-bot Sep 2, 2023
b9029f6
Fix/v2.0.0 merge conflicts (#278)
EstebanDalelR Sep 4, 2023
c0a6103
Merge branch 'dev' into snyk-upgrade-2fb799419e235800c37aacf6ded49ad0
EstebanDalelR Sep 4, 2023
9a29a68
Build a prompt that produces the desired results
baristaGeek Sep 5, 2023
86c81f7
Flag PRs
baristaGeek Sep 5, 2023
4d0b5bc
Change comparison string for detecting already labeled PRs
baristaGeek Sep 5, 2023
1d246b0
Fix/double cloud id getting (#271)
EstebanDalelR Sep 5, 2023
835e833
Change hardcoded pr title and number to the ones stored on GitHubPRs[0
baristaGeek Sep 5, 2023
aa3ce80
Remove console logs
baristaGeek Sep 5, 2023
6bbba41
Chore/delete unused files (#283)
EstebanDalelR Sep 6, 2023
b174fd6
Feature/new integrations script (#272)
EstebanDalelR Sep 6, 2023
30dabb9
Feature/asana integration (#273)
EstebanDalelR Sep 7, 2023
25e1350
Move logic to actions/github
baristaGeek Sep 7, 2023
7aa4cad
Fix parameter names
baristaGeek Sep 7, 2023
415b917
Merge pull request #275 from watermelontools/snyk-upgrade-2fb799419e2…
baristaGeek Sep 7, 2023
7095af1
Merge branch 'dev' into feature/Watermelon-as-reviewer-0
baristaGeek Sep 8, 2023
63bcbbb
Feature/improve logging (#291)
EstebanDalelR Sep 11, 2023
8ccb1c7
Feature/streamline new integrations (#292)
EstebanDalelR Sep 11, 2023
5c5c103
Remove running in the extension
baristaGeek Sep 11, 2023
ee21e0d
Run sync
baristaGeek Sep 12, 2023
033e3eb
Change threshold back to 9
baristaGeek Sep 12, 2023
d05c0f3
Update route.ts (#296)
EstebanDalelR Sep 13, 2023
9aaa301
Update route.ts (#295)
EstebanDalelR Sep 13, 2023
62ab841
Update responses.ts (#294)
EstebanDalelR Sep 13, 2023
1b2c123
Update redirect.tsx (#293)
EstebanDalelR Sep 13, 2023
82a2093
Feature/intellij login (#286)
EstebanDalelR Sep 13, 2023
fcb992d
Refactor Watermelon as reviewer 0 to work sync and prevent race condi…
baristaGeek Sep 13, 2023
0e13788
Remove user prompt
baristaGeek Sep 13, 2023
058f1a2
Couple logic inside a single function
baristaGeek Sep 13, 2023
047d4d1
Merge branch 'dev' into feature/Watermelon-as-reviewer-0
baristaGeek Sep 13, 2023
aa99e79
Feature/intellij data mapping (#298)
EstebanDalelR Sep 14, 2023
f433f20
Make function async again, use installation octokit, and add auto lab…
baristaGeek Sep 14, 2023
2211ba9
Track pr rating per repo on PostHog
baristaGeek Sep 14, 2023
229e3d0
Add param validation
baristaGeek Sep 14, 2023
9693c5d
Merge branch 'dev' into feature/Watermelon-as-reviewer-0
EstebanDalelR Sep 14, 2023
9d37d0f
Merge pull request #282 from watermelontools/feature/Watermelon-as-re…
baristaGeek Sep 14, 2023
ef4d61b
Feature/encapsulated tracking (#297)
EstebanDalelR Sep 14, 2023
4569bd6
Chore/2.0.1 merge conflicts (#299)
EstebanDalelR Sep 14, 2023
f0458e2
Merge branch 'main' into dev
EstebanDalelR Sep 14, 2023
c4e9644
v2.0.1 (#281) (#300)
EstebanDalelR Sep 14, 2023
eccc506
Add date to textToWrite for the GitHub service
baristaGeek Sep 19, 2023
7647a25
Calculate relative date
baristaGeek Sep 19, 2023
7fe4841
Print PR author
baristaGeek Sep 19, 2023
1ef114e
Remove conole logs
baristaGeek Sep 19, 2023
0e09202
Remove organization as a mandtory paramater for running the GH app (#…
baristaGeek Sep 20, 2023
d6f99d6
REmove github special case
EstebanDalelR Sep 20, 2023
51dfe6f
Fix helper text
EstebanDalelR Sep 20, 2023
eb45693
REmove github special case (#303)
EstebanDalelR Sep 20, 2023
0f4e18f
Merge branch 'dev' into fix/broken-deactivation-text
EstebanDalelR Sep 20, 2023
7fbb7af
Merge pull request #304 from watermelontools/fix/broken-deactivation-…
baristaGeek Sep 20, 2023
1ac634b
Merge branch 'dev' into feature/add-ownership-and-date
baristaGeek Sep 20, 2023
3f91367
Merge pull request #301 from watermelontools/feature/add-ownership-an…
baristaGeek Sep 20, 2023
efc1be6
Add conditional if the count passes 500
baristaGeek Sep 22, 2023
e0d2894
Feature/unauthorized tracking (#309)
EstebanDalelR Sep 22, 2023
8b376fe
Feature/random suggestion text on markdown (#310)
EstebanDalelR Sep 22, 2023
5404ad4
Replace all indexed information with invitation to pay
baristaGeek Sep 25, 2023
7e1ad3d
Remove the logic from the markdown helper
baristaGeek Sep 25, 2023
04f185a
Feature/intellijdownload (#312)
EstebanDalelR Sep 26, 2023
9fdaa47
Remove console log
baristaGeek Sep 26, 2023
e20efd1
Unwrapp comenting logic from being below the threshold. Replace comme…
baristaGeek Sep 26, 2023
44bf4ef
Create and call the new stored procedure (increment_owner_github_app_…
baristaGeek Sep 26, 2023
ef7dc57
Merge pull request #311 from watermelontools/bugfix/bugfix-not-showin…
baristaGeek Sep 27, 2023
84c0dd8
Merge branch 'dev' into bugfix/user-or-org-to-gh-app
baristaGeek Sep 27, 2023
a6b47ba
Merge pull request #313 from watermelontools/bugfix/user-or-org-to-gh…
baristaGeek Sep 27, 2023
3474cb0
Allow empty responses (#317)
EstebanDalelR Sep 28, 2023
c0932ed
Make prompt multi-line to make its iteration easier
baristaGeek Oct 10, 2023
cd57b53
Bias prompt towards conciseness
baristaGeek Oct 10, 2023
70b92ef
Remove role user prompt for simplicity
baristaGeek Oct 10, 2023
c78988e
Merge into a single prompt, and bias the prompt even more towards con…
baristaGeek Oct 10, 2023
3b5a068
Remove github param validation because of the recent permission changes
baristaGeek Oct 10, 2023
52784fe
Remove prod console logs
baristaGeek Oct 10, 2023
0a18c09
Add logic that removes outdated PR labels
baristaGeek Oct 10, 2023
3aff27c
Change the take a deeper dive label emoji
baristaGeek Oct 10, 2023
cca1d2c
Merge pull request #322 from watermelontools/feature/trinary-pr-labeling
baristaGeek Oct 11, 2023
dab95a9
console.log() file patches
baristaGeek Oct 11, 2023
e336e75
Detect console logs and its equivalent in other programming languages…
baristaGeek Oct 11, 2023
fd187fe
Comment the PR if there's a console log detected
baristaGeek Oct 13, 2023
ec591fa
Remove console logs
baristaGeek Oct 13, 2023
161bc91
remove console log
baristaGeek Oct 17, 2023
9d68579
update prompt
baristaGeek Oct 17, 2023
0a3a481
update prompt
baristaGeek Oct 17, 2023
63b0933
Extract functions and texts (#323) (#325)
EstebanDalelR Oct 17, 2023
b937bf3
Ommit console logs that are in code comments
baristaGeek Oct 17, 2023
81380cd
remove old logic
baristaGeek Oct 17, 2023
c01fb42
Restric rating to a maximum of 8 if console log comments are found
baristaGeek Oct 17, 2023
c86a96a
Merge the commits into the prompt list
baristaGeek Oct 18, 2023
48970fb
Update utils/actions/detectConsoleLogs.ts
baristaGeek Oct 18, 2023
d69514b
Promise.allSettled octokit call
baristaGeek Oct 18, 2023
ab41609
Merge branch 'dev' into feature/search-console-logs
baristaGeek Oct 18, 2023
3eb21e2
Concat with different syntax
baristaGeek Oct 18, 2023
955413a
fix merge conflict
baristaGeek Oct 18, 2023
d642ac4
Merge pull request #324 from watermelontools/feature/search-console-logs
baristaGeek Oct 18, 2023
afb3999
Chore/extract PR grading (#333)
EstebanDalelR Oct 23, 2023
df0bb3e
bring the user prompt back
baristaGeek Oct 23, 2023
28a53a4
Add consistent CSS for boxes regardless of login state + prettify design
baristaGeek Oct 25, 2023
b361795
Style the boxes for all integrations
baristaGeek Oct 25, 2023
cfe74c9
Merge pull request #321 from watermelontools/enhancement/improve-prom…
baristaGeek Oct 25, 2023
2fd51f0
Make IDE boxes consistenet with the integraitons boxes
baristaGeek Oct 25, 2023
b86be53
Rebuild sidebar
baristaGeek Oct 25, 2023
8e33d77
Redesign the header
baristaGeek Oct 25, 2023
cf21362
Make final adjustments to header
baristaGeek Oct 25, 2023
894cc1a
Make instruction more specific and add the C++ exmaple
baristaGeek Oct 25, 2023
c62f679
Merge pull request #340 from watermelontools/chore/detect-cpp-console…
baristaGeek Oct 26, 2023
36bfcfd
Create getTeamGitHub.ts (#338)
EstebanDalelR Oct 26, 2023
eae03a0
Create createTeamAndMatchUser.ts (#337)
EstebanDalelR Oct 26, 2023
eaa2ec6
Conditionally render placeholder (#342)
EstebanDalelR Oct 26, 2023
7925771
Feature/comment specific line diffs (#331)
baristaGeek Oct 26, 2023
72b539f
Watermelon using app token (team tool) (#326)
EstebanDalelR Oct 26, 2023
fd21a53
Re-style team page
baristaGeek Oct 27, 2023
ec94f5d
Style settings screen
baristaGeek Oct 27, 2023
ace2080
Merge branch 'dev' into enhancement/beautify-dashboard
baristaGeek Oct 27, 2023
4ad49a4
Pull changes
baristaGeek Oct 27, 2023
9c07826
Merge pull request #339 from watermelontools/enhancement/beautify-das…
baristaGeek Oct 27, 2023
78eac4c
Merge branch 'main' into dev
baristaGeek Oct 27, 2023
8d8c315
Fix indexing issue for the single line file border-case (#346)
baristaGeek Oct 30, 2023
4739d5b
Delete app/(loggedIn)/settings/layout.tsx (#348)
EstebanDalelR Oct 30, 2023
adcc4a9
Add tracking to keys existing
EstebanDalelR Oct 31, 2023
5015186
Fix/broken login getting (#353)
EstebanDalelR Oct 31, 2023
3d25273
Chore/readme update messaging and strategy (#352)
baristaGeek Oct 31, 2023
754aeea
Merge branch 'main' into dev
EstebanDalelR Oct 31, 2023
07d6a42
Fix/add catches to octokit (#359)
EstebanDalelR Nov 2, 2023
d4dbf41
Fix/missing params when a comment is created or edited (#358)
EstebanDalelR Nov 2, 2023
d55a869
Delete public/favicon_io.zip (#357)
EstebanDalelR Nov 2, 2023
fd615b1
Merge branch 'main' into dev
EstebanDalelR Nov 2, 2023
822ed7a
Add random files
baristaGeek Nov 3, 2023
c5e9d46
Add a space to WatermelonAI summary title
baristaGeek Nov 3, 2023
af00550
Search for new app name to avoid duplicate comments
baristaGeek Nov 3, 2023
a340702
Remove test files (I messed up)
baristaGeek Nov 3, 2023
9938292
Delete public/questionsHappyTulia.png (#361)
EstebanDalelR Nov 3, 2023
0fc9057
Merge pull request #362 from watermelontools/bugfix/duplicate-pr-comm…
baristaGeek Nov 7, 2023
6b74050
Update model to GPT4-turbo (#365)
EstebanDalelR Nov 9, 2023
5450bb6
Pin Next version to 13
EstebanDalelR Nov 10, 2023
5b8ab6f
Add the ruby case, and add the [input=email] string as a counter example
baristaGeek Nov 10, 2023
25c4f93
Make calls to other services concurrent (#375)
EstebanDalelR Nov 14, 2023
769ce19
Merge branch 'main' into dev
EstebanDalelR Nov 15, 2023
5c2427e
Fix/prevent double console log commenting (#377)
EstebanDalelR Nov 15, 2023
4817f85
Feature/uninstall email (#366)
EstebanDalelR Nov 15, 2023
9098ee9
Change all watermelon-context references to watermelon-copilot-for-co…
baristaGeek Nov 20, 2023
6255bca
Create dependabot.yml
baristaGeek Nov 20, 2023
0c5cd93
Merge pull request #387 from watermelontools/baristaGeek-patch-5
baristaGeek Nov 20, 2023
6795e85
Return 400 if PR author user type is a bot
baristaGeek Nov 20, 2023
ec51510
REmove commons clause
baristaGeek Nov 20, 2023
fff3cfe
Comment GitLab and Bitbucket login links
baristaGeek Nov 20, 2023
99fa05e
Remove IDEs from the login grid
baristaGeek Nov 20, 2023
635513b
Remove VS Code link from header
baristaGeek Nov 20, 2023
d68e934
Remove references to IDE extensions on the random text
baristaGeek Nov 20, 2023
af12329
Fix undefined team url (#384)
EstebanDalelR Nov 21, 2023
817161c
Merge pull request #386 from watermelontools/bugfix/multiple-comments
baristaGeek Nov 21, 2023
f5ce673
Merge pull request #389 from watermelontools/docs/remove-commons-clause
baristaGeek Nov 21, 2023
34d9ba5
Remove comments
baristaGeek Nov 21, 2023
1e29ed0
Merge pull request #390 from watermelontools/chore/remove-unused-pieces
baristaGeek Nov 21, 2023
073cea3
Merge pull request #388 from watermelontools/bugfix-373/deactivate-co…
baristaGeek Nov 21, 2023
156aaf3
Fix stored procedure call
baristaGeek Nov 27, 2023
d62f2ec
Add idiomatic name to the stored procedure that updates github usage …
baristaGeek Nov 27, 2023
a862399
Remove prod console log
baristaGeek Nov 27, 2023
3950d08
Add bugfix and its derivatives as stopwords
baristaGeek Nov 28, 2023
d37e074
Update README.md
baristaGeek Nov 28, 2023
1e5018a
Merge pull request #395 from watermelontools/bugfix/new-counter
baristaGeek Nov 28, 2023
0d5e953
Merge pull request #396 from watermelontools/chore/add-bugfix-as-a-se…
baristaGeek Nov 29, 2023
b44ba29
Upgrade GPT in detectConsoleLogs.ts (#398)
EstebanDalelR Nov 30, 2023
cfae6ed
Update pull_request_template.md (#400)
EstebanDalelR Dec 5, 2023
71a236c
Update README.md
baristaGeek Dec 8, 2023
b80d9e1
Merge pull request #408 from watermelontools/baristaGeek-patch-6
baristaGeek Dec 11, 2023
823f158
Replace calendly with Stripe link (#410)
baristaGeek Dec 12, 2023
9ded9eb
Merge branch 'main' into dev
EstebanDalelR Dec 12, 2023
a86f8bb
Build the prototype for a RegEx
baristaGeek Dec 12, 2023
3af5a28
modify regex
baristaGeek Dec 13, 2023
d46c305
Feature/granular settings (#402)
EstebanDalelR Dec 13, 2023
0bccee7
Add a regex that works decently
baristaGeek Dec 13, 2023
8b8c483
Comment the correct line
baristaGeek Dec 13, 2023
0f417b4
Ignorelines with comments
baristaGeek Dec 13, 2023
ebe0afe
Remove console log detection prompt
baristaGeek Dec 13, 2023
7bcdd4f
Remove unused function
baristaGeek Dec 13, 2023
6aebf2f
Remove leftout comments
baristaGeek Dec 13, 2023
5ec7628
Remove overinformative console log
baristaGeek Dec 13, 2023
336e40b
Merge pull request #413 from watermelontools/feature/better-console-l…
baristaGeek Dec 14, 2023
99c0da8
Fix max (#420)
EstebanDalelR Dec 26, 2023
5cc8a42
Pass summary as always true (#425)
EstebanDalelR Jan 17, 2024
6feaec7
Feature/push user to create account (#426)
EstebanDalelR Jan 18, 2024
e24b403
Sign up page: Image is broken #418 (#427)
gvijay4321 Jan 18, 2024
f6b9b06
Merge branch 'main' into dev
EstebanDalelR Jan 18, 2024
cc24d37
Fix marketplace link
Jan 22, 2024
446ca04
Merge pull request #430 from gvijay4321/FixReadMeLinks
baristaGeek Jan 22, 2024
0f4973a
Test allSettled to allow commenting (#432)
EstebanDalelR Jan 24, 2024
3e3f6bb
Feature/417 leftout comments (#424)
baristaGeek Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Finally, our GitHub app uses "LLMs as Reviewer Zero". Watermelon evaluates the t

## Installation

You can install Watermelon's GitHub Application [directly from the marketplace](https://github.com/marketplace/watermelon-copilot-for-code-review).
You can install Watermelon's GitHub Application [directly from the marketplace](https://github.com/marketplace/watermelon-context).

## Contributing

Expand Down
35 changes: 32 additions & 3 deletions app/api/actions/github/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
import validateParams from "../../../../utils/api/validateParams";

import labelPullRequest from "../../../../utils/actions/labelPullRequest";
import detectConsoleLogs from "../../../../utils/actions/detectConsoleLogs";
import detectLefoutComments from "../../../../utils/codeSmells/detectLefoutComments";
import detectConsoleLogs from "../../../../utils/codeSmells/detectConsoleLogs";

import {
failedPosthogTracking,
Expand Down Expand Up @@ -375,6 +376,9 @@ export async function POST(request: Request) {
textToWrite +=
"Error getting summary" + businessLogicSummary?.error + "\n";
}
if (AISummary === 2) {
textToWrite += `Please create an account on [Watermelon](https://app.watermelontools.com/) to get better results. \n`;
}
} else {
textToWrite += `AI Summary deactivated by ${userLogin} \n`;
}
Expand Down Expand Up @@ -434,7 +438,7 @@ export async function POST(request: Request) {
repoName: repo,
});
textToWrite += randomText();
Promise.all([
Promise.allSettled([
// Detect console.logs and its equivalent in other languages
CodeComments
? detectConsoleLogs({
Expand All @@ -448,6 +452,19 @@ export async function POST(request: Request) {
reqEmail: req.email,
})
: null,
// Detect console.logs and its equivalent in other languages
CodeComments
? detectLefoutComments({
prTitle: title,
businessLogicSummary,
repo,
owner,
issue_number: number,
installationId,
reqUrl: request.url,
reqEmail: req.email,
})
: null,
// Make Watermelon Review the PR's business logic here by comparing the title with the AI-generated summary
Badges
? labelPullRequest({
Expand Down Expand Up @@ -551,7 +568,7 @@ export async function POST(request: Request) {
});
}

// If the count is surpassed, we replace the
// If the count is surpassed, we replace the text
if (count.github_app_uses > 500) {
textToWrite = `Your team has surpassed the free monthly usage. [Please click here](https://buy.stripe.com/28o0289KVaYV5wY004) to upgrade.`;

Expand Down Expand Up @@ -641,6 +658,18 @@ export async function POST(request: Request) {
reqEmail: req.email,
});

// Detect multi-line leftout comments
await detectLefoutComments({
prTitle: title,
businessLogicSummary,
repo,
owner,
issue_number: number,
installationId,
reqUrl: request.url,
reqEmail: req.email,
});

// Make Watermelon Review the PR's business logic here by comparing the title with the AI-generated summary
await labelPullRequest({
prTitle: title,
Expand Down
2 changes: 1 addition & 1 deletion app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const authOptions: NextAuthOptions = {
theme: {
colorScheme: "dark", // "auto" | "dark" | "light"
brandColor: "#238636", // Hex color code
logo: "public/logo.png", // Absolute URL to image
logo: "/favicon/favicon-32x32.png", // Absolute URL to image
},
// Configure one or more authentication providers
providers: [
Expand Down
2 changes: 1 addition & 1 deletion utils/actions/getAllServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default async function getAllServices({
notion: { error: "no notion token", data: [], fullData: [] },
slack: { error: "no slack token", data: [], fullData: [] },
watermelon_user: "team",
AISummary,
AISummary: 2,
ResponseTexts,
SearchAmount,
};
Expand Down
3 changes: 3 additions & 0 deletions utils/actions/markdownHelpers/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const countMarkdown = ({
if (count.error) {
return `\n We're sorry, we ran into an error: ${count.error}`;
} else {
if (count.name && count.github_app_uses > 500) {
return `Your team has surpassed the free monthly usage. [Please click here](https://buy.stripe.com/28o0289KVaYV5wY004) to upgrade.`;
}
if (count.name && count.github_app_uses) {
return `\n Your team ${count.name} has used Watermelon ${count.github_app_uses} times.`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Configuration, OpenAIApi } = require("openai");
import { App } from "@octokit/app";
import { getLineDiffs } from "./getLineDiffs";

const configuration = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
Expand All @@ -10,31 +11,8 @@ 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.`;

function getLineDiffs(filePatch: string) {
const additions: string[] = [];
const removals: string[] = [];

// Split the patch into lines
const lines = filePatch.split("\n");

// Loop through lines
for (let i = 0; i < lines.length; i++) {
const line = lines[i];

// Check if entering a deletion block
if (line.startsWith("-")) {
removals.push(line.replace("-", "").trim());
}

// Check if exiting a deletion block
if (line.startsWith("+")) {
additions.push(line.replace("+", "").trim());
}
}
return { additions: additions.join("\n"), removals: removals.join("\n") };
}
const consoleLogCommentBody = `This PR contains console logs. Please review or remove them.`;

export default async function detectConsoleLogs({
installationId,
Expand Down Expand Up @@ -79,26 +57,34 @@ export default async function detectConsoleLogs({
console.log(err);
});
}

const latestCommitHash = await getLatestCommitHash();

const commentPromises = diffFiles.map(async (file) => {
const { additions } = getLineDiffs(file.patch ?? "");

// Leftout comment RegEx
const leftoverCommentRegex = /^\/\*[\s\S]*?\*\/|\/\/[\s\S]*?\n/gm;
const matches = additions.match(leftoverCommentRegex);
const splitAdditions = additions.split("\n");

// RegEx to detect console log equivalents in different languages
const consoleLogRegex = /(console\.log|print|printf|fmt\.Print|log\.Print|NSLog|puts|println|println!)\([^)]*\)(?![^]*?\/\/|[^]*?\/\*|#)/;

const consoleLogRegex =
/(console\.log|print|printf|fmt\.Print|log\.Print|NSLog|puts|println|println!)\([^)]*\)(?![^]*?\/\/|[^]*?\/\*|#)/;

// RegEx to ignore lines that contian //, /*, etc.
const commentRegex = /^\s*\/{2,}|^\s*\/\*|\*\/|#/;

for (let i=0; i < splitAdditions.length; i++) {
for (let i = 0; i < splitAdditions.length; i++) {
let currentLine = splitAdditions[i];
if (!currentLine.match(commentRegex) && currentLine.match(consoleLogRegex)) {
const commentFileDiff = async () => {

if (
!currentLine.match(commentRegex) &&
currentLine.match(consoleLogRegex)
) {
const commentFileDiff = async () => {
const consoleLogPosition = i + 1; // The +1 is because IDEs and GitHub file diff view index LOC at 1, not 0

await octokit
.request("POST /repos/{owner}/{repo}/pulls/{pull_number}/reviews", {
owner,
Expand All @@ -114,18 +100,17 @@ export default async function detectConsoleLogs({
{
path: file.filename,
position: consoleLogPosition || 1, // comment at the beggining of the file by default
body: commentBody,
body: consoleLogCommentBody,
},
],
})
.catch((err) => {
throw err;
});
};

commentFileDiff();
}

}
});
try {
Expand Down
115 changes: 115 additions & 0 deletions utils/codeSmells/detectLefoutComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const { Configuration, OpenAIApi } = require("openai");
import { App } from "@octokit/app";
import { getLineDiffs } from "./getLineDiffs";

const configuration = new Configuration({
apiKey: process.env.OPEN_AI_KEY,
});
const openai = new OpenAIApi(configuration);

const app = new App({
appId: process.env.GITHUB_APP_ID!,
privateKey: process.env.GITHUB_PRIVATE_KEY!,
});

const leftoverCommentBody = `This PR contains leftover multi-line comments. Please review or remove them.`;

export default async function detectLefoutComments({
installationId,
owner,
repo,
issue_number,
reqUrl,
reqEmail,
}: {
prTitle?: string;
businessLogicSummary?: string;
installationId: number;
owner: string;
repo: string;
issue_number: number;
reqUrl: string;
reqEmail: string;
}) {
const octokit = await app.getInstallationOctokit(installationId);

// get the diffs
const { data: diffFiles } = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/files",
{
owner,
repo,
pull_number: issue_number,
}
);

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 = await getLatestCommitHash();

const commentPromises = diffFiles.map(async (file) => {
const { additions } = getLineDiffs(file.patch ?? "");

// Leftout comment RegEx
const leftoverCommentRegex = /^\/\*[\s\S]*?\*\/|\/\/[\s\S]*?\n/gm;
const matches = additions.match(leftoverCommentRegex);

if (matches) {
const firstMatch = matches[0];

// Find the position of the start of the comment, then split the additions into lines
const startPos = additions.indexOf(firstMatch);
const lines = additions.split("\n");

// This is very important
// lineNumber is not the position of the comment in the file, but the line number in the diff (this is on the Octokit docs)
// What's important to note here is that after the header that contains a "@@" on the GitHub code review UI, GitHub adds 3 lines before the code diff. So that's why we need to index this variable at 4.
let lineNumber = 4;

for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.includes(firstMatch)) {
lineNumber = i + 1;
break;
}
}

await octokit
.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: [
{
path: file.filename,
position: lineNumber, // comment at the beggining of the file by default
body: leftoverCommentBody,
},
],
})
.catch((err) => {
throw err;
});
}
});
try {
await Promise.allSettled(commentPromises);
} catch {}
}
23 changes: 23 additions & 0 deletions utils/codeSmells/getLineDiffs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export function getLineDiffs(filePatch: string) {
const additions: string[] = [];
const removals: string[] = [];

// Split the patch into lines
const lines = filePatch.split("\n");

// Loop through lines
for (let i = 0; i < lines.length; i++) {
const line = lines[i];

// Check if entering a deletion block
if (line.startsWith("-")) {
removals.push(line.replace("-", "").trim());
}

// Check if exiting a deletion block
if (line.startsWith("+")) {
additions.push(line.replace("+", "").trim());
}
}
return { additions: additions.join("\n"), removals: removals.join("\n") };
}
Loading