Skip to content
Snippets Groups Projects
Commit caad338e authored by Rhys Arkins's avatar Rhys Arkins
Browse files

refactor(gitlabci-includes): use autoReplace

parent c273e684
No related merge requests found
import { extractPackageFile } from './extract';
import { updateDependency } from './update';
export { extractPackageFile, updateDependency };
export { extractPackageFile };
export const autoReplace = true;
export const defaultConfig = {
fileMatch: ['^\\.gitlab-ci\\.yml$'],
......
import { readFileSync } from 'fs';
import { updateDependency } from './update';
const yamlFile = readFileSync(
'lib/manager/gitlabci-include/__fixtures__/gitlab-ci.yaml',
'utf8'
);
describe('manager/gitlabci-include/update', () => {
describe('updateDependency', () => {
it('replaces existing value', () => {
const upgrade = {
depType: 'repository',
depName: 'mikebryant/include-source-example',
newValue: '1.0.1',
};
const res = updateDependency({ fileContent: yamlFile, upgrade });
expect(res).not.toEqual(yamlFile);
expect(res.includes(upgrade.newValue)).toBe(true);
});
it('returns same', () => {
const upgrade = {
depType: 'repository',
depName: 'mikebryant/include-source-example',
newValue: '1.0.0',
};
const res = updateDependency({ fileContent: yamlFile, upgrade });
expect(res).toEqual(yamlFile);
});
it('returns null if error', () => {
const res = updateDependency({ fileContent: null, upgrade: null });
expect(res).toBeNull();
});
});
});
import YAWN from 'yawn-yaml/cjs';
import { logger } from '../../logger';
import { UpdateDependencyConfig } from '../common';
export function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): string | null {
try {
const { depName, newValue } = upgrade;
const yawn = new YAWN(fileContent);
const doc = yawn.json;
for (const includeObj of doc.include) {
if (
includeObj.project &&
includeObj.ref &&
includeObj.project === depName
) {
includeObj.ref = newValue;
}
}
yawn.json = doc;
return yawn.yaml;
} catch (err) {
logger.debug({ err }, 'Error setting new .gitlab-ci.yml include value');
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