Skip to content
Open
Changes from all commits
Commits
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
48 changes: 39 additions & 9 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ const OPENAI_API_KEY = 'OPENAI_API_KEY';
const MAX_PATCH_COUNT = process.env.MAX_PATCH_LENGTH
? +process.env.MAX_PATCH_LENGTH
: Infinity;
const REVIEW_BATCH_SIZE = process.env.REVIEW_BATCH_SIZE
? +process.env.REVIEW_BATCH_SIZE
: 40;
const REVIEW_BATCH_DELAY_MS = process.env.REVIEW_BATCH_DELAY_MS
? +process.env.REVIEW_BATCH_DELAY_MS
: 5000;

export const robot = (app: Probot) => {
const loadChat = async (context: Context) => {
Expand Down Expand Up @@ -241,15 +247,39 @@ export const robot = (app: Probot) => {
}
}
try {
await context.octokit.pulls.createReview({
repo: repo.repo,
owner: repo.owner,
pull_number: context.pullRequest().pull_number,
body: ress.length ? "Code review by ChatGPT" : "LGTM 👍",
event: 'COMMENT',
commit_id: context.payload.pull_request.head.sha,
comments: ress,
});
if (ress.length <= REVIEW_BATCH_SIZE) {
await context.octokit.pulls.createReview({
repo: repo.repo,
owner: repo.owner,
pull_number: context.pullRequest().pull_number,
body: ress.length ? "Code review by ChatGPT" : "LGTM 👍",
event: 'COMMENT',
commit_id: context.payload.pull_request.head.sha,
comments: ress,
});
} else {
// Submit comments in batches to avoid GitHub secondary rate limits
for (let i = 0; i < ress.length; i += REVIEW_BATCH_SIZE) {
const batch = ress.slice(i, i + REVIEW_BATCH_SIZE);
const isFirst = i === 0;
const isLast = i + REVIEW_BATCH_SIZE >= ress.length;

await context.octokit.pulls.createReview({
repo: repo.repo,
owner: repo.owner,
pull_number: context.pullRequest().pull_number,
body: isFirst ? "Code review by ChatGPT" : "",
event: 'COMMENT',
commit_id: context.payload.pull_request.head.sha,
comments: batch,
});

if (!isLast) {
log.info(`Submitted batch ${Math.floor(i / REVIEW_BATCH_SIZE) + 1}, waiting before next batch...`);
await new Promise((resolve) => setTimeout(resolve, REVIEW_BATCH_DELAY_MS));
}
}
}
} catch (e) {
log.info(`Failed to create review`, e);
throw e;
Expand Down