generated from ubiquity-os/plugin-template
-
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
Merged
0x4007
merged 8 commits into
ubiquity-os-marketplace:development
from
sshivaditya2019:development
Oct 28, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5d19ba6
fix: ignore file path and diffs
sshivaditya 312bede
fix: adds sorting diff size and then adds diffs size wise add
sshivaditya 4e928df
fix: removed context optimizer
sshivaditya e0b252a
fix: knip
sshivaditya 9efaf53
fix: conflicts
sshivaditya c4b43d7
fix: updated logic for fetching linked pr
sshivaditya 195e8e8
fix: remove file ignores
sshivaditya 30f876c
fix: package.json missing issue
sshivaditya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,36 @@ 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. |
||
const files = stats.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 +207,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 +314,40 @@ 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.