mirror of
https://github.com/freeedcom/ai-codereviewer.git
synced 2025-04-21 01:56:47 +00:00
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:
commit
d1f892332a
4 changed files with 65 additions and 8 deletions
10
.github/workflows/code_review.yml
vendored
10
.github/workflows/code_review.yml
vendored
|
@ -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
35
dist/index.js
vendored
|
@ -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
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
26
src/main.ts
26
src/main.ts
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue