Skip to content
Snippets Groups Projects
Unverified Commit e8c80fbe authored by Jonas Michaelis's avatar Jonas Michaelis Committed by GitHub
Browse files

feat(gitlabci-include): add support for 'include:local' (#6630)


* feat(gitlabci-include): add support for 'include:local'

* fix: lint

* fix: apply suggestions from code review

Co-authored-by: default avatarMichael Kriese <michael.kriese@visualon.de>

* fix: lint

* fix: includedDeps null check

Co-authored-by: default avatarMichael Kriese <michael.kriese@visualon.de>
parent fc41751f
No related branches found
Tags 21.20.0
No related merge requests found
include:
- local: 'lib/manager/gitlabci-include/__fixtures__/include.1.yml'
stages:
- test
include:
- local: 'lib/manager/gitlabci-include/__fixtures__/include.1.yml'
- local: 'lib/manager/gitlabci-include/__fixtures__/include.2.yml'
stages:
- test
test:
stage: test
image: alpine:3.11
script:
- echo test
test:
stage: test
image: node:12
script:
- echo test
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`lib/manager/gitlabci-include/extract extractPackageFile() extracts local include block 1`] = `
Array [
Object {
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
"currentDigest": undefined,
"currentValue": "3.11",
"datasource": "docker",
"depName": "alpine",
"depType": "image",
"replaceString": "alpine:3.11",
},
]
`;
exports[`lib/manager/gitlabci-include/extract extractPackageFile() extracts multiple include blocks 1`] = ` exports[`lib/manager/gitlabci-include/extract extractPackageFile() extracts multiple include blocks 1`] = `
Array [ Array [
Object { Object {
...@@ -22,3 +36,27 @@ Array [ ...@@ -22,3 +36,27 @@ Array [
}, },
] ]
`; `;
exports[`lib/manager/gitlabci-include/extract extractPackageFile() extracts multiple local include blocks 1`] = `
Array [
Object {
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
"currentDigest": undefined,
"currentValue": "3.11",
"datasource": "docker",
"depName": "alpine",
"depType": "image",
"replaceString": "alpine:3.11",
},
Object {
"autoReplaceStringTemplate": "{{depName}}{{#if newValue}}:{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}",
"commitMessageTopic": "Node.js",
"currentDigest": undefined,
"currentValue": "12",
"datasource": "docker",
"depName": "node",
"depType": "image",
"replaceString": "node:12",
},
]
`;
...@@ -2,33 +2,57 @@ import fs from 'fs'; ...@@ -2,33 +2,57 @@ import fs from 'fs';
import { extractPackageFile } from './extract'; import { extractPackageFile } from './extract';
const yamlFile = fs.readFileSync( const yamlFile = fs.readFileSync(
'lib/manager/gitlabci-include/__fixtures__/gitlab-ci.yaml', 'lib/manager/gitlabci-include/__fixtures__/gitlab-ci.1.yaml',
'utf8'
);
const yamlLocal = fs.readFileSync(
'lib/manager/gitlabci-include/__fixtures__/gitlab-ci.2.yaml',
'utf8'
);
const yamlLocalBlock = fs.readFileSync(
'lib/manager/gitlabci-include/__fixtures__/gitlab-ci.3.yaml',
'utf8' 'utf8'
); );
describe('lib/manager/gitlabci-include/extract', () => { describe('lib/manager/gitlabci-include/extract', () => {
describe('extractPackageFile()', () => { describe('extractPackageFile()', () => {
it('returns null for empty', () => { it('returns null for empty', async () => {
expect( expect(
extractPackageFile('nothing here', '.gitlab-ci.yml', {}) await extractPackageFile('nothing here', '.gitlab-ci.yml', {})
).toBeNull(); ).toBeNull();
}); });
it('extracts multiple include blocks', () => { it('extracts multiple include blocks', async () => {
const res = extractPackageFile(yamlFile, '.gitlab-ci.yml', {}); const res = await extractPackageFile(yamlFile, '.gitlab-ci.yml', {});
expect(res.deps).toMatchSnapshot(); expect(res.deps).toMatchSnapshot();
expect(res.deps).toHaveLength(3); expect(res.deps).toHaveLength(3);
}); });
it('normalizes configured endpoints', () => { it('extracts local include block', async () => {
const res = await extractPackageFile(yamlLocal, '.gitlab-ci.yml', {});
expect(res.deps).toMatchSnapshot();
expect(res.deps).toHaveLength(1);
});
it('extracts multiple local include blocks', async () => {
const res = await extractPackageFile(
yamlLocalBlock,
'.gitlab-ci.yml',
{}
);
expect(res.deps).toMatchSnapshot();
expect(res.deps).toHaveLength(2);
});
it('normalizes configured endpoints', async () => {
const endpoints = [ const endpoints = [
'http://gitlab.test/api/v4', 'http://gitlab.test/api/v4',
'http://gitlab.test/api/v4/', 'http://gitlab.test/api/v4/',
]; ];
endpoints.forEach((endpoint) => {
const res = extractPackageFile(yamlFile, '.gitlab-ci.yml', { for (const endpoint of endpoints) {
const res = await extractPackageFile(yamlFile, '.gitlab-ci.yml', {
endpoint, endpoint,
}); });
expect(res.deps[0].registryUrls[0]).toEqual('http://gitlab.test'); expect(res.deps[0].registryUrls[0]).toEqual('http://gitlab.test');
}); }
}); });
}); });
}); });
...@@ -3,16 +3,15 @@ import yaml from 'js-yaml'; ...@@ -3,16 +3,15 @@ import yaml from 'js-yaml';
import * as datasourceGitlabTags from '../../datasource/gitlab-tags'; import * as datasourceGitlabTags from '../../datasource/gitlab-tags';
import { logger } from '../../logger'; import { logger } from '../../logger';
import { SkipReason } from '../../types'; import { SkipReason } from '../../types';
import { readLocalFile } from '../../util/gitfs';
import { ExtractConfig, PackageDependency, PackageFile } from '../common'; import { ExtractConfig, PackageDependency, PackageFile } from '../common';
import * as gitlabci from '../gitlabci/extract';
function extractDepFromInclude(includeObj: { function extractDepFromIncludeFile(includeObj: {
file: any; file: any;
project: string; project: string;
ref: string; ref: string;
}): PackageDependency | null { }): PackageDependency {
if (!includeObj.file || !includeObj.project) {
return null;
}
const dep: PackageDependency = { const dep: PackageDependency = {
datasource: datasourceGitlabTags.id, datasource: datasourceGitlabTags.id,
depName: includeObj.project, depName: includeObj.project,
...@@ -26,22 +25,37 @@ function extractDepFromInclude(includeObj: { ...@@ -26,22 +25,37 @@ function extractDepFromInclude(includeObj: {
return dep; return dep;
} }
export function extractPackageFile( async function extractDepsFromIncludeLocal(includeObj: {
local: string;
}): Promise<PackageDependency[] | null> {
const content = await readLocalFile(includeObj.local, 'utf8');
const deps = gitlabci.extractPackageFile(content)?.deps;
return deps;
}
export async function extractPackageFile(
content: string, content: string,
_packageFile: string, _packageFile: string,
config: ExtractConfig config: ExtractConfig
): PackageFile | null { ): Promise<PackageFile | null> {
const deps: PackageDependency[] = []; const deps: PackageDependency[] = [];
try { try {
const doc = yaml.safeLoad(content, { json: true }); const doc = yaml.safeLoad(content, { json: true });
if (doc?.include && is.array(doc.include)) { if (doc?.include && is.array(doc.include)) {
for (const includeObj of doc.include) { for (const includeObj of doc.include) {
const dep = extractDepFromInclude(includeObj); if (includeObj.file && includeObj.project) {
if (dep) { const dep = extractDepFromIncludeFile(includeObj);
if (config.endpoint) { if (config.endpoint) {
dep.registryUrls = [config.endpoint.replace(/\/api\/v4\/?/, '')]; dep.registryUrls = [config.endpoint.replace(/\/api\/v4\/?/, '')];
} }
deps.push(dep); deps.push(dep);
} else if (includeObj.local) {
const includedDeps = await extractDepsFromIncludeLocal(includeObj);
if (includedDeps) {
for (const includedDep of includedDeps) {
deps.push(includedDep);
}
}
} }
} }
} }
...@@ -53,7 +67,6 @@ export function extractPackageFile( ...@@ -53,7 +67,6 @@ export function extractPackageFile(
logger.warn({ err }, 'Error extracting GitLab CI includes'); logger.warn({ err }, 'Error extracting GitLab CI includes');
} }
} }
if (!deps.length) { if (!deps.length) {
return null; return null;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment