Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(manager): move some logic from gitlabci-include to gitlabci manager #26722

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 0 additions & 61 deletions lib/modules/manager/gitlabci-include/common.spec.ts

This file was deleted.

34 changes: 0 additions & 34 deletions lib/modules/manager/gitlabci-include/common.ts

This file was deleted.

10 changes: 5 additions & 5 deletions lib/modules/manager/gitlabci-include/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { logger } from '../../../logger';
import { regEx } from '../../../util/regex';
import { parseSingleYaml } from '../../../util/yaml';
import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
import {
filterIncludeFromGitlabPipeline,
isGitlabIncludeProject,
isNonEmptyObject,
} from '../gitlabci/common';
import type {
GitlabInclude,
GitlabIncludeProject,
GitlabPipeline,
} from '../gitlabci/types';
import { replaceReferenceTags } from '../gitlabci/utils';
import type { PackageDependency, PackageFileContent } from '../types';
import {
filterIncludeFromGitlabPipeline,
isGitlabIncludeProject,
isNonEmptyObject,
} from './common';

function extractDepFromIncludeFile(
includeObj: GitlabIncludeProject,
Expand Down
14 changes: 14 additions & 0 deletions lib/modules/manager/gitlabci/__fixtures__/include.2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

fgreinacher marked this conversation as resolved.
Show resolved Hide resolved
include:
- project: mikebryant/include-source-example
file: /template.yaml
ref: 1.0.0
- project: mikebryant/include-source-example2
file: /template.yaml
ref: master
- {"project":"mikebryant/include-source-example3", "file": "/template.yaml",}
- {}

script:
- !reference [.setup, script]
- !reference [arbitrary job name with space and no starting dot, nested1, nested2, nested3]
47 changes: 46 additions & 1 deletion lib/modules/manager/gitlabci/common.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
import { isGitlabIncludeLocal } from './common';
import { Fixtures } from '../../../../test/fixtures';
import { parseSingleYaml } from '../../../util/yaml';
import type { GitlabPipeline } from '../gitlabci/types';
import { replaceReferenceTags } from '../gitlabci/utils';
import {
filterIncludeFromGitlabPipeline,
isGitlabIncludeLocal,
isGitlabIncludeProject,
isNonEmptyObject,
} from './common';

const yamlFileMultiConfig = Fixtures.get('include.2.yaml');
// TODO: use schema (#9610)
const pipeline = parseSingleYaml<GitlabPipeline>(
replaceReferenceTags(yamlFileMultiConfig),
);
const includeLocal = { local: 'something' };
const includeProject = { project: 'something' };

describe('modules/manager/gitlabci/common', () => {
describe('filterIncludeFromGitlabPipeline()', () => {
it('returns GitlabPipeline without top level include key', () => {
expect(pipeline).toHaveProperty('include');
const filtered_pipeline = filterIncludeFromGitlabPipeline(pipeline);
expect(filtered_pipeline).not.toHaveProperty('include');
expect(filtered_pipeline).toEqual({
script: [null, null],
});
});
});

describe('isGitlabIncludeLocal()', () => {
it('returns true if GitlabInclude is GitlabIncludeLocal', () => {
expect(isGitlabIncludeLocal(includeLocal)).toBe(true);
Expand All @@ -13,4 +38,24 @@ describe('modules/manager/gitlabci/common', () => {
expect(isGitlabIncludeLocal(includeProject)).toBe(false);
});
});

describe('isGitlabIncludeProject()', () => {
it('returns true if GitlabInclude is GitlabIncludeProject', () => {
expect(isGitlabIncludeProject(includeProject)).toBe(true);
});

it('returns false if GitlabInclude is not GitlabIncludeProject', () => {
expect(isGitlabIncludeProject(includeLocal)).toBe(false);
});
});

describe('isNonEmptyObject()', () => {
it('returns true if not empty', () => {
expect(isNonEmptyObject({ attribute1: 1 })).toBe(true);
});

it('returns false if empty', () => {
expect(isNonEmptyObject({})).toBe(false);
});
});
});
28 changes: 27 additions & 1 deletion lib/modules/manager/gitlabci/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import is from '@sindresorhus/is';
import type { GitlabInclude, GitlabIncludeLocal } from '../gitlabci/types';
import type {
GitlabInclude,
GitlabIncludeLocal,
GitlabIncludeProject,
GitlabPipeline,
} from '../gitlabci/types';

export function isNonEmptyObject(obj: any): boolean {
return is.object(obj) && Object.keys(obj).length !== 0;
}

export function filterIncludeFromGitlabPipeline(
pipeline: GitlabPipeline,
): GitlabPipeline {
const pipeline_without_include: GitlabPipeline = {};
for (const key of Object.keys(pipeline).filter((key) => key !== 'include')) {
const pipeline_key = key as keyof typeof pipeline;
pipeline_without_include[pipeline_key] = pipeline[pipeline_key];
}
return pipeline_without_include;
}

export function isGitlabIncludeProject(
include: GitlabInclude,
): include is GitlabIncludeProject {
return !is.undefined((include as GitlabIncludeProject).project);
}

export function isGitlabIncludeLocal(
include: GitlabInclude,
Expand Down