-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: ignore file path and diffs #21
Changes from 6 commits
5d19ba6
312bede
4e928df
e0b252a
9efaf53
c4b43d7
195e8e8
30f876c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { GithubDiff } from "github-diff-tool"; | ||
import { createKey, getAllStreamlinedComments } from "../handlers/comments"; | ||
import { Context } from "../types"; | ||
import { IssueWithUser, SimplifiedComment, User } from "../types/github-types"; | ||
import { IssueWithUser, LinkedPullsToIssue, SimplifiedComment, User } from "../types/github-types"; | ||
import { FetchParams, Issue, Comments, LinkedIssues } from "../types/github-types"; | ||
import { StreamlinedComment } from "../types/llm"; | ||
import { | ||
|
@@ -164,18 +165,40 @@ export async function mergeCommentsAndFetchSpec( | |
* @param issue - The pull request number. | ||
* @returns A promise that resolves to the diff of the pull request as a string, or null if an error occurs. | ||
*/ | ||
export async function fetchPullRequestDiff(context: Context, org: string, repo: string, issue: number): Promise<string | null> { | ||
export async function fetchPullRequestDiff(context: Context, org: string, repo: string, issue: number): Promise<{ diff: string; diffSize: number }[] | null> { | ||
const { octokit, logger } = context; | ||
try { | ||
const { data } = await octokit.pulls.get({ | ||
owner: org, | ||
repo, | ||
pull_number: issue, | ||
mediaType: { | ||
format: "diff", | ||
}, | ||
}); | ||
return data as unknown as string; | ||
const githubDiff = new GithubDiff(octokit); | ||
//Fetch the statistics of the pull request | ||
const stats = await githubDiff.getPullRequestStats(org, repo, issue); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lib is only called twice and can be done just as simply with one or two rest calls I'm sure, it's not clear if this is ideal relying on a lib for this. Nothing to hold back the PR for just an observation. |
||
//Ignore files like in dist or build or .lock files | ||
const ignoredFiles = (await buildIgnoreFilesFromGitIgnore(context, org, repo)) || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey i just realized this is pointless. If its ignored it wont be on git or on the diff. Remove all this logic and just rely on filtering files out based on largest amount of changes to smallest. |
||
const files = stats | ||
.filter((file) => !ignoredFiles.some((pattern) => file.filename.includes(pattern))) | ||
.map((file) => ({ filename: file.filename, diffSizeInBytes: file.diffSizeInBytes })); | ||
//Fetch the diff of the files | ||
const prDiffs = await Promise.all( | ||
files.map(async (file) => { | ||
let diff = null; | ||
try { | ||
diff = await githubDiff.getPullRequestDiff({ | ||
owner: org, | ||
repo, | ||
pullNumber: issue, | ||
filePath: file.filename, | ||
}); | ||
} catch { | ||
logger.error(`Error fetching pull request diff for the file`, { | ||
owner: org, | ||
repo, | ||
pull_number: issue, | ||
file: file.filename, | ||
}); | ||
} | ||
return diff ? { diff: file.filename + diff, diffSize: file.diffSizeInBytes } : null; | ||
}) | ||
); | ||
return prDiffs.filter((diff): diff is { diff: string; diffSize: number } => diff !== null).sort((a, b) => a.diffSize - b.diffSize); | ||
} catch (error) { | ||
logger.error(`Error fetching pull request diff`, { | ||
error: error as Error, | ||
|
@@ -188,10 +211,9 @@ export async function fetchPullRequestDiff(context: Context, org: string, repo: | |
} | ||
|
||
/** | ||
* Fetches the details of a pull request. | ||
* | ||
* @param params - The parameters required to fetch the pull request, including context and other details. | ||
* @returns A promise that resolves to the pull request details or null if an error occurs. | ||
* Fetches an issue from the GitHub API. | ||
* @param params - Context | ||
* @returns A promise that resolves to an issue object or null if an error occurs. | ||
*/ | ||
export async function fetchIssue(params: FetchParams): Promise<Issue | null> { | ||
const { octokit, payload, logger } = params.context; | ||
|
@@ -296,3 +318,68 @@ function castCommentsToSimplifiedComments(comments: Comments, params: FetchParam | |
url: comment.html_url, | ||
})); | ||
} | ||
|
||
export async function fetchLinkedPullRequests(owner: string, repo: string, issueNumber: number, context: Context) { | ||
const query = ` | ||
query($owner: String!, $repo: String!, $issueNumber: Int!) { | ||
repository(owner: $owner, name: $repo) { | ||
issue(number: $issueNumber) { | ||
closedByPullRequestsReferences(first: 100) { | ||
nodes { | ||
number | ||
title | ||
state | ||
merged | ||
url | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
try { | ||
const { repository } = await context.octokit.graphql<LinkedPullsToIssue>(query, { | ||
owner, | ||
repo, | ||
issueNumber, | ||
}); | ||
return repository.issue.closedByPullRequestsReferences.nodes; | ||
} catch (error) { | ||
context.logger.error(`Error fetching linked PRs from issue`, { | ||
error: error as Error, | ||
owner, | ||
repo, | ||
issueNumber, | ||
}); | ||
return null; | ||
} | ||
} | ||
|
||
async function buildIgnoreFilesFromGitIgnore(context: Context, owner: string, repo: string): Promise<string[] | null> { | ||
try { | ||
const gitignore = await context.octokit.rest.repos.getContent({ | ||
owner, | ||
repo, | ||
path: ".gitignore", | ||
}); | ||
// Build an array of files to ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get rid of all this logic too |
||
const ignoreFiles: string[] = []; | ||
if ("content" in gitignore.data) { | ||
const content = Buffer.from(gitignore.data.content, "base64").toString(); | ||
content.split("\n").forEach((line) => { | ||
if (line && !line.startsWith("#")) { | ||
ignoreFiles.push(line); | ||
} | ||
}); | ||
} | ||
return ignoreFiles; | ||
} catch (error) { | ||
context.logger.error(`Error fetching .gitignore file`, { | ||
error: error as Error, | ||
owner, | ||
repo, | ||
}); | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason that you change this? The template uses
:4000
so all other plugins also, it's tedious having to change it back for no reasonThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is port 4000 required by some other service/kernel? I'm currently unable to use it but can switch back if needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The plugin-template comes with
:4000
as standard so it's habitual for plugin devs to copy paste that url into-config.yml
and expecting it to work thinking something is broken for it to be the dev port lmao.Ideally don't commit it if you want to use another port, like running multiple plugins locally for example, always change it back to the template port if you can remember to.