Skip to content
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
3 changes: 3 additions & 0 deletions packages/shared/src/pullRequestAnchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./pullRequestAnchor.ts";

const gitlab = "https://gitlab.com/group/repo/-/merge_requests/7";
const forgejo = "https://codeberg.org/owner/repo/pulls/7";

const url = "https://github.com/owner/repo/pull/7";
it.each(["discussion_r123", "r123", "discussion-diff-123R5-L9", "discussion-diff-123"])(
Expand Down Expand Up @@ -107,6 +108,7 @@ it("handles encoded hashes and rejects invalid line numbers and URLs", () => {
it("keeps the commit scope of a PR diff permalink", () => {
const sha = "a".repeat(40);
expect(pullRequestLinkCommit(`${url}/commits/${sha}#diff-abcR5`)).toBe(sha);
expect(pullRequestLinkCommit(`${forgejo}/commits/${sha}#diff-abcR5`)).toBe(sha);
expect(pullRequestLinkCommit(`${url}/files#diff-abcR5`)).toBeNull();
expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=${sha}#${"b".repeat(40)}`)).toBe(sha);
expect(pullRequestLinkCommit(`${gitlab}/diffs?commit_id=main`)).toBeNull();
Expand Down Expand Up @@ -153,6 +155,7 @@ it.each([
[`${url}/commits/${"a".repeat(40)}`, "code"],
[`${url}/commits`, "commits"],
[`${url}/checks?check_run_id=1`, "checks"],
[`${forgejo}/files`, "code"],
[`${gitlab}/diffs`, "code"],
[`${gitlab}/commits`, "commits"],
[`${gitlab}/pipelines`, "checks"],
Expand Down
7 changes: 4 additions & 3 deletions packages/shared/src/pullRequestAnchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ function normalizePullRequestCommentAnchor(anchor: string): string {
);
}

// GitHub pull request tabs and their GitLab merge request equivalents.
// GitHub pull request tabs and their GitLab merge request equivalents. Forgejo and Gitea spell the
// path `/pulls/`.
function pullRequestPathTab(pathname: string): PullRequestAnchor | null {
const tab = /\/(?:pull|-\/merge_requests)\/\d+\/([a-z]+)(?:\/(.*))?$/i.exec(pathname);
const tab = /\/(?:pulls?|-\/merge_requests)\/\d+\/([a-z]+)(?:\/(.*))?$/i.exec(pathname);
const rest = tab?.[2] ?? "";
switch (tab?.[1]?.toLowerCase()) {
case "files":
Expand Down Expand Up @@ -206,7 +207,7 @@ export function pullRequestLinkCommit(url: string | null | undefined): string |
const { pathname, searchParams } = new URL(url);
const commit = /\/-\/merge_requests\/\d+\/diffs\/?$/i.test(pathname)
? searchParams.get("commit_id")
: /\/pull\/\d+\/commits\/([a-f0-9]{40})\/?$/i.exec(pathname)?.[1];
: /\/pulls?\/\d+\/commits\/([a-f0-9]{40})\/?$/i.exec(pathname)?.[1];
return commit && /^[a-f0-9]{40}$/i.test(commit) ? commit : null;
} catch {
return null;
Expand Down
Loading