Skip to content

Commit 7cb9a5e

Browse files
committed
test: add unit tests for syncLfGithubRepos URL normalization (IN-1251)
Signed-off-by: Joana Maia <jmaia@contractor.linuxfoundation.org>
1 parent 5f25579 commit 7cb9a5e

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { toRepoRow } from '../activities'
4+
5+
describe('toRepoRow', () => {
6+
it('converts a canonical GitHub URL', () => {
7+
expect(toRepoRow('https://github.com/torvalds/linux')).toEqual({
8+
url: 'https://github.com/torvalds/linux',
9+
host: 'github',
10+
owner: 'torvalds',
11+
name: 'linux',
12+
})
13+
})
14+
15+
it('lowercases GitHub URLs', () => {
16+
const row = toRepoRow('https://github.com/Owner/Repo')
17+
expect(row?.url).toBe('https://github.com/owner/repo')
18+
expect(row?.owner).toBe('owner')
19+
expect(row?.name).toBe('repo')
20+
})
21+
22+
it('strips .git suffix', () => {
23+
const row = toRepoRow('https://github.com/owner/repo.git')
24+
expect(row?.url).toBe('https://github.com/owner/repo')
25+
})
26+
27+
it('strips trailing slash', () => {
28+
const row = toRepoRow('https://github.com/owner/repo/')
29+
expect(row?.url).toBe('https://github.com/owner/repo')
30+
})
31+
32+
it('returns null for non-GitHub URL', () => {
33+
expect(toRepoRow('https://gitlab.com/owner/repo')).toBeNull()
34+
})
35+
36+
it('returns null for URL with no repo segment', () => {
37+
expect(toRepoRow('https://github.com/owner')).toBeNull()
38+
})
39+
40+
it('returns null for empty string', () => {
41+
expect(toRepoRow('')).toBeNull()
42+
})
43+
44+
it('returns null for invalid URL', () => {
45+
expect(toRepoRow('not-a-url')).toBeNull()
46+
})
47+
})

services/apps/packages_worker/src/scorecard/activities.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import { buildInsert } from '../deps-dev/sqlUtils'
66

77
const log = getServiceChildLogger('syncLfGithubRepos')
88

9+
type RepoRow = { url: string; host: string; owner: string; name: string }
10+
11+
export function toRepoRow(url: string): RepoRow | null {
12+
let parsed: URL
13+
try {
14+
parsed = new URL(url)
15+
} catch {
16+
return null
17+
}
18+
if (parsed.hostname !== 'github.com') return null
19+
const canonical = canonicalRepoUrl('GITHUB', url)
20+
if (!canonical) return null
21+
const { host, owner, name } = parseRepoUrl(canonical)
22+
return { url: canonical, host, owner, name }
23+
}
24+
925
export async function syncLfGithubRepos(): Promise<{ inserted: number }> {
1026
const cdpDb = await getCdpDb()
1127
const pkgsDb = await getPackagesDb()
@@ -31,15 +47,10 @@ export async function syncLfGithubRepos(): Promise<{ inserted: number }> {
3147

3248
log.info({ count: cdpRows.length }, 'Seeding LF GitHub repos into packages-db')
3349

34-
type RepoRow = { url: string; host: string; owner: string; name: string }
35-
3650
const rows: RepoRow[] = []
3751
for (const { url } of cdpRows) {
38-
const canonical = canonicalRepoUrl('GITHUB', url)
39-
if (!canonical) continue
40-
const { host, owner, name } = parseRepoUrl(canonical)
41-
if (host !== 'github') continue
42-
rows.push({ url: canonical, host, owner, name })
52+
const row = toRepoRow(url)
53+
if (row) rows.push(row)
4354
}
4455

4556
if (rows.length === 0) {

0 commit comments

Comments
 (0)