Files
renovate/lib/modules/manager/gitlabci-include/extract.ts
T
Rhys Arkins dca3418bbd refactor: lib/modules (#14488)
Moves datasource, manager, platform and versioning code from lib/ into new lib/modules/

BREAKING CHANGE: External tools must update paths to datasource, manager, platform and versioning
2022-03-04 09:04:02 +01:00

67 lines
1.8 KiB
TypeScript

import is from '@sindresorhus/is';
import { load } from 'js-yaml';
import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
import { replaceReferenceTags } from '../gitlabci/utils';
import type { ExtractConfig, PackageDependency, PackageFile } from '../types';
function extractDepFromIncludeFile(includeObj: {
file: any;
project: string;
ref: string;
}): PackageDependency {
const dep: PackageDependency = {
datasource: GitlabTagsDatasource.id,
depName: includeObj.project,
depType: 'repository',
};
if (!includeObj.ref) {
dep.skipReason = 'unknown-version';
return dep;
}
dep.currentValue = includeObj.ref;
return dep;
}
export function extractPackageFile(
content: string,
_packageFile: string,
config: ExtractConfig
): PackageFile | null {
const deps: PackageDependency[] = [];
try {
// TODO: fix me (#9610)
const doc: any = load(replaceReferenceTags(content), {
json: true,
});
let includes;
if (doc?.include && is.array(doc.include)) {
includes = doc.include;
} else {
includes = [doc.include];
}
for (const includeObj of includes) {
if (includeObj?.file && includeObj.project) {
const dep = extractDepFromIncludeFile(includeObj);
if (config.endpoint) {
dep.registryUrls = [
config.endpoint.replace(regEx(/\/api\/v4\/?/), ''),
];
}
deps.push(dep);
}
}
} catch (err) /* istanbul ignore next */ {
if (err.stack?.startsWith('YAMLException:')) {
logger.debug({ err }, 'YAML exception extracting GitLab CI includes');
} else {
logger.warn({ err }, 'Error extracting GitLab CI includes');
}
}
if (!deps.length) {
return null;
}
return { deps };
}