Skip to content

Commit a304ba3

Browse files
authored
Add pathParts to getRepositoryInfo (#205)
1 parent e77e296 commit a304ba3

File tree

4 files changed

+1130
-494
lines changed

4 files changed

+1130
-494
lines changed

index.test.ts

Lines changed: 82 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference lib="dom" />
22
import assert from 'node:assert/strict';
3-
import {test} from 'vitest';
3+
import {test, expect} from 'vitest';
44
import stripIndent from 'strip-indent';
55
import {getAllUrls, getTests} from './collector.js';
66
import * as pageDetect from './index.js';
@@ -153,48 +153,87 @@ test('getRepositoryInfo', () => {
153153
assert.equal(getRepositoryInfoAdapter('https://github.com'), undefined);
154154
assert.equal(getRepositoryInfoAdapter('https://gist.github.com/'), undefined);
155155
assert.equal(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined);
156-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), {
157-
owner: 'refined-github',
158-
name: 'github-url-detection',
159-
nameWithOwner: 'refined-github/github-url-detection',
160-
path: '',
161-
});
162-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), {
163-
owner: 'refined-github',
164-
name: 'github-url-detection',
165-
nameWithOwner: 'refined-github/github-url-detection',
166-
path: '',
167-
});
168-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), {
169-
owner: 'refined-github',
170-
name: 'github-url-detection',
171-
nameWithOwner: 'refined-github/github-url-detection',
172-
path: 'blame/master/package.json',
173-
});
174-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), {
175-
owner: 'refined-github',
176-
name: 'github-url-detection',
177-
nameWithOwner: 'refined-github/github-url-detection',
178-
path: 'commit/57bf4',
179-
});
180-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), {
181-
owner: 'refined-github',
182-
name: 'github-url-detection',
183-
nameWithOwner: 'refined-github/github-url-detection',
184-
path: 'compare/test-branch',
185-
});
186-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), {
187-
owner: 'refined-github',
188-
name: 'github-url-detection',
189-
nameWithOwner: 'refined-github/github-url-detection',
190-
path: 'tree/master/distribution',
191-
});
192-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), {
193-
owner: 'refined-github',
194-
name: 'github-url-detection',
195-
nameWithOwner: 'refined-github/github-url-detection',
196-
path: 'tree/master/distribution',
197-
});
156+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection')).toMatchInlineSnapshot(`
157+
{
158+
"name": "github-url-detection",
159+
"nameWithOwner": "refined-github/github-url-detection",
160+
"owner": "refined-github",
161+
"path": "",
162+
"pathParts": [],
163+
}
164+
`);
165+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/')).toMatchInlineSnapshot(`
166+
{
167+
"name": "github-url-detection",
168+
"nameWithOwner": "refined-github/github-url-detection",
169+
"owner": "refined-github",
170+
"path": "",
171+
"pathParts": [],
172+
}
173+
`);
174+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json')).toMatchInlineSnapshot(`
175+
{
176+
"name": "github-url-detection",
177+
"nameWithOwner": "refined-github/github-url-detection",
178+
"owner": "refined-github",
179+
"path": "blame/master/package.json",
180+
"pathParts": [
181+
"blame",
182+
"master",
183+
"package.json",
184+
],
185+
}
186+
`);
187+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4')).toMatchInlineSnapshot(`
188+
{
189+
"name": "github-url-detection",
190+
"nameWithOwner": "refined-github/github-url-detection",
191+
"owner": "refined-github",
192+
"path": "commit/57bf4",
193+
"pathParts": [
194+
"commit",
195+
"57bf4",
196+
],
197+
}
198+
`);
199+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0')).toMatchInlineSnapshot(`
200+
{
201+
"name": "github-url-detection",
202+
"nameWithOwner": "refined-github/github-url-detection",
203+
"owner": "refined-github",
204+
"path": "compare/test-branch",
205+
"pathParts": [
206+
"compare",
207+
"test-branch",
208+
],
209+
}
210+
`);
211+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution')).toMatchInlineSnapshot(`
212+
{
213+
"name": "github-url-detection",
214+
"nameWithOwner": "refined-github/github-url-detection",
215+
"owner": "refined-github",
216+
"path": "tree/master/distribution",
217+
"pathParts": [
218+
"tree",
219+
"master",
220+
"distribution",
221+
],
222+
}
223+
`);
224+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/')).toMatchInlineSnapshot(`
225+
{
226+
"name": "github-url-detection",
227+
"nameWithOwner": "refined-github/github-url-detection",
228+
"owner": "refined-github",
229+
"path": "tree/master/distribution",
230+
"pathParts": [
231+
"tree",
232+
"master",
233+
"distribution",
234+
],
235+
}
236+
`);
198237
}
199238
});
200239

index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,12 @@ export type RepositoryInfo = {
880880
@example '/user/repo/' -> ''
881881
@example '/settings/token/' -> undefined */
882882
path: string;
883+
884+
/** The `path` segments
885+
@example '/user/repo/' -> []
886+
@example '/user/repo/issues/' -> ['issues']
887+
@example '/user/repo/issues/new' -> ['issues', 'new'] */
888+
pathParts: string[];
883889
};
884890

885891
/**
@@ -909,12 +915,13 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
909915
return;
910916
}
911917

912-
const [owner, name, ...path] = getCleanPathname(url).split('/') as [string, string, string];
918+
const [owner, name, ...pathParts] = getCleanPathname(url).split('/') as [string, string, string];
913919
return {
914920
owner,
915921
name,
922+
pathParts,
916923
nameWithOwner: `${owner}/${name}`,
917-
path: path.join('/'),
924+
path: pathParts.join('/'),
918925
};
919926
};
920927

0 commit comments

Comments
 (0)