Skip to content

Commit

Permalink
feat(manager/gitlab-include): support multi-document package files (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fgreinacher authored Sep 30, 2024
1 parent 2a1a9fb commit 980677e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
34 changes: 34 additions & 0 deletions lib/modules/manager/gitlabci-include/extract.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { codeBlock } from 'common-tags';
import { Fixtures } from '../../../../test/fixtures';
import { GlobalConfig } from '../../../config/global';
import { extractPackageFile } from '.';
Expand Down Expand Up @@ -68,5 +69,38 @@ describe('modules/manager/gitlabci-include/extract', () => {
expect(res?.deps[0].registryUrls).toEqual(['http://gitlab.test']);
}
});

it('supports multi-document files', () => {
const multiDocFile = codeBlock`
other:
content: to be ignored
---
include:
- project: mikebryant/include-source-example
ref: 1.0.0
---
include:
- project: mikebryant/include-source-example2
ref: 2.0.0
---
more:
content: to be ignored
`;
const res = extractPackageFile(multiDocFile);
expect(res).toMatchObject({
deps: [
{
currentValue: '1.0.0',
datasource: 'gitlab-tags',
depName: 'mikebryant/include-source-example',
},
{
currentValue: '2.0.0',
datasource: 'gitlab-tags',
depName: 'mikebryant/include-source-example2',
},
],
});
});
});
});
26 changes: 18 additions & 8 deletions lib/modules/manager/gitlabci-include/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import is from '@sindresorhus/is';
import { GlobalConfig } from '../../../config/global';
import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { parseSingleYaml } from '../../../util/yaml';
import { parseYaml } from '../../../util/yaml';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
import {
filterIncludeFromGitlabPipeline,
Expand Down Expand Up @@ -70,16 +70,26 @@ export function extractPackageFile(
const deps: PackageDependency[] = [];
const platform = GlobalConfig.get('platform');
const endpoint = GlobalConfig.get('endpoint');
const registryUrls =
platform === 'gitlab' && endpoint
? [endpoint.replace(regEx(/\/api\/v4\/?/), '')]
: null;
try {
// TODO: use schema (#9610)
const doc = parseSingleYaml<GitlabPipeline>(replaceReferenceTags(content));
const includes = getAllIncludeProjects(doc);
for (const includeObj of includes) {
const dep = extractDepFromIncludeFile(includeObj);
if (platform === 'gitlab' && endpoint) {
dep.registryUrls = [endpoint.replace(regEx(/\/api\/v4\/?/), '')];
const docs = parseYaml<GitlabPipeline>(replaceReferenceTags(content), {
uniqueKeys: false,
});
for (const doc of docs) {
if (is.object(doc)) {
const includes = getAllIncludeProjects(doc);
for (const includeObj of includes) {
const dep = extractDepFromIncludeFile(includeObj);
if (registryUrls) {
dep.registryUrls = registryUrls;
}
deps.push(dep);
}
}
deps.push(dep);
}
} catch (err) /* istanbul ignore next */ {
if (err.stack?.startsWith('YAMLException:')) {
Expand Down

0 comments on commit 980677e

Please sign in to comment.