Files
renovate/lib/modules/manager/gitlabci/extract.ts
Michael Kriese 55a7341be2 test(manager): close long-tail branch coverage gaps (#45954)
* test(manager): close azure-pipelines, bundler and cocoapods gaps

Adds a pipeline whose step task is not versioned, so nothing is
extracted, and a gem lock line carrying no version, which is skipped.

Marks four guards that cannot be false: `extractContainer()` always
returns a dep, `getLockFilePath()` always returns a path, and the
cocoapods dep name, spec name and git account all derive from values
their enclosing check already proved present.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(manager): close cargo, composer and cpanfile gaps

Adds a composer repository map that disables something other than
packagist, which is dropped rather than recorded.

Marks five guards: the cargo warning only fires for a crate dep and its
retry only runs when a dep was already current, a gitlab rule without a
token never passes the auth check, and both cpanfile handlers run only
once their match produced a value.

Two implicit elses stay counted, in the cargo version resolver and the
composer repository mapper, since a coverage-ignore hint does not attach
to an `else if`.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(manager): close deps-edn, docker-compose and dockerfile gaps

Adds three extractions: a deps.edn alias whose value is not a map, a
compose extension carrying no image, and a dockerfile escape directive
naming a character other than a backtick, which leaves the default in
place.

Marks the deps-edn repository name check, since object keys are always
strings, and notes the repository spec chain, where a coverage-ignore
hint does not attach to an `else if`.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(manager/flux): close the flux coverage gaps

Adds two GitRepository resources whose url is ssh rather than http, one
pinned to a tag and one to a commit. Neither yields a source url, since
only http urls can be turned into one.

Marks the Kustomization image guard: the schema rejects an image without
a name, so `extractImage()` cannot return null there.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(manager): mark github-actions, gitlabci and glasskube guards

None of these six needs a test. A parsed action ref is either a docker
or a repository ref, `getDep()` always returns a dep for a job container
and a service, and the remaining three need a listed file that cannot be
read, a listed file that yields no deps, or a manifest whose
repositories match no name.

The glasskube resource kind chain stays counted, since a coverage-ignore
hint does not attach to an `else if`.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

* test(manager): mark gomod and gradle-wrapper guards

Four guards that need setups no test provides: a removed dep that is not
added back, a go.mod the replace massaging leaves unchanged, and a
daemon-jvm file carrying no toolchain version. A parsed go directive
always yields a version.

The additional-dependency count chain stays counted, since a
coverage-ignore hint does not attach to an `else if`.

Co-Authored-By: Claude Opus 5 <michael.kriese+claude-code@mend.io>

---------

Co-authored-by: Claude Opus 5 <michael.kriese+claude-code@mend.io>
2026-09-15 07:10:10 +00:00

184 lines
4.9 KiB
TypeScript

import { logger } from '../../../logger/index.ts';
import { readLocalFile } from '../../../util/fs/index.ts';
import { regEx } from '../../../util/regex.ts';
import { Result } from '../../../util/result.ts';
import { parseYaml } from '../../../util/yaml.ts';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags/index.ts';
import { id as semverPartialId } from '../../versioning/semver-partial/index.ts';
import type {
ExtractConfig,
PackageDependency,
PackageFile,
PackageFileContent,
} from '../types.ts';
import {
GitlabDocument,
Job,
Jobs,
MultiDocumentLocalIncludes,
} from './schema.ts';
import { getGitlabDep } from './utils.ts';
// See https://docs.gitlab.com/ee/ci/components/index.html#use-a-component
const componentReferenceRegex = regEx(
/(?<fqdn>[^/]+)\/(?<projectPath>.+)\/(?:.+)@(?<specificVersion>.+)/,
);
function extractDepFromIncludeComponent(
component: string,
registryAliases?: Record<string, string>,
): PackageDependency | null {
let componentUrl = component;
if (registryAliases) {
for (const key in registryAliases) {
componentUrl = componentUrl.replace(key, registryAliases[key]);
}
}
const componentReference = componentReferenceRegex.exec(componentUrl)?.groups;
if (!componentReference) {
logger.debug(
{ componentReference: componentUrl },
'Ignoring malformed component reference',
);
return null;
}
const projectPathParts = componentReference.projectPath.split('/');
if (projectPathParts.length < 2) {
logger.debug(
{ componentReference: componentUrl },
'Ignoring component reference with incomplete project path',
);
return null;
}
const dep: PackageDependency = {
datasource: GitlabTagsDatasource.id,
depName: componentReference.projectPath,
depType: 'repository',
currentValue: componentReference.specificVersion,
registryUrls: [`https://${componentReference.fqdn}`],
versioning: semverPartialId,
};
return dep;
}
export function extractPackageFile(
content: string,
packageFile: string,
config: ExtractConfig,
): PackageFileContent | null {
const deps: PackageDependency[] = [];
try {
const docs = parseYaml(content, { uniqueKeys: false });
for (const doc of docs) {
const topLevel = Job.parse(doc);
const jobs = Jobs.parse(doc);
for (const job of [topLevel, ...jobs]) {
const { image, services } = job;
if (image) {
const dep = getGitlabDep(image.value, config.registryAliases);
dep.depType = image.type;
deps.push(dep);
}
for (const service of services) {
const dep = getGitlabDep(service, config.registryAliases);
dep.depType = 'service-image';
deps.push(dep);
}
}
const includedComponents = GitlabDocument.parse(doc);
for (const includedComponent of includedComponents) {
const dep = extractDepFromIncludeComponent(
includedComponent,
config.registryAliases,
);
if (dep) {
deps.push(dep);
}
}
}
} catch (err) /* istanbul ignore next */ {
if (err.stack?.startsWith('YAMLException:')) {
logger.debug(
{ err, packageFile },
'YAML exception extracting GitLab CI includes',
);
} else {
logger.debug(
{ err, packageFile },
'Error extracting GitLab CI dependencies',
);
}
}
return deps.length ? { deps } : null;
}
export async function extractAllPackageFiles(
config: ExtractConfig,
packageFiles: string[],
): Promise<PackageFile[] | null> {
const filesToExamine = [...packageFiles];
const seen = new Set<string>(packageFiles);
const results: PackageFile[] = [];
// extract all includes from the files
while (filesToExamine.length > 0) {
const file = filesToExamine.pop()!;
const content = await readLocalFile(file, 'utf8');
if (!content) {
logger.debug(
{ packageFile: file },
`Empty or non existent gitlabci file`,
);
continue;
}
const { val: docs, err } = Result.wrap(() =>
parseYaml(content, { uniqueKeys: false }),
).unwrap();
if (err) {
logger.debug(
{ err, packageFile: file },
'Error extracting GitLab CI dependencies',
);
continue;
}
const localIncludes = MultiDocumentLocalIncludes.parse(docs);
for (const file of localIncludes) {
if (!seen.has(file)) {
seen.add(file);
filesToExamine.push(file);
}
}
const result = extractPackageFile(content, file, config);
// v8 ignore else -- needs a listed file that yields no deps
if (result !== null) {
results.push({
packageFile: file,
deps: result.deps,
});
}
}
logger.trace(
{ packageFiles, files: filesToExamine.entries() },
'extracted all GitLab CI files',
);
if (!results.length) {
return null;
}
return results;
}