-
Notifications
You must be signed in to change notification settings - Fork 728
feat: implement nuget registry worker [CM-1276] #4265
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd132b7
feat: implement nuget worker
mbani01 35a02b9
fix: code review fixes
mbani01 8de3017
fix: remove unecessary user-agent
mbani01 af8251d
fix: fixes after prod test
mbani01 3036aaa
Merge branch 'main' into feat/nuget-worker
mbani01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
backend/src/osspckgs/migrations/V1782345600__nuget_total_downloads.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE packages ADD COLUMN IF NOT EXISTS total_downloads bigint; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| DOCKERFILE="./services/docker/Dockerfile.packages" | ||
| CONTEXT="../" | ||
| REPO="sjc.ocir.io/axbydjxa5zuh/packages" | ||
| SERVICES="github-repos-enricher bq-dataset-ingest npm-worker maven-worker osv-worker dockerhub-sync cargo-worker go-worker" | ||
| SERVICES="github-repos-enricher bq-dataset-ingest npm-worker maven-worker osv-worker dockerhub-sync cargo-worker go-worker nuget-worker" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| version: '3.1' | ||
|
|
||
| x-env-args: &env-args | ||
| DOCKER_BUILDKIT: 1 | ||
| NODE_ENV: docker | ||
| SERVICE: nuget-worker | ||
| SHELL: /bin/sh | ||
| SUPPRESS_NO_CONFIG_WARNING: 'true' | ||
| CROWD_TEMPORAL_TASKQUEUE: nuget-worker | ||
|
|
||
| services: | ||
| nuget-worker: | ||
| build: | ||
| context: ../../ | ||
| dockerfile: ./scripts/services/docker/Dockerfile.packages | ||
| command: 'pnpm run start:nuget-worker' | ||
| working_dir: /usr/crowd/app/services/apps/packages_worker | ||
| env_file: | ||
| - ../../backend/.env.dist.local | ||
| - ../../backend/.env.dist.composed | ||
| - ../../backend/.env.override.local | ||
| - ../../backend/.env.override.composed | ||
| environment: | ||
| <<: *env-args | ||
| restart: always | ||
| networks: | ||
| - crowd-bridge | ||
|
|
||
| nuget-worker-dev: | ||
| build: | ||
| context: ../../ | ||
| dockerfile: ./scripts/services/docker/Dockerfile.packages | ||
| command: 'pnpm run dev:nuget-worker' | ||
| working_dir: /usr/crowd/app/services/apps/packages_worker | ||
| env_file: | ||
| - ../../backend/.env.dist.local | ||
| - ../../backend/.env.dist.composed | ||
| - ../../backend/.env.override.local | ||
| - ../../backend/.env.override.composed | ||
| environment: | ||
| <<: *env-args | ||
| hostname: nuget-worker | ||
| networks: | ||
| - crowd-bridge | ||
| volumes: | ||
| - ../../services/libs/audit-logs/src:/usr/crowd/app/services/libs/audit-logs/src | ||
| - ../../services/libs/common/src:/usr/crowd/app/services/libs/common/src | ||
| - ../../services/libs/common_services/src:/usr/crowd/app/services/libs/common_services/src | ||
| - ../../services/libs/data-access-layer/src:/usr/crowd/app/services/libs/data-access-layer/src | ||
| - ../../services/libs/database/src:/usr/crowd/app/services/libs/database/src | ||
| - ../../services/libs/integrations/src:/usr/crowd/app/services/libs/integrations/src | ||
| - ../../services/libs/logging/src:/usr/crowd/app/services/libs/logging/src | ||
| - ../../services/libs/nango/src:/usr/crowd/app/services/libs/nango/src | ||
| - ../../services/libs/opensearch/src:/usr/crowd/app/services/libs/opensearch/src | ||
| - ../../services/libs/queue/src:/usr/crowd/app/services/libs/queue/src | ||
| - ../../services/libs/redis/src:/usr/crowd/app/services/libs/redis/src | ||
| - ../../services/libs/snowflake/src:/usr/crowd/app/services/libs/snowflake/src | ||
| - ../../services/libs/telemetry/src:/usr/crowd/app/services/libs/telemetry/src | ||
| - ../../services/libs/temporal/src:/usr/crowd/app/services/libs/temporal/src | ||
| - ../../services/libs/types/src:/usr/crowd/app/services/libs/types/src | ||
| - ../../services/apps/packages_worker/src:/usr/crowd/app/services/apps/packages_worker/src | ||
|
|
||
| networks: | ||
| crowd-bridge: | ||
| external: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { scheduleNuGetIngestion } from '../nuget/schedule' | ||
| import { svc } from '../service' | ||
|
|
||
| setImmediate(async () => { | ||
| await svc.init() | ||
| await scheduleNuGetIngestion() | ||
| await svc.start() | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { getServiceChildLogger } from '@crowd/logging' | ||
|
|
||
| import { getNuGetConfig } from '../config' | ||
| import { getPackagesDb } from '../db' | ||
|
|
||
| import { processBatch } from './runNuGetEnrichmentLoop' | ||
| import { BatchResult } from './types' | ||
|
|
||
| const log = getServiceChildLogger('nuget-activity') | ||
|
|
||
| export async function processNuGetBatch(): Promise<BatchResult> { | ||
| const config = getNuGetConfig() | ||
| const qx = await getPackagesDb() | ||
|
|
||
| const today = new Date().toISOString().split('T')[0] | ||
| const result = await processBatch(qx, config, today) | ||
| log.info({ ...result }, 'NuGet batch complete') | ||
| return result | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import axios from 'axios' | ||
|
|
||
| import { | ||
| NuGetFetchError, | ||
| NuGetRegistrationIndex, | ||
| NuGetRegistrationPage, | ||
| NuGetSearchItem, | ||
| } from './types' | ||
|
|
||
| const SERVICE_INDEX_URL = 'https://api.nuget.org/v3/index.json' | ||
|
|
||
| interface ServiceIndexResource { | ||
| '@id': string | ||
| '@type': string | ||
| } | ||
|
|
||
| interface ServiceIndex { | ||
| resources: ServiceIndexResource[] | ||
| } | ||
|
|
||
| interface ResolvedEndpoints { | ||
| searchBaseUrl: string | ||
| registrationBaseUrl: string | ||
| } | ||
|
|
||
| let cachedEndpoints: ResolvedEndpoints | null = null | ||
|
|
||
| async function resolveEndpoints(): Promise<ResolvedEndpoints> { | ||
| if (cachedEndpoints) return cachedEndpoints | ||
|
|
||
| const resp = await axios.get<ServiceIndex>(SERVICE_INDEX_URL, { | ||
| timeout: 10000, | ||
| }) | ||
|
|
||
| const resources = resp.data.resources | ||
|
|
||
| const findFirst = (...types: string[]): string | undefined => { | ||
| for (const t of types) { | ||
| const found = resources.find((r) => r['@type'] === t) | ||
| if (found) return found['@id'] | ||
| } | ||
| return undefined | ||
| } | ||
|
|
||
| const searchBaseUrl = findFirst('SearchQueryService/3.5.0', 'SearchQueryService') | ||
| const registrationBaseUrl = findFirst( | ||
| 'RegistrationsBaseUrl/3.6.0', | ||
| 'RegistrationsBaseUrl/3.5.0', | ||
| 'RegistrationsBaseUrl', | ||
| ) | ||
|
|
||
| if (!searchBaseUrl || !registrationBaseUrl) { | ||
| throw new Error('NuGet service index missing required endpoints') | ||
| } | ||
|
|
||
| cachedEndpoints = { searchBaseUrl, registrationBaseUrl } | ||
| return cachedEndpoints | ||
| } | ||
|
|
||
| function classifyError(err: unknown): NuGetFetchError | null { | ||
| if (!axios.isAxiosError(err)) return null | ||
| const status = err.response?.status | ||
| if (status === 404) return { kind: 'NOT_FOUND', status, message: err.message } | ||
| if (status === 429) return { kind: 'RATE_LIMIT', status, message: err.message } | ||
| return null | ||
| } | ||
|
|
||
| export async function fetchSearch(packageId: string): Promise<NuGetSearchItem | NuGetFetchError> { | ||
| const { searchBaseUrl } = await resolveEndpoints() | ||
| const lowerPackageId = packageId.toLowerCase() | ||
|
|
||
| try { | ||
| const resp = await axios.get<{ totalHits: number; data: NuGetSearchItem[] }>(searchBaseUrl, { | ||
| params: { | ||
| q: `packageid:${packageId}`, | ||
| prerelease: 'true', | ||
| semVerLevel: '2.0.0', | ||
| take: 20, | ||
| }, | ||
| headers: { | ||
| 'Accept-Encoding': 'gzip', | ||
| }, | ||
| timeout: 15000, | ||
|
Comment on lines
+80
to
+83
Comment on lines
+80
to
+83
|
||
| }) | ||
|
|
||
| const match = resp.data.data.find((item) => item.id.toLowerCase() === lowerPackageId) | ||
| if (!match) { | ||
| return { kind: 'NOT_FOUND', message: `Package ${packageId} not found in search results` } | ||
| } | ||
| return match | ||
| } catch (err) { | ||
| const classified = classifyError(err) | ||
| if (classified) return classified | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| async function fetchRegistrationPage( | ||
| pageId: string, | ||
| maxAttempts = 2, | ||
| ): Promise<NuGetRegistrationPage> { | ||
| for (let attempt = 1; ; attempt++) { | ||
| try { | ||
| const resp = await axios.get<NuGetRegistrationPage>(pageId, { | ||
| headers: { 'Accept-Encoding': 'gzip' }, | ||
| timeout: 15000, | ||
| }) | ||
|
Comment on lines
+104
to
+107
|
||
| return resp.data | ||
| } catch (err) { | ||
| if (attempt >= maxAttempts) throw err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export async function fetchRegistration( | ||
| packageId: string, | ||
| ): Promise<NuGetRegistrationIndex | NuGetFetchError> { | ||
| const { registrationBaseUrl } = await resolveEndpoints() | ||
| const lowerId = packageId.toLowerCase() | ||
|
|
||
| try { | ||
| const resp = await axios.get<NuGetRegistrationIndex>( | ||
| `${registrationBaseUrl}${lowerId}/index.json`, | ||
| { | ||
| headers: { 'Accept-Encoding': 'gzip' }, | ||
| timeout: 15000, | ||
| }, | ||
|
mbani01 marked this conversation as resolved.
|
||
| ) | ||
|
Comment on lines
+122
to
+128
|
||
|
|
||
| const index = resp.data | ||
|
|
||
| for (let i = 0; i < index.items.length; i++) { | ||
| const page = index.items[i] | ||
| if (!page.items) { | ||
| const fullPage = await fetchRegistrationPage(page['@id']) | ||
| index.items[i] = { ...page, items: fullPage.items ?? [] } | ||
| } | ||
| } | ||
|
|
||
| return index | ||
| } catch (err) { | ||
| const classified = classifyError(err) | ||
| if (classified) return classified | ||
| throw err | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.