Skip to content

Commit

Permalink
fix: remove empty strings and fix file exlcusion
Browse files Browse the repository at this point in the history
  • Loading branch information
ishowvel committed Jan 7, 2025
1 parent 23a636c commit 4a875b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 41 deletions.
24 changes: 13 additions & 11 deletions src/helpers/excluded-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,33 @@ async function parseGitAttributes(content: string): Promise<GitAttributes[]> {
}

export async function getExcludedFiles(context: Context) {
const [gitIgnoreContent, gitAttributesContent] = await Promise.all([getFileContent(".gitignore", context), getFileContent(".gitattributes", context)]);
const [gitIgnoreContent, gitAttributesContent] = await Promise.all([getFileContent(context, ".gitignore"), getFileContent(context, ".gitattributes")]);

const gitAttributesLinguistGenerated = (await parseGitAttributes(gitAttributesContent))
.filter((v) => v.attributes["linguist-generated"])
.map((v) => v.pattern);
const gitIgnoreExcludedFiles = gitIgnoreContent.split("\n").filter((v) => !v.startsWith("#"));
const gitAttributesLinguistGenerated = gitAttributesContent
? (await parseGitAttributes(gitAttributesContent)).filter((v) => v.attributes["linguist-generated"]).map((v) => v.pattern)
: [];
const gitIgnoreExcludedFiles = gitIgnoreContent ? gitIgnoreContent.split("\n").filter((v) => !v.startsWith("#")) : [];
return [...gitAttributesLinguistGenerated, ...gitIgnoreExcludedFiles];
}

async function getFileContent(path: string, context: Context): Promise<string> {
async function getFileContent(context: Context, path: string): Promise<string | null> {
try {
const response = await context.octokit.rest.repos.getContent({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
path,
ref: context.payload.pull_request.head.sha,
ref: context.payload.pull_request.base.sha,
});

// GitHub API returns content as base64
if ("content" in response.data && !Array.isArray(response.data)) {
return Buffer.from(response.data.content, "base64").toString("utf-8");
}
return "";
} catch {
// File doesn't exist
return "";
return null;
} catch (err) {
if (err instanceof Error && "status" in err && err.status === 404) {
return null;
}
throw context.logger.error(`Error fetching files to be excluded ${err}`);
}
}
2 changes: 1 addition & 1 deletion src/helpers/pull-helpers/fetch-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export async function fetchPullRequestDiff(context: Context, org: string, repo:
return { diff: null };
}

return await processPullRequestDiff(diff, tokenLimits, context.logger);
return await processPullRequestDiff(diff, tokenLimits, context);
}
47 changes: 18 additions & 29 deletions src/helpers/pull-helpers/pull-request-parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ function selectIncludedFiles(
return includedFiles;
}

export async function processPullRequestDiff(diff: string, tokenLimits: TokenLimits, logger: Context["logger"]) {
const excludedFilePatterns = await getExcludedFiles();
export async function processPullRequestDiff(diff: string, tokenLimits: TokenLimits, context: Context) {
const excludedFilePatterns = await getExcludedFiles(context);
const sortedDiffs = await filterAndSortDiffs(diff, excludedFilePatterns);

const includedFiles = selectIncludedFiles(sortedDiffs, tokenLimits, logger);
const includedFiles = selectIncludedFiles(sortedDiffs, tokenLimits, context.logger);

if (includedFiles.length === 0) {
logger.error(`Cannot include any files from diff without exceeding token limits.`);
context.logger.error(`Cannot include any files from diff without exceeding token limits.`);
return { diff: null };
}

Expand All @@ -66,29 +66,18 @@ async function encodeAsync(text: string, options: EncodeOptions): Promise<number

// Helper to parse a diff into per-file diffs
function parsePerFileDiffs(diff: string): { filename: string; diffContent: string }[] {
// regex to capture diff sections, including the last file
const diffPattern = /^diff --git a\/(.*?) b\/.*$/gm;
let match: RegExpExecArray | null;
const perFileDiffs = [];
let lastIndex = 0;

// iterate over each file in the diff
while ((match = diffPattern.exec(diff)) !== null) {
const filename = match[1];
const startIndex = match.index;

// if we have pushed a file into the array, "append" the diff content
if (perFileDiffs.length > 0) {
perFileDiffs[perFileDiffs.length - 1].diffContent = diff.substring(lastIndex, startIndex).trim();
}

perFileDiffs.push({ filename, diffContent: "" });
lastIndex = startIndex;
}
// append the last file's diff content
if (perFileDiffs.length > 0 && lastIndex < diff.length) {
perFileDiffs[perFileDiffs.length - 1].diffContent = diff.substring(lastIndex).trim();
}

return perFileDiffs;
// Split the diff string into chunks for each file
const fileDiffs = diff.split(/^diff --git /gm).filter((chunk) => chunk.trim() !== "");

return fileDiffs.map((chunk) => {
// Extract the filename and content from each chunk
const lines = chunk.split("\n");
const firstLine = lines.shift()?.trim() || "";
const [filename] = firstLine.split(" ").map((part) => part.replace(/^a\//, ""));

return {
filename,
diffContent: `diff --git ${firstLine}\n${lines.join("\n").trim()}`,
};
});
}

0 comments on commit 4a875b9

Please sign in to comment.