Skip to content
Snippets Groups Projects
Unverified Commit b0cc6080 authored by Timo Furrer's avatar Timo Furrer Committed by GitHub
Browse files

Correctly support includes in gitlab-ci (#11839)

parent 867c0133
No related merge requests found
include:
project: mikebryant/include-source-example
file: /template.yaml
ref: 1.0.0
script:
- !reference [.setup, script]
- !reference [arbitrary job name with space and no starting dot, nested1, nested2, nested3]
include:
script:
- !reference [.setup, script]
- !reference [arbitrary job name with space and no starting dot, nested1, nested2, nested3]
......@@ -22,3 +22,14 @@ Array [
},
]
`;
exports[`manager/gitlabci-include/extract extractPackageFile() extracts single include block 1`] = `
Array [
Object {
"currentValue": "1.0.0",
"datasource": "gitlab-tags",
"depName": "mikebryant/include-source-example",
"depType": "repository",
},
]
`;
import { loadFixture } from '../../../test/util';
import { extractPackageFile } from './extract';
const yamlFile = loadFixture('gitlab-ci.1.yaml');
const yamlFileMultiConfig = loadFixture('gitlab-ci.1.yaml');
const yamlFileSingleConfig = loadFixture('gitlab-ci.2.yaml');
const yamlWithEmptyIncludeConfig = loadFixture('gitlab-ci.3.yaml');
describe('manager/gitlabci-include/extract', () => {
describe('extractPackageFile()', () => {
......@@ -10,8 +12,25 @@ describe('manager/gitlabci-include/extract', () => {
extractPackageFile('nothing here', '.gitlab-ci.yml', {})
).toBeNull();
});
it('returns null for include block without any actual includes', () => {
const res = extractPackageFile(
yamlWithEmptyIncludeConfig,
'.gitlab-ci.yml',
{}
);
expect(res).toBeNull();
});
it('extracts single include block', () => {
const res = extractPackageFile(
yamlFileSingleConfig,
'.gitlab-ci.yml',
{}
);
expect(res.deps).toMatchSnapshot();
expect(res.deps).toHaveLength(1);
});
it('extracts multiple include blocks', () => {
const res = extractPackageFile(yamlFile, '.gitlab-ci.yml', {});
const res = extractPackageFile(yamlFileMultiConfig, '.gitlab-ci.yml', {});
expect(res.deps).toMatchSnapshot();
expect(res.deps).toHaveLength(3);
});
......@@ -22,7 +41,7 @@ describe('manager/gitlabci-include/extract', () => {
];
for (const endpoint of endpoints) {
const res = extractPackageFile(yamlFile, '.gitlab-ci.yml', {
const res = extractPackageFile(yamlFileMultiConfig, '.gitlab-ci.yml', {
endpoint,
});
expect(res.deps[0].registryUrls[0]).toEqual('http://gitlab.test');
......
......@@ -35,8 +35,13 @@ export function extractPackageFile(
const doc: any = load(replaceReferenceTags(content), {
json: true,
});
let includes;
if (doc?.include && is.array(doc.include)) {
for (const includeObj of 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) {
......@@ -45,7 +50,6 @@ export function extractPackageFile(
deps.push(dep);
}
}
}
} catch (err) /* istanbul ignore next */ {
if (err.stack?.startsWith('YAMLException:')) {
logger.debug({ err }, 'YAML exception extracting GitLab CI includes');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment