Merge pull request #10 from cds-snc/feature/validate-line-no

Prevent AI bot comment on code outside of the PR
This commit is contained in:
Jimmy Royer 2024-09-17 15:52:18 -04:00 committed by GitHub
commit d1f892332a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 65 additions and 8 deletions

View file

@ -1,9 +1,9 @@
name: CDS Code Review with OpenAI
on:
pull_request:
types:
- opened
- synchronize
# on:
# pull_request:
# types:
# - opened
# - synchronize
permissions: write-all
jobs:
code_review:

35
dist/index.js vendored
View file

@ -197,10 +197,43 @@ require("./sourcemap-register.js");
for (const file of parsedDiff) {
if (file.to === "/dev/null") continue; // Ignore deleted files
for (const chunk of file.chunks) {
const validLineNumbers = new Set();
chunk.changes.forEach((change) => {
if ("ln" in change && change.ln)
validLineNumbers.add(change.ln);
if ("ln2" in change && change.ln2)
validLineNumbers.add(change.ln2);
// Generate a range of line numbers for additive changes.
if (
"ln1" in change &&
"ln2" in change &&
change.ln1 &&
change.ln2
) {
for (let i = change.ln1; i <= change.ln2; i++) {
validLineNumbers.add(i);
}
}
});
const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = yield getAIResponse(prompt);
if (aiResponse) {
const newComments = createComment(file, chunk, aiResponse);
const validAIResponses = aiResponse.filter((response) =>
validLineNumbers.has(Number(response.lineNumber))
);
// Leave a log for each invalid line numbers for further debugging.
aiResponse.forEach((response) => {
if (!validLineNumbers.has(Number(response.lineNumber))) {
console.log(
`Invalid line number: ${response.lineNumber} in file: ${file.to}\nComment: ${response.reviewComment}`
);
}
});
const newComments = createComment(
file,
chunk,
validAIResponses
);
if (newComments) {
comments.push(...newComments);
}

2
dist/index.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -109,10 +109,34 @@ async function analyzeCode(
for (const file of parsedDiff) {
if (file.to === "/dev/null") continue; // Ignore deleted files
for (const chunk of file.chunks) {
const validLineNumbers = new Set<number>();
chunk.changes.forEach((change: parseDiff.Change) => {
if ("ln" in change && change.ln) validLineNumbers.add(change.ln);
if ("ln2" in change && change.ln2) validLineNumbers.add(change.ln2);
// Generate a range of line numbers for additive changes.
if ("ln1" in change && "ln2" in change && change.ln1 && change.ln2) {
for (let i = change.ln1; i <= change.ln2; i++) {
validLineNumbers.add(i);
}
}
});
const prompt = createPrompt(file, chunk, prDetails);
const aiResponse = await getAIResponse(prompt);
if (aiResponse) {
const newComments = createComment(file, chunk, aiResponse);
const validAIResponses = aiResponse.filter((response) =>
validLineNumbers.has(Number(response.lineNumber))
);
// Leave a log for each invalid line numbers for further debugging.
aiResponse.forEach((response) => {
if (!validLineNumbers.has(Number(response.lineNumber))) {
console.log(
`Invalid line number: ${response.lineNumber} in file: ${file.to}\nComment: ${response.reviewComment}`
);
}
});
const newComments = createComment(file, chunk, validAIResponses);
if (newComments) {
comments.push(...newComments);
}