Skip to content
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

Linkref #66

Merged
merged 19 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/handlers/issue-deduplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ async function handleSimilarIssuesComment(
const { sentence } = issue.mostSimilarSentence;
// Insert footnote reference in the body
const sentencePattern = new RegExp(`${sentence.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, "g");
updatedBody = updatedBody.replace(sentencePattern, `${sentence}${footnoteRef}`);
updatedBody = updatedBody.replace(sentencePattern, `${sentence} ${footnoteRef}`);

// Initialize footnotes array if not already done
if (!footnotes) {
footnotes = [];
Expand Down
3 changes: 3 additions & 0 deletions tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import threshold95_2 from "../__sample__/match_threshold_95_2.json";
import warning75_1 from "../__sample__/warning_threshold_75_1.json";
import warning75_2 from "../__sample__/warning_threshold_75_2.json";
import taskComplete from "../__sample__/task_complete.json";
import markdownLink1 from "../__sample__/markdown_link_1.json";

interface SampleIssue {
title: string;
Expand Down Expand Up @@ -241,6 +242,8 @@ export function fetchSimilarIssues(type?: string): SampleIssue[] {
return [threshold95_1, threshold95_2];
case "task_complete":
return [taskComplete];
case "markdown_link":
return [markdownLink1];
default:
return [threshold95_1, threshold95_2];
}
Expand Down
4 changes: 4 additions & 0 deletions tests/__sample__/markdown_link_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "Issue with markdown link",
"issue_body": "_Originally posted by @0x4007 in https://www.github.com/ubiquity-os-marketplace/command-start-stop/issues/100#issuecomment-2535532258_"
}
81 changes: 81 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,87 @@ describe("Plugin tests", () => {
expect(comments[0].body).toContain("50% Match");
});

it("When an issue contains markdown links, footnotes should be added after the entire line", async () => {
const [markdownLinkIssue1] = fetchSimilarIssues("markdown_link");
const { context } = createContextIssues(markdownLinkIssue1.issue_body, "markdown1", 7, markdownLinkIssue1.title);

context.adapters.supabase.issue.findSimilarIssues = jest.fn<typeof context.adapters.supabase.issue.findSimilarIssues>().mockResolvedValue([]);
context.adapters.supabase.issue.createIssue = jest.fn(async () => {
createIssue(
markdownLinkIssue1.issue_body,
"markdown1",
markdownLinkIssue1.title,
7,
{ login: "test", id: 1 },
"open",
null,
STRINGS.TEST_REPO,
STRINGS.USER_1
);
});

await runPlugin(context);

const { context: context2 } = createContextIssues(markdownLinkIssue1.issue_body, "markdown2", 8, markdownLinkIssue1.title);
context2.adapters.supabase.issue.findSimilarIssues = jest
.fn<typeof context2.adapters.supabase.issue.findSimilarIssues>()
.mockResolvedValue([{ issue_id: "markdown1", similarity: 0.8 }] as unknown as IssueSimilaritySearchResult[]);

context2.octokit.graphql = jest.fn<typeof context2.octokit.graphql>().mockResolvedValue({
node: {
title: markdownLinkIssue1.title,
url: STRINGS.ISSUE_URL,
number: 7,
body: markdownLinkIssue1.issue_body,
repository: {
name: STRINGS.TEST_REPO,
owner: {
login: STRINGS.USER_1,
},
},
},
}) as unknown as typeof context2.octokit.graphql;

context2.adapters.supabase.issue.createIssue = jest.fn(async () => {
createIssue(
markdownLinkIssue1.issue_body,
"markdown2",
markdownLinkIssue1.title,
8,
{ login: "test", id: 1 },
"open",
null,
STRINGS.TEST_REPO,
STRINGS.USER_1
);
});

context2.octokit.rest.issues.update = jest.fn(async (params: { owner: string; repo: string; issue_number: number; body: string }) => {
// The footnote should be added after the entire line containing the markdown link
db.issue.update({
where: {
number: { equals: params.issue_number },
},
data: {
body: params.body,
},
});
}) as unknown as typeof octokit.rest.issues.update;

await runPlugin(context2);

const issue = db.issue.findFirst({ where: { node_id: { equals: "markdown2" } } }) as unknown as Context["payload"]["issue"];
expect(issue.state).toBe("open");
// Verify the footnote is added after the line containing the markdown link
expect(issue.body).toContain(
"_Originally posted by @0x4007 in https://www.github.com/ubiquity-os-marketplace/command-start-stop/issues/100#issuecomment-2535532258_ [^01^]"
);
// Verify the markdown link is not broken
expect(issue.body).not.toContain(
"_Originally posted by @0x4007 in https://www.github.com/ubiquity-os-marketplace/command-start-stop/issues/100#issuecomment-2535532258_[^01^]"
);
});

function createContext(
commentBody: string = "Hello, world!",
repoId: number = 1,
Expand Down
Loading