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
58 changes: 55 additions & 3 deletions packages/das/src/webhook/github-fetcher.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ interface ClosingIssueReference {
repository?: { nameWithOwner?: string } | null;
}

interface GitHubBlob {
text?: string | null;
byteSize?: number | null;
isBinary?: boolean | null;
}

interface BlobRequirement {
blob: GitHubBlob | null | undefined;
required: boolean;
side: "base" | "head";
repoFullName: string;
prNumber: number;
filename: string;
sha: string | null;
}

// Files larger than this are stored with null content (AST parsing is wasteful past this).
const MAX_FILE_SIZE_BYTES = 1_000_000;

Expand Down Expand Up @@ -747,8 +763,27 @@ export class GitHubFetcherService implements OnModuleInit {
for (let i = 0; i < batch.length; i++) {
const file = batch[i];

const baseBlob = repoData[`base${i}`];
const headBlob = repoData[`head${i}`];
const baseBlob = repoData[`base${i}`] as GitHubBlob | null | undefined;
const headBlob = repoData[`head${i}`] as GitHubBlob | null | undefined;

this.assertRequiredBlob({
blob: baseBlob,
required: file.status !== "added" && baseSha !== null,
side: "base",
repoFullName,
prNumber,
filename: file.previous_filename ?? file.filename,
sha: baseSha,
});
this.assertRequiredBlob({
blob: headBlob,
required: file.status !== "removed",
side: "head",
repoFullName,
prNumber,
filename: file.filename,
sha: headSha,
});

const isBinary = !!headBlob?.isBinary || !!baseBlob?.isBinary;

Expand All @@ -771,7 +806,24 @@ export class GitHubFetcherService implements OnModuleInit {
}
}

private extractBlobText(blob: any): string | null {
private assertRequiredBlob({
blob,
required,
side,
repoFullName,
prNumber,
filename,
sha,
}: BlobRequirement): void {
if (!required || blob != null) return;

throw new Error(
`Missing required ${side} blob for ${repoFullName}#${prNumber} ` +
`${filename} at ${sha ?? "unknown sha"}`,
);
}

private extractBlobText(blob: GitHubBlob | null | undefined): string | null {
if (!blob) return null;
if (blob.isBinary) return null;
if (
Expand Down
Loading