Skip to content

Commit 9b69d17

Browse files
authored
test: cover PR/MR platform helpers (#426)
1 parent 2ae4f2a commit 9b69d17

File tree

2 files changed

+156
-2
lines changed

2 files changed

+156
-2
lines changed

packages/review-editor/App.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,18 @@ const ReviewApp: React.FC = () => {
132132
const [platformUser, setPlatformUser] = useState<string | null>(null);
133133
const [platformCommentDialog, setPlatformCommentDialog] = useState<{ action: 'approve' | 'comment' } | null>(null);
134134
const [platformGeneralComment, setPlatformGeneralComment] = useState('');
135-
const [platformOpenPR, setPlatformOpenPR] = useState(() => storage.getItem('plannotator-github-open-pr') !== 'false');
135+
const [platformOpenPR, setPlatformOpenPR] = useState(() => {
136+
const platformSetting = storage.getItem('plannotator-platform-open-pr');
137+
if (platformSetting !== null) return platformSetting !== 'false';
138+
139+
const legacyGitHubSetting = storage.getItem('plannotator-github-open-pr');
140+
if (legacyGitHubSetting !== null) {
141+
storage.setItem('plannotator-platform-open-pr', legacyGitHubSetting);
142+
return legacyGitHubSetting !== 'false';
143+
}
144+
145+
return true;
146+
});
136147

137148
// Derived: Platform mode is active when destination is platform AND we have PR/MR metadata
138149
const platformMode = reviewDestination === 'platform' && !!prMetadata;
@@ -1632,7 +1643,7 @@ const ReviewApp: React.FC = () => {
16321643
checked={platformOpenPR}
16331644
onChange={e => {
16341645
setPlatformOpenPR(e.target.checked);
1635-
storage.setItem('plannotator-github-open-pr', String(e.target.checked));
1646+
storage.setItem('plannotator-platform-open-pr', String(e.target.checked));
16361647
}}
16371648
className="rounded border-border"
16381649
/>
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import { describe, expect, test } from "bun:test";
2+
import {
3+
getCliInstallUrl,
4+
getCliName,
5+
getDisplayRepo,
6+
getMRLabel,
7+
getMRNumberLabel,
8+
getPlatformLabel,
9+
parsePRUrl,
10+
prRefFromMetadata,
11+
type PRMetadata,
12+
} from "./pr-provider";
13+
14+
describe("pr-provider platform helpers", () => {
15+
test("parses GitHub PR URLs including nested suffixes", () => {
16+
const ref = parsePRUrl("https://github.com/backnotprop/plannotator/pull/364/files");
17+
18+
expect(ref).toEqual({
19+
platform: "github",
20+
owner: "backnotprop",
21+
repo: "plannotator",
22+
number: 364,
23+
});
24+
});
25+
26+
test("parses GitLab.com MR URLs", () => {
27+
const ref = parsePRUrl("https://gitlab.com/group/project/-/merge_requests/42/diffs");
28+
29+
expect(ref).toEqual({
30+
platform: "gitlab",
31+
host: "gitlab.com",
32+
projectPath: "group/project",
33+
iid: 42,
34+
});
35+
});
36+
37+
test("parses self-hosted GitLab MR URLs with nested groups", () => {
38+
const ref = parsePRUrl("https://gitlab.example.com/group/subgroup/project/-/merge_requests/7");
39+
40+
expect(ref).toEqual({
41+
platform: "gitlab",
42+
host: "gitlab.example.com",
43+
projectPath: "group/subgroup/project",
44+
iid: 7,
45+
});
46+
});
47+
48+
test("returns null for unsupported URLs", () => {
49+
expect(parsePRUrl("https://example.com/not-a-pr/123")).toBeNull();
50+
expect(parsePRUrl("")).toBeNull();
51+
});
52+
53+
test("formats platform-aware labels for GitHub and GitLab", () => {
54+
const githubMeta: PRMetadata = {
55+
platform: "github",
56+
owner: "backnotprop",
57+
repo: "plannotator",
58+
number: 364,
59+
title: "GitHub PR",
60+
author: "backnotprop",
61+
baseBranch: "main",
62+
headBranch: "feature/github",
63+
baseSha: "base",
64+
headSha: "head",
65+
url: "https://github.com/backnotprop/plannotator/pull/364",
66+
};
67+
68+
const gitlabMeta: PRMetadata = {
69+
platform: "gitlab",
70+
host: "gitlab.example.com",
71+
projectPath: "group/project",
72+
iid: 42,
73+
title: "GitLab MR",
74+
author: "alice",
75+
baseBranch: "main",
76+
headBranch: "feature/gitlab",
77+
baseSha: "base",
78+
headSha: "head",
79+
url: "https://gitlab.example.com/group/project/-/merge_requests/42",
80+
};
81+
82+
expect(getPlatformLabel(githubMeta)).toBe("GitHub");
83+
expect(getMRLabel(githubMeta)).toBe("PR");
84+
expect(getMRNumberLabel(githubMeta)).toBe("#364");
85+
expect(getDisplayRepo(githubMeta)).toBe("backnotprop/plannotator");
86+
87+
expect(getPlatformLabel(gitlabMeta)).toBe("GitLab");
88+
expect(getMRLabel(gitlabMeta)).toBe("MR");
89+
expect(getMRNumberLabel(gitlabMeta)).toBe("!42");
90+
expect(getDisplayRepo(gitlabMeta)).toBe("group/project");
91+
});
92+
93+
test("reconstructs refs and CLI metadata for each platform", () => {
94+
const githubMeta: PRMetadata = {
95+
platform: "github",
96+
owner: "backnotprop",
97+
repo: "plannotator",
98+
number: 1,
99+
title: "GitHub PR",
100+
author: "backnotprop",
101+
baseBranch: "main",
102+
headBranch: "feature/github",
103+
baseSha: "base",
104+
headSha: "head",
105+
url: "https://github.com/backnotprop/plannotator/pull/1",
106+
};
107+
108+
const gitlabMeta: PRMetadata = {
109+
platform: "gitlab",
110+
host: "gitlab.example.com",
111+
projectPath: "group/project",
112+
iid: 2,
113+
title: "GitLab MR",
114+
author: "alice",
115+
baseBranch: "main",
116+
headBranch: "feature/gitlab",
117+
baseSha: "base",
118+
headSha: "head",
119+
url: "https://gitlab.example.com/group/project/-/merge_requests/2",
120+
};
121+
122+
const githubRef = prRefFromMetadata(githubMeta);
123+
const gitlabRef = prRefFromMetadata(gitlabMeta);
124+
125+
expect(githubRef).toEqual({
126+
platform: "github",
127+
owner: "backnotprop",
128+
repo: "plannotator",
129+
number: 1,
130+
});
131+
expect(gitlabRef).toEqual({
132+
platform: "gitlab",
133+
host: "gitlab.example.com",
134+
projectPath: "group/project",
135+
iid: 2,
136+
});
137+
138+
expect(getCliName(githubRef)).toBe("gh");
139+
expect(getCliInstallUrl(githubRef)).toBe("https://cli.github.com");
140+
expect(getCliName(gitlabRef)).toBe("glab");
141+
expect(getCliInstallUrl(gitlabRef)).toBe("https://gitlab.com/gitlab-org/cli");
142+
});
143+
});

0 commit comments

Comments
 (0)