Initial open source GitHub PR review bot
This commit is contained in:
commit
c6bf0346b4
23 files changed
+4577
No files matched your search
@@ -0,0 +1,17 @@
|
||||
import { createAppAuth } from "@octokit/auth-app";
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import { config } from "../config.js";
|
||||
|
||||
export async function getInstallationOctokit(installationId: number): Promise<Octokit> {
|
||||
const auth = createAppAuth({
|
||||
appId: config.githubAppId,
|
||||
privateKey: config.githubPrivateKey,
|
||||
installationId
|
||||
});
|
||||
|
||||
const installationAuthentication = await auth({ type: "installation" });
|
||||
|
||||
return new Octokit({
|
||||
auth: installationAuthentication.token
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import type { Octokit } from "@octokit/rest";
|
||||
|
||||
export async function requiredChecksAreGreen(
|
||||
octokit: Octokit,
|
||||
params: {
|
||||
owner: string;
|
||||
repo: string;
|
||||
ref: string;
|
||||
}
|
||||
): Promise<{ ok: boolean; reason?: string }> {
|
||||
const [checks, statuses] = await Promise.all([
|
||||
octokit.rest.checks.listForRef({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
ref: params.ref
|
||||
}),
|
||||
octokit.rest.repos.getCombinedStatusForRef({
|
||||
owner: params.owner,
|
||||
repo: params.repo,
|
||||
ref: params.ref
|
||||
})
|
||||
]);
|
||||
|
||||
const failedCheck = checks.data.check_runs.find((check) => {
|
||||
return (
|
||||
check.status !== "completed" ||
|
||||
!["success", "neutral", "skipped"].includes(check.conclusion ?? "")
|
||||
);
|
||||
});
|
||||
|
||||
if (failedCheck) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: `Check "${failedCheck.name}" is ${failedCheck.status}/${failedCheck.conclusion ?? "pending"}.`
|
||||
};
|
||||
}
|
||||
|
||||
if (statuses.data.state !== "success" && statuses.data.statuses.length > 0) {
|
||||
return {
|
||||
ok: false,
|
||||
reason: `Commit status is ${statuses.data.state}.`
|
||||
};
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import type { DiffPosition, PullRequestFile } from "../types.js";
|
||||
|
||||
export function collectValidNewLines(files: PullRequestFile[]): Set<string> {
|
||||
const validLines = new Set<string>();
|
||||
|
||||
for (const file of files) {
|
||||
if (!file.patch) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let newLine = 0;
|
||||
for (const rawLine of file.patch.split("\n")) {
|
||||
if (rawLine.startsWith("@@")) {
|
||||
const match = /\+(\d+)(?:,\d+)?/.exec(rawLine);
|
||||
if (match) {
|
||||
newLine = Number(match[1]);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rawLine.startsWith("+") && !rawLine.startsWith("+++")) {
|
||||
validLines.add(`${file.filename}:${newLine}`);
|
||||
newLine += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!rawLine.startsWith("-")) {
|
||||
newLine += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return validLines;
|
||||
}
|
||||
|
||||
export function toDiffPosition(
|
||||
file: PullRequestFile,
|
||||
line: number,
|
||||
validLines: Set<string>
|
||||
): DiffPosition | null {
|
||||
if (validLines.has(`${file.filename}:${line}`)) {
|
||||
return {
|
||||
path: file.filename,
|
||||
line,
|
||||
side: "RIGHT"
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Webhooks } from "@octokit/webhooks";
|
||||
import { config } from "../config.js";
|
||||
import { getInstallationOctokit } from "./app.js";
|
||||
import { logger } from "../logger.js";
|
||||
import { processPullRequest } from "../review/processor.js";
|
||||
|
||||
export function createGitHubWebhooks(): Webhooks {
|
||||
const webhooks = new Webhooks({
|
||||
secret: config.webhookSecret
|
||||
});
|
||||
|
||||
webhooks.on(
|
||||
["pull_request.opened", "pull_request.synchronize", "pull_request.reopened", "pull_request.ready_for_review"],
|
||||
async ({ payload }) => {
|
||||
const installationId = payload.installation?.id;
|
||||
if (!installationId) {
|
||||
logger.warn({ repository: payload.repository.full_name, pullNumber: payload.pull_request.number }, "Skipping PR without installation id.");
|
||||
return;
|
||||
}
|
||||
|
||||
const octokit = await getInstallationOctokit(installationId);
|
||||
await processPullRequest(octokit, {
|
||||
owner: payload.repository.owner.login,
|
||||
repo: payload.repository.name,
|
||||
pullNumber: payload.pull_request.number,
|
||||
installationId
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
webhooks.onError((error) => {
|
||||
logger.error({ error }, "Webhook handler failed.");
|
||||
});
|
||||
|
||||
return webhooks;
|
||||
}
|
||||
Reference in new issue
Block a user