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
25 changes: 20 additions & 5 deletions src/source-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ import { isAbsolute, resolve } from 'path';
import type { ParsedSource } from './types.ts';

/**
* Extract owner/repo from a parsed source for telemetry.
* Extract owner/repo (or group/subgroup/repo for GitLab) from a parsed source
* for lockfile tracking and telemetry.
* Returns null for local paths or unparseable sources.
* Supports any Git host with an owner/repo URL structure, including GitLab subgroups.
*/
export function getOwnerRepo(parsed: ParsedSource): string | null {
if (parsed.type === 'local') {
return null;
}

// Extract from git URL: https://github.com/owner/repo.git or similar
const match = parsed.url.match(/(?:github|gitlab)\.com\/([^/]+)\/([^/]+?)(?:\.git)?$/);
if (match) {
return `${match[1]}/${match[2]}`;
// Only handle HTTP(S) URLs
if (!parsed.url.startsWith('http://') && !parsed.url.startsWith('https://')) {
return null;
}

try {
const url = new URL(parsed.url);
// Get pathname, remove leading slash and trailing .git
let path = url.pathname.slice(1);
path = path.replace(/\.git$/, '');

// Must have at least owner/repo (one slash)
if (path.includes('/')) {
return path;
}
} catch {
// Invalid URL
}

return null;
Expand Down
52 changes: 47 additions & 5 deletions tests/source-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,61 @@ describe('getOwnerRepo', () => {
expect(getOwnerRepo(parsed)).toBeNull();
});

it('getOwnerRepo - custom git host returns null', () => {
it('getOwnerRepo - custom git host extracts owner/repo', () => {
const parsed = parseSource('https://git.example.com/owner/repo.git');
expect(getOwnerRepo(parsed)).toBeNull();
expect(getOwnerRepo(parsed)).toBe('owner/repo');
});

it('getOwnerRepo - SSH format returns null', () => {
const parsed = parseSource('git@github.com:owner/repo.git');
expect(getOwnerRepo(parsed)).toBeNull();
});

it('getOwnerRepo - private GitLab instance returns null', () => {
// This falls through to 'git' type since it's not gitlab.com
it('getOwnerRepo - private GitLab instance extracts owner/repo', () => {
const parsed = parseSource('https://gitlab.company.com/team/repo');
expect(getOwnerRepo(parsed)).toBeNull();
expect(getOwnerRepo(parsed)).toBe('team/repo');
});

it('getOwnerRepo - self-hosted git with .git suffix', () => {
const parsed = parseSource('https://git.internal.io/myteam/skills.git');
expect(getOwnerRepo(parsed)).toBe('myteam/skills');
});

it('getOwnerRepo - URL with query string', () => {
const parsed = { type: 'git', url: 'https://git.example.com/owner/repo?ref=main' } as const;
expect(getOwnerRepo(parsed)).toBe('owner/repo');
});

it('getOwnerRepo - URL with fragment', () => {
const parsed = { type: 'git', url: 'https://git.example.com/owner/repo#readme' } as const;
expect(getOwnerRepo(parsed)).toBe('owner/repo');
});

it('getOwnerRepo - URL with .git and query string', () => {
const parsed = { type: 'git', url: 'https://git.example.com/owner/repo.git?ref=main' } as const;
expect(getOwnerRepo(parsed)).toBe('owner/repo');
});

it('getOwnerRepo - GitLab subgroup (2 levels)', () => {
const parsed = { type: 'git', url: 'https://gitlab.com/group/subgroup/repo' } as const;
expect(getOwnerRepo(parsed)).toBe('group/subgroup/repo');
});

it('getOwnerRepo - GitLab subgroup (3 levels)', () => {
const parsed = { type: 'git', url: 'https://gitlab.com/org/team/project/repo.git' } as const;
expect(getOwnerRepo(parsed)).toBe('org/team/project/repo');
});

it('getOwnerRepo - GitLab subgroup with query string', () => {
const parsed = { type: 'git', url: 'https://gitlab.com/group/subgroup/repo?ref=main' } as const;
expect(getOwnerRepo(parsed)).toBe('group/subgroup/repo');
});

it('getOwnerRepo - self-hosted GitLab with subgroups', () => {
const parsed = {
type: 'git',
url: 'https://gitlab.company.com/division/team/repo.git',
} as const;
expect(getOwnerRepo(parsed)).toBe('division/team/repo');
});
});