Skip to content

Commit cdd8bf0

Browse files
committed
fix: re-use canonicalizeRepoUrl & insert package_repos
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent 20104c8 commit cdd8bf0

4 files changed

Lines changed: 87 additions & 50 deletions

File tree

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

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import { Context } from '@temporalio/activity'
22

3-
import { logAuditFieldChanges } from '@crowd/data-access-layer/src/packages'
3+
import {
4+
getOrCreateRepoByUrl,
5+
logAuditFieldChanges,
6+
upsertPackageRepo,
7+
} from '@crowd/data-access-layer/src/packages'
48
import type { QueryExecutor } from '@crowd/data-access-layer/src/queryExecutor'
59
import { getServiceChildLogger } from '@crowd/logging'
610

711
import { getGoConfig } from '../config'
812
import { getPackagesDb } from '../db'
13+
import { canonicalizeRepoUrl } from '../utils/canonicalizeRepoUrl'
914

1015
import { fetchStatus } from './pkgGoDevClient'
1116
import { fetchLatest } from './proxyClient'
@@ -21,7 +26,7 @@ export interface GoScanCursor {
2126
after: string
2227
}
2328

24-
type GoRow = { purl: string; name: string }
29+
type GoRow = { id: string; purl: string; name: string }
2530

2631
// Two independent purl-keyset cursors — one for critical packages, one for everything else —
2732
// each ordered/paginated purely by purl so WHERE and ORDER BY always match (no gaps, no
@@ -37,7 +42,7 @@ async function getGoBatch(
3742
): Promise<GoRow[]> {
3843
if (batchSize <= 0) return []
3944
return qx.select(
40-
`SELECT purl, name FROM packages
45+
`SELECT id::text AS id, purl, name FROM packages
4146
WHERE ecosystem = 'go' AND is_critical = $(isCritical) AND purl > $(after)
4247
ORDER BY purl ASC
4348
LIMIT $(limit)`,
@@ -75,7 +80,7 @@ export async function enrichGoVersionsBatch(
7580

7681
const { fetchTimeoutMs, proxyConcurrency } = getGoConfig()
7782

78-
const enrichOne = async (row: { purl: string; name: string }): Promise<void> => {
83+
const enrichOne = async (row: GoRow): Promise<void> => {
7984
Context.current().heartbeat(row.purl)
8085
const result = await fetchLatest(row.name, fetchTimeoutMs)
8186
if (isFetchError(result)) {
@@ -85,37 +90,54 @@ export async function enrichGoVersionsBatch(
8590
)
8691
return
8792
}
88-
const changed = await qx.selectOne(
89-
`WITH old AS (
90-
SELECT latest_version AS v, latest_release_at AS t, repository_url AS r
91-
FROM packages WHERE purl = $(purl)
92-
),
93-
upd AS (
94-
UPDATE packages p SET
95-
latest_version = $(version),
96-
latest_release_at = $(releaseAt),
97-
repository_url = COALESCE($(repoUrl), p.repository_url),
98-
last_synced_at = NOW()
99-
WHERE p.purl = $(purl)
100-
RETURNING latest_version AS v, latest_release_at AS t, repository_url AS r
101-
)
102-
SELECT
103-
(SELECT v FROM old) IS DISTINCT FROM (SELECT v FROM upd) AS v_changed,
104-
(SELECT t FROM old) IS DISTINCT FROM (SELECT t FROM upd) AS t_changed,
105-
(SELECT r FROM old) IS DISTINCT FROM (SELECT r FROM upd) AS r_changed`,
106-
{
107-
version: result.version,
108-
releaseAt: result.releaseAt,
109-
repoUrl: result.repoUrl,
110-
purl: row.purl,
111-
},
112-
)
113-
const changedFields = [
114-
changed?.v_changed ? 'packages.latest_version' : null,
115-
changed?.t_changed ? 'packages.latest_release_at' : null,
116-
changed?.r_changed ? 'packages.repository_url' : null,
117-
].filter(Boolean) as string[]
118-
await logAuditFieldChanges(qx, PROXY_SOURCE, row.purl, changedFields)
93+
const repo = result.repoUrl ? canonicalizeRepoUrl(result.repoUrl) : null
94+
95+
await qx.tx(async (t) => {
96+
const changed = await t.selectOne(
97+
`WITH old AS (
98+
SELECT latest_version AS v, latest_release_at AS t, repository_url AS r
99+
FROM packages WHERE purl = $(purl)
100+
),
101+
upd AS (
102+
UPDATE packages p SET
103+
latest_version = $(version),
104+
latest_release_at = $(releaseAt),
105+
repository_url = COALESCE($(repoUrl), p.repository_url),
106+
last_synced_at = NOW()
107+
WHERE p.purl = $(purl)
108+
RETURNING latest_version AS v, latest_release_at AS t, repository_url AS r
109+
)
110+
SELECT
111+
(SELECT v FROM old) IS DISTINCT FROM (SELECT v FROM upd) AS v_changed,
112+
(SELECT t FROM old) IS DISTINCT FROM (SELECT t FROM upd) AS t_changed,
113+
(SELECT r FROM old) IS DISTINCT FROM (SELECT r FROM upd) AS r_changed`,
114+
{
115+
version: result.version,
116+
releaseAt: result.releaseAt,
117+
repoUrl: repo?.url ?? null,
118+
purl: row.purl,
119+
},
120+
)
121+
const changedFields = [
122+
changed?.v_changed ? 'packages.latest_version' : null,
123+
changed?.t_changed ? 'packages.latest_release_at' : null,
124+
changed?.r_changed ? 'packages.repository_url' : null,
125+
].filter(Boolean) as string[]
126+
127+
if (repo) {
128+
const { id: repoId, changedFields: repoChanged } = await getOrCreateRepoByUrl(
129+
t,
130+
repo.url,
131+
repo.host,
132+
)
133+
changedFields.push(...repoChanged)
134+
135+
const linkChanged = await upsertPackageRepo(t, row.id, repoId, 'declared', 0.8)
136+
changedFields.push(...linkChanged)
137+
}
138+
139+
await logAuditFieldChanges(t, PROXY_SOURCE, row.purl, changedFields)
140+
})
119141
}
120142

121143
for (let i = 0; i < rows.length; i += proxyConcurrency) {

services/apps/packages_worker/src/nuget/normalize.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { canonicalizeRepoUrl } from '../utils/canonicalizeRepoUrl'
2+
13
import {
24
NormalizedNuGetPackage,
35
NormalizedNuGetVersion,
@@ -28,16 +30,6 @@ function parsePublishedDate(published: string | undefined): Date | null {
2830

2931
const SCM_HOSTS = ['github.com', 'gitlab.com', 'bitbucket.org']
3032

31-
function normalizeRepoUrl(url: string | undefined): string | null {
32-
if (!url) return null
33-
return url
34-
.trim()
35-
.replace(/\.git$/, '')
36-
.replace(/^git\+/, '')
37-
.replace(/^git:\/\//, 'https://')
38-
.replace(/^http:\/\/github\.com\//, 'https://github.com/')
39-
}
40-
4133
function isScmUrl(url: string | undefined): boolean {
4234
if (!url) return false
4335
try {
@@ -98,8 +90,9 @@ export function normalizeNuGetPackage(
9890
: [...allEntries].reverse()
9991
const nuspecRepoUrl = entriesForRepo.find((e) => e.repository?.url)?.repository?.url
10092
const declaredRepositoryUrl = nuspecRepoUrl ?? null
101-
const repoCandidate = nuspecRepoUrl ?? (isScmUrl(homepage) ? homepage : null)
102-
const repositoryUrl = normalizeRepoUrl(repoCandidate ?? undefined)
93+
const repo =
94+
(nuspecRepoUrl ? canonicalizeRepoUrl(nuspecRepoUrl) : null) ??
95+
(isScmUrl(homepage) ? canonicalizeRepoUrl(homepage) : null)
10396

10497
const keywords = searchResult?.tags && searchResult.tags.length > 0 ? searchResult.tags : null
10598

@@ -154,7 +147,7 @@ export function normalizeNuGetPackage(
154147
description,
155148
homepage: homepage || null,
156149
declaredRepositoryUrl,
157-
repositoryUrl,
150+
repo,
158151
licenses,
159152
licensesRaw,
160153
keywords,

services/apps/packages_worker/src/nuget/runNuGetEnrichmentLoop.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
IDbNuGetVersionUpsert,
33
NuGetPackageToSync,
44
QueryExecutor,
5+
getOrCreateRepoByUrl,
56
listNuGetPackagesToSync,
67
logAuditFieldChange,
78
markNuGetPackageError,
@@ -10,6 +11,7 @@ import {
1011
upsertMaintainer,
1112
upsertNuGetPackage,
1213
upsertNuGetVersionsBatch,
14+
upsertPackageRepo,
1315
} from '@crowd/data-access-layer'
1416
import { getServiceChildLogger } from '@crowd/logging'
1517

@@ -108,7 +110,7 @@ async function processPackage(
108110
description: normalized.description,
109111
homepage: normalized.homepage,
110112
declaredRepositoryUrl: normalized.declaredRepositoryUrl,
111-
repositoryUrl: normalized.repositoryUrl,
113+
repositoryUrl: normalized.repo?.url ?? null,
112114
licenses: normalized.licenses,
113115
licensesRaw: normalized.licensesRaw,
114116
keywords: normalized.keywords,
@@ -122,6 +124,24 @@ async function processPackage(
122124
})
123125
pkgChanged.forEach((f) => changed.add(f))
124126

127+
if (normalized.repo) {
128+
const { id: repoId, changedFields: repoChanged } = await getOrCreateRepoByUrl(
129+
t,
130+
normalized.repo.url,
131+
normalized.repo.host,
132+
)
133+
repoChanged.forEach((f) => changed.add(f))
134+
135+
const linkChanged = await upsertPackageRepo(
136+
t,
137+
packageDbId.toString(),
138+
repoId,
139+
'declared',
140+
0.8,
141+
)
142+
linkChanged.forEach((f) => changed.add(f))
143+
}
144+
125145
if (normalized.versions.length > 0) {
126146
const versionRows: IDbNuGetVersionUpsert[] = normalized.versions.map((v) => ({
127147
packageId: packageDbId,

services/apps/packages_worker/src/nuget/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { CanonicalRepo } from '../utils/canonicalizeRepoUrl'
2+
13
export interface NuGetConfig {
24
batchSize: number
35
concurrency: number
@@ -73,7 +75,7 @@ export interface NormalizedNuGetPackage {
7375
description: string | null
7476
homepage: string | null
7577
declaredRepositoryUrl: string | null
76-
repositoryUrl: string | null
78+
repo: CanonicalRepo | null
7779
licenses: string[] | null
7880
licensesRaw: string | null
7981
keywords: string[] | null

0 commit comments

Comments
 (0)