Files
renovate/lib/modules/manager/gitlabci/utils.spec.ts
Marius b22f58045e fix(manager): Handle variables with default values in registry aliases (#36293)
* fix(manager): handle registryAliases with shell variable defaults

Allows `registryAliases` keys to contain shell-style variables with
empty (or other) defaults, e.g. `${DEPENDENCY_PROXY:-}` or
`${CI_REGISTRY:-}`. The same image string is then resolved both
online (variable expanded) and offline (variable empty).

Changes in `dockerfile/extract.ts` (used by `gitlabci` too):
* Match the alias key as either `${name}/...` (existing) or `${name}...`
  when `name` already ends in a non-identifier boundary character such
  as `}`. Bare-identifier keys like `$CI_REGISTRY` continue to require
  the slash boundary so they no longer overmatch keys that begin with
  the same prefix (e.g. `$CI_REGISTRY` vs `$CI_REGISTRY_IMAGE`).
* Use `ensureTrailingSlash` to handle alias values that already include
  a trailing slash.
* Preserve the original `name` and path structure in `dep.depName`
  instead of substringing only after the alias `value`. This keeps the
  variable in the depName so it survives a `splitImageParts` round-trip
  and the resulting branch name comes out clean.

Adds `sanitizeDepName` regex passes in
`lib/workers/repository/updates/flatten.ts` so any leftover
`${VAR}`/`${VAR:-...}` placeholder is stripped from the depName before
it becomes a branch name (otherwise we'd get
`renovate/${dependency_proxy}python-3.x` instead of
`renovate/python-3.x`).

Tests cover:
* `${VAR:-}/image` (with slash) and `${VAR:-}image` (no slash, requires
  the boundary form).
* Overlapping prefixes: `$CI_REGISTRY` vs `$CI_REGISTRY_IMAGE`.
* Registry alias with port (`localhost:5000/repo`).
* `sanitizeDepName` strips `${VAR}` / `${VAR:-}` / bare `$VAR`.

Reproducer: https://github.com/Wayneoween/renovate-36293-repro

* revert(manager/gitlabci): keep regex group name as 'prefix'

Reverting the unrelated `prefix` -> `rawPrefix` rename in `depProxyRe`.
Per review on #36293, this rename is out of scope for the registry-alias
fix and just adds review surface.

* refactor(manager/dockerfile): narrow alias boundary check to '}'

Per review on #36293. The previous regex `/[^A-Za-z0-9_]$/` matched any
non-word ending, which is broader than the documented case (alias keys
ending in `}` from `${VAR}` / `${VAR:-...}`). Replace with a plain
`name.endsWith('}')` check, which also avoids re-building a regex on
every iteration of the alias loop.

Existing tests (`'variables with default and slash'`,
`'variables with default no slash'`, `'alias with digest only'`)
continue to exercise the `}`-boundary path; the `'overlapping prefixes'`
case continues to exercise the slash-only path.

* chore(manager/dockerfile): trim comment and TODO library/ strip edge

Per review on #36293:
* Drop the verbose explanation block above the depName-restore branch;
  the spec rows already document the expected shape.
* Add a TODO above the `dep.depName?.startsWith(valueWithSlash)` guard
  noting a pre-existing edge case: when the inner `getDep` strips a
  `library/` prefix (or similar) the depName no longer starts with
  `valueWithSlash`, so the alias-rooted depName is not restored. Out of
  scope for this PR.

* fix(manager/dockerfile): handle empty registry alias value

- strip trailing slash after ${VAR} placeholders in sanitizeDepName
- treat an empty registry alias value as Docker Hub instead of '/'
- clarify registry alias docs wording

* refactor(manager/dockerfile): drop redundant alias boundary guard

- `matchedAtBoundary` is only read behind `!matchedWithSlash`
- add docker-compose test for `${VAR:-}` registry aliases

* test(manager/dockerfile): pin special-prefix registry alias behavior

`getDep` strips `library/`, `amd64/` and `arm64/` as special prefixes, so
an alias whose value is one of those loses its alias-rooted `depName`.
Both the slash form and the variable form behave this way on `main`.
2026-09-03 12:43:47 +00:00

89 lines
6.6 KiB
TypeScript

import type { PackageDependency } from '../types.ts';
import { getGitlabDep } from './utils.ts';
describe('modules/manager/gitlabci/utils', () => {
describe('getGitlabDep', () => {
const versionAndDigestTemplate =
':{{#if newValue}}{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}';
const defaultAutoReplaceStringTemplate =
'{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}';
it.each`
name | imagePrefix
${'no variable'} | ${''}
${'group proxy'} | ${'$CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX/'}
${'group proxy with brackets'} | ${'${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/'}
${'direct group proxy'} | ${'$CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX/'}
`('offical image - $name', ({ imagePrefix }: { imagePrefix: string }) => {
const imageName = `${imagePrefix}mariadb:10.4.11`;
expect(getGitlabDep(imageName)).toMatchObject({
autoReplaceStringTemplate:
imagePrefix + defaultAutoReplaceStringTemplate,
replaceString: imageName,
depName: 'mariadb',
currentValue: '10.4.11',
});
});
it.each`
name | imagePrefix
${'no variable'} | ${''}
${'group proxy'} | ${'$CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX/'}
${'group proxy with brackets'} | ${'${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/'}
${'direct group proxy'} | ${'$CI_DEPENDENCY_PROXY_DIRECT_GROUP_IMAGE_PREFIX/'}
`(
'image with organization - $name',
({ imagePrefix }: { imagePrefix: string }) => {
const imageName = `${imagePrefix}renovate/renovate:19.70.8-slim`;
expect(getGitlabDep(imageName)).toMatchObject({
autoReplaceStringTemplate:
imagePrefix + defaultAutoReplaceStringTemplate,
replaceString: imageName,
depName: 'renovate/renovate',
currentValue: '19.70.8-slim',
});
},
);
it.each`
name | registryAliases | imageName | dep
${'multiple aliases'} | ${{ foo: 'foo.registry.com', bar: 'bar.registry.com' }} | ${'foo/image:1.0'} | ${{ depName: 'foo/image', packageName: 'foo.registry.com/image', currentValue: '1.0', autoReplaceStringTemplate: `foo/image${versionAndDigestTemplate}` }}
${'aliased variable'} | ${{ $CI_REGISTRY: 'registry.com' }} | ${'$CI_REGISTRY/image:1.0'} | ${{ depName: '$CI_REGISTRY/image', packageName: 'registry.com/image', currentValue: '1.0', autoReplaceStringTemplate: `$CI_REGISTRY/image${versionAndDigestTemplate}` }}
${'overlapping prefixes'} | ${{ $CI_REGISTRY: 'registry.example.com', $CI_REGISTRY_IMAGE: 'registry.example.com/project' }} | ${'$CI_REGISTRY_IMAGE/image:1.0'} | ${{ depName: '$CI_REGISTRY_IMAGE/image', packageName: 'registry.example.com/project/image', currentValue: '1.0', autoReplaceStringTemplate: `$CI_REGISTRY_IMAGE/image${versionAndDigestTemplate}` }}
${'variables with brackets'} | ${{ '${CI_REGISTRY}': 'registry.com' }} | ${'${CI_REGISTRY}/image:1.0'} | ${{ depName: '${CI_REGISTRY}/image', packageName: 'registry.com/image', currentValue: '1.0', autoReplaceStringTemplate: `$\{CI_REGISTRY}/image${versionAndDigestTemplate}` }}
${'variables with default and slash'} | ${{ '${CI_REGISTRY:-}': 'registry.com' }} | ${'${CI_REGISTRY:-}/image:1.0'} | ${{ depName: '${CI_REGISTRY:-}/image', packageName: 'registry.com/image', currentValue: '1.0', autoReplaceStringTemplate: `$\{CI_REGISTRY:-}/image${versionAndDigestTemplate}` }}
${'variables with default no slash'} | ${{ '${CI_REGISTRY:-}': 'registry.com/' }} | ${'${CI_REGISTRY:-}image:1.0'} | ${{ depName: '${CI_REGISTRY:-}image', packageName: 'registry.com/image', currentValue: '1.0', autoReplaceStringTemplate: `$\{CI_REGISTRY:-}image${versionAndDigestTemplate}` }}
${'alias with digest only'} | ${{ '${CI_REGISTRY:-}': 'registry.com' }} | ${'${CI_REGISTRY:-}image@sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789'} | ${{ depName: '${CI_REGISTRY:-}image', packageName: 'registry.com/image', currentDigest: 'sha256:abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789', autoReplaceStringTemplate: `$\{CI_REGISTRY:-}image@{{#if newDigest}}{{newDigest}}{{/if}}` }}
${'not aliased variable'} | ${{}} | ${'$CI_REGISTRY/image:1.0'} | ${{ autoReplaceStringTemplate: defaultAutoReplaceStringTemplate }}
${'plain image'} | ${{}} | ${'registry.com/image:1.0'} | ${{ depName: 'registry.com/image', currentValue: '1.0', autoReplaceStringTemplate: defaultAutoReplaceStringTemplate }}
`(
'supports registry aliases - $name',
({
registryAliases,
imageName,
dep,
}: {
registryAliases: Record<string, string>;
imageName: string;
dep: PackageDependency;
}) => {
expect(getGitlabDep(imageName, registryAliases)).toMatchObject({
...dep,
replaceString: imageName,
});
},
);
it('no Docker hub', () => {
expect(
getGitlabDep('quay.io/prometheus/node-exporter:v1.3.1'),
).toMatchObject({
autoReplaceStringTemplate: defaultAutoReplaceStringTemplate,
replaceString: 'quay.io/prometheus/node-exporter:v1.3.1',
depName: 'quay.io/prometheus/node-exporter',
currentValue: 'v1.3.1',
});
});
});
});