diff --git a/lib/modules/manager/pep621/processors/uv.ts b/lib/modules/manager/pep621/processors/uv.ts index e59ac557dc1f3c..044e4163115ec6 100644 --- a/lib/modules/manager/pep621/processors/uv.ts +++ b/lib/modules/manager/pep621/processors/uv.ts @@ -1,4 +1,6 @@ import is from '@sindresorhus/is'; +import { PypiDatasource } from '../../../datasource/pypi'; +import { find } from '../../../../util/host-rules'; import { quote } from 'shlex'; import { TEMPORARY_ERROR } from '../../../../constants/error-messages'; import { logger } from '../../../../logger'; @@ -15,6 +17,7 @@ import type { import { type PyProject, UvLockfileSchema } from '../schema'; import { depTypes, parseDependencyList } from '../utils'; import type { PyProjectProcessor } from './types'; +import { HostRule } from '../../../../types'; const uvUpdateCMD = 'uv lock'; @@ -115,8 +118,12 @@ export class UvProcessor implements PyProjectProcessor { constraint: config.constraints?.uv, }; + const extraEnv = { + ...getUvExtraIndexUrl(updateArtifact.updatedDeps), + }; const execOptions: ExecOptions = { cwdFile: packageFileName, + extraEnv, docker: {}, userConfiguredEnv: config.env, toolConstraints: [pythonConstraint, uvConstraint], @@ -191,3 +198,31 @@ function generateCMD(updatedDeps: Upgrade[]): string { return `${uvUpdateCMD} ${deps.map((dep) => `--upgrade-package ${quote(dep)}`).join(' ')}`; } + +function getMatchingHostRule(url: string | undefined): HostRule { + const scopedMatch = find({ hostType: PypiDatasource.id, url }); + return is.nonEmptyObject(scopedMatch) ? scopedMatch : find({ url }); +} + +function getUvExtraIndexUrl(updatedDeps: Upgrade[]): NodeJS.ProcessEnv { + const registryUrls = updatedDeps.map((dep) => dep.registryUrls).flat(); + const extraIndexUrls: string[] = []; + + for (const registryUrl of registryUrls) { + const parsedUrl = new URL(registryUrl ?? ''); + + const rule = getMatchingHostRule(parsedUrl.toString()); + if (rule.username) { + parsedUrl.username = rule.username; + } + if (rule.password) { + parsedUrl.password = rule.password; + } + + extraIndexUrls.push(parsedUrl.toString()); + } + + return { + UV_EXTRA_INDEX_URL: extraIndexUrls.join(' '), + }; +}