Skip to content

Add pathParts to getRepositoryInfo #205

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
265 changes: 265 additions & 0 deletions __snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`getRepositoryInfo 1`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`;

exports[`getRepositoryInfo 2`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`;

exports[`getRepositoryInfo 3`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "blame/master/package.json",
"pathParts": [
"blame",
"master",
"package.json",
],
}
`;

exports[`getRepositoryInfo 4`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "commit/57bf4",
"pathParts": [
"commit",
"57bf4",
],
}
`;

exports[`getRepositoryInfo 5`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "compare/test-branch",
"pathParts": [
"compare",
"test-branch",
],
}
`;

exports[`getRepositoryInfo 6`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`;

exports[`getRepositoryInfo 7`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`;

exports[`getRepositoryInfo 8`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`;

exports[`getRepositoryInfo 9`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`;

exports[`getRepositoryInfo 10`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "blame/master/package.json",
"pathParts": [
"blame",
"master",
"package.json",
],
}
`;

exports[`getRepositoryInfo 11`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "commit/57bf4",
"pathParts": [
"commit",
"57bf4",
],
}
`;

exports[`getRepositoryInfo 12`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "compare/test-branch",
"pathParts": [
"compare",
"test-branch",
],
}
`;

exports[`getRepositoryInfo 13`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`;

exports[`getRepositoryInfo 14`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`;

exports[`getRepositoryInfo 15`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`;

exports[`getRepositoryInfo 16`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "",
"pathParts": [],
}
`;

exports[`getRepositoryInfo 17`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "blame/master/package.json",
"pathParts": [
"blame",
"master",
"package.json",
],
}
`;

exports[`getRepositoryInfo 18`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "commit/57bf4",
"pathParts": [
"commit",
"57bf4",
],
}
`;

exports[`getRepositoryInfo 19`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "compare/test-branch",
"pathParts": [
"compare",
"test-branch",
],
}
`;

exports[`getRepositoryInfo 20`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`;

exports[`getRepositoryInfo 21`] = `
{
"name": "github-url-detection",
"nameWithOwner": "refined-github/github-url-detection",
"owner": "refined-github",
"path": "tree/master/distribution",
"pathParts": [
"tree",
"master",
"distribution",
],
}
`;
51 changes: 8 additions & 43 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference lib="dom" />
import assert from 'node:assert/strict';
import {test} from 'vitest';
import {test, expect} from 'vitest';
import stripIndent from 'strip-indent';
import {getAllUrls, getTests} from './collector.js';
import * as pageDetect from './index.js';
Expand Down Expand Up @@ -153,48 +153,13 @@ test('getRepositoryInfo', () => {
assert.equal(getRepositoryInfoAdapter('https://github.com'), undefined);
assert.equal(getRepositoryInfoAdapter('https://gist.github.com/'), undefined);
assert.equal(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined);
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: '',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: '',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'blame/master/package.json',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'commit/57bf4',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'compare/test-branch',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'tree/master/distribution',
});
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'tree/master/distribution',
});
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection')).toMatchSnapshot();
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/')).toMatchSnapshot();
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json')).toMatchSnapshot();
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4')).toMatchSnapshot();
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0')).toMatchSnapshot();
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution')).toMatchSnapshot();
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/')).toMatchSnapshot();
}
});

Expand Down
11 changes: 9 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,12 @@ export type RepositoryInfo = {
@example '/user/repo/' -> ''
@example '/settings/token/' -> undefined */
path: string;

/** The `path` segments
@example '/user/repo/' -> []
@example '/user/repo/issues/' -> ['issues']
@example '/user/repo/issues/new' -> ['issues', 'new'] */
pathParts: string[];
};

/**
Expand Down Expand Up @@ -886,12 +892,13 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
return;
}

const [owner, name, ...path] = getCleanPathname(url).split('/') as [string, string, string];
const [owner, name, ...pathParts] = getCleanPathname(url).split('/') as [string, string, string];
return {
owner,
name,
pathParts,
nameWithOwner: `${owner}/${name}`,
path: path.join('/'),
path: pathParts.join('/'),
};
};

Expand Down