Skip to content

Conversation

DanielLuo7
Copy link

Core functionality

  • summarize changes from file diffs
  • check for bugs from file diffs
  • add comments to PR

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Implements automated code review functionality using OpenAI's GPT-4 for analyzing pull requests, generating change summaries, and detecting potential bugs.

  • Added new src/llm.ts with checkForBugs() and generateSummary() functions for AI-powered code analysis
  • Added createFileComment and createPRComment utilities in src/utils.ts for handling both inline and general PR comments
  • Integrated OpenAI package (v5.8.4) and ngrok for development testing in package.json
  • Security concern: src/index.ts now logs entire webhook payload which may expose sensitive information
  • Enhanced src/handlers.ts with LLM-based diff analysis and automated comment generation

5 files reviewed, 6 comments
Edit PR Review Bot Settings | Greptile

// Webhook endpoint
app.post('/webhook', async (req, res) => {
try {
console.log(req.body)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Remove or guard this log statement. GitHub webhook payloads contain sensitive data. If needed, log specific fields or use debug logging that's disabled in production.

Suggested change
console.log(req.body)
// Log only necessary fields to avoid exposing sensitive data
console.log('Received webhook event:', req.body.action);


const FIRST_LINE = 1;

export const handleWebhook = async (payload: any) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: payload type needs to be properly typed for webhook events rather than 'any' to ensure type safety

Suggested change
export const handleWebhook = async (payload: any) => {
export const handleWebhook = async (payload: WebhookPayload) => {

Comment on lines 44 to 47
function getFileDiffFromRaw(diff: any, filename: string): string | null {
const pattern = new RegExp(`^diff --git a/${filename.replace(/\./g, '\\.')}.*?(?=^diff --git|\\Z)`, 'gms');
const match = diff.match(pattern);
return match ? match[0] : null;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: regex pattern could fail with filenames containing special characters - needs more robust diff parsing

Suggested change
function getFileDiffFromRaw(diff: any, filename: string): string | null {
const pattern = new RegExp(`^diff --git a/${filename.replace(/\./g, '\\.')}.*?(?=^diff --git|\\Z)`, 'gms');
const match = diff.match(pattern);
return match ? match[0] : null;
function getFileDiffFromRaw(diff: any, filename: string): string | null {
const escapedFilename = filename.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const pattern = new RegExp(`^diff --git a/${escapedFilename}.*?(?=^diff --git|\\Z)`, 'gms');
const match = diff.match(pattern);
return match ? match[0] : null;

src/llm.ts Outdated
@@ -0,0 +1,61 @@
import { OpenAI } from "openai"

const openai = new OpenAI({ apiKey: process.env.OPEN_AI_KEY});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider validating process.env.OPEN_AI_KEY exists early to fail fast if API key is missing

Suggested change
const openai = new OpenAI({ apiKey: process.env.OPEN_AI_KEY});
if (!process.env.OPEN_AI_KEY) throw new Error('OPEN_AI_KEY environment variable is required');
const openai = new OpenAI({ apiKey: process.env.OPEN_AI_KEY});

Comment on lines +31 to +37
try {
const output = response.choices[0].message.content?.trim() || "[]";
return JSON.parse(output);
} catch (err){
console.error('Failed to parse bug comment response:', err);
return [];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Add type information to err parameter. Also consider rethrowing or handling specific OpenAI errors differently than JSON parse errors

Comment on lines +51 to +58
const response = await openai.chat.completions.create({
model: 'gpt-4',
temperature: 0.5,
messages: [
{ role: "system", content: SYSTEM_CONTENT },
{ role: "user", content: context}
]
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Missing error handling here - add try/catch like in checkForBugs()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant