Skip to content

Commit 7f2e076

Browse files
authored
Feat/track packages (#4169)
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 757af33 commit 7f2e076

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

services/apps/packages_worker/src/deps-dev/canonicalRepoUrl.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const HOST_BY_TYPE: Record<string, string> = {
1+
const DOMAIN_BY_TYPE: Record<string, string> = {
22
GITHUB: 'github.com',
33
GITLAB: 'gitlab.com',
44
BITBUCKET: 'bitbucket.org',
55
}
66

77
export function canonicalRepoUrl(type: string, name: string): string | null {
8-
const host = HOST_BY_TYPE[type]
9-
if (!host) return null
8+
const domain = DOMAIN_BY_TYPE[type]
9+
if (!domain) return null
1010
// Names are bare paths (owner/repo or owner/subgroup/repo) — no host prefix.
1111
// Strip full URL prefix only if present; bare paths pass through untouched.
1212
// TODO(Step 2a): verify against sampled ProjectName shapes from BQ console.
@@ -18,16 +18,22 @@ export function canonicalRepoUrl(type: string, name: string): string | null {
1818
// GitHub enforces case-insensitive uniqueness (can't create Foo and foo as separate orgs/repos),
1919
// so lowercasing is always safe and aligns with deps.dev's canonical form.
2020
// GitLab, Bitbucket, and self-hosted forges are case-sensitive — preserve original casing.
21-
const path = host === 'github.com' ? stripped.toLowerCase() : stripped
21+
const path = type === 'GITHUB' ? stripped.toLowerCase() : stripped
2222
if (!/^[a-zA-Z0-9._-]+\/[a-zA-Z0-9._/-]+$/.test(path)) return null
23-
return `https://${host}/${path}`
23+
return `https://${domain}/${path}`
24+
}
25+
26+
const HOST_BY_DOMAIN: Record<string, string> = {
27+
'github.com': 'github',
28+
'gitlab.com': 'gitlab',
29+
'bitbucket.org': 'bitbucket',
2430
}
2531

2632
export function parseRepoUrl(url: string): { host: string; owner: string; name: string } {
2733
const u = new URL(url)
2834
const parts = u.pathname.slice(1).split('/')
2935
return {
30-
host: u.hostname,
36+
host: HOST_BY_DOMAIN[u.hostname] ?? u.hostname,
3137
owner: parts[0],
3238
name: parts.slice(1).join('/'),
3339
}

services/apps/packages_worker/src/deps-dev/queries/packageReposSql.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ path_computed AS (
1111
SELECT
1212
pm.purl,
1313
CASE pvp.ProjectType
14-
WHEN 'GITHUB' THEN 'github.com'
15-
WHEN 'GITLAB' THEN 'gitlab.com'
16-
WHEN 'BITBUCKET' THEN 'bitbucket.org'
14+
WHEN 'GITHUB' THEN 'github'
15+
WHEN 'GITLAB' THEN 'gitlab'
16+
WHEN 'BITBUCKET' THEN 'bitbucket'
1717
END AS host,
1818
CASE pvp.ProjectType
1919
WHEN 'GITHUB' THEN LOWER(
@@ -40,7 +40,7 @@ path_computed AS (
4040
)
4141
SELECT
4242
purl,
43-
CONCAT('https://', host, '/', path) AS canonical_url,
43+
CONCAT('https://', CASE host WHEN 'github' THEN 'github.com' WHEN 'gitlab' THEN 'gitlab.com' WHEN 'bitbucket' THEN 'bitbucket.org' END, '/', path) AS canonical_url,
4444
confidence
4545
FROM path_computed
4646
WHERE REGEXP_CONTAINS(path, r'^[a-zA-Z0-9._-]+/[a-zA-Z0-9._/-]+$')

services/apps/packages_worker/src/deps-dev/queries/reposSql.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ WITH raw AS (
55
p.Type AS raw_project_type,
66
p.Name AS raw_project_name,
77
CASE p.Type
8-
WHEN 'GITHUB' THEN 'github.com'
9-
WHEN 'GITLAB' THEN 'gitlab.com'
10-
WHEN 'BITBUCKET' THEN 'bitbucket.org'
8+
WHEN 'GITHUB' THEN 'github'
9+
WHEN 'GITLAB' THEN 'gitlab'
10+
WHEN 'BITBUCKET' THEN 'bitbucket'
1111
END AS host,
1212
CASE p.Type
1313
WHEN 'GITHUB' THEN LOWER(
@@ -32,7 +32,7 @@ WITH raw AS (
3232
AND p.Type IN ('GITHUB', 'GITLAB', 'BITBUCKET')
3333
)
3434
SELECT
35-
CONCAT('https://', host, '/', path) AS canonical_url,
35+
CONCAT('https://', CASE raw_project_type WHEN 'GITHUB' THEN 'github.com' WHEN 'GITLAB' THEN 'gitlab.com' WHEN 'BITBUCKET' THEN 'bitbucket.org' END, '/', path) AS canonical_url,
3636
host,
3737
REGEXP_EXTRACT(path, r'^([^/]+)/') AS owner,
3838
REGEXP_EXTRACT(path, r'^[^/]+/(.+)$') AS name,

0 commit comments

Comments
 (0)