refactor(enabled-managers): implement custom.<customMgrName> syntax (#24079)

Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
RahulGautamSingh 2023-11-08 18:58:26 +05:45 committed by GitHub
parent f1fa4c1e3a
commit 79556f4ecb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 88 additions and 31 deletions

View file

@ -1170,6 +1170,16 @@ Example:
}
```
To enable custom managers you will need to add `custom.` prefix before their names
Example:
```json
{
"enabledManagers": ["custom.regex"]
}
```
For the full list of available managers, see the [Supported Managers](https://docs.renovatebot.com/modules/manager/#supported-managers) documentation.
## encrypted

View file

@ -1,13 +1,29 @@
import { EnabledManagersMigration } from './enabled-managers-migration';
describe('config/migrations/custom/enabled-managers-migration', () => {
it('should replace yarn with npm', () => {
it('migrates', () => {
expect(EnabledManagersMigration).toMigrate(
{
enabledManagers: ['test1', 'yarn', 'test2'],
enabledManagers: ['test1', 'yarn', 'test2', 'regex', 'custom.regex'],
},
{
enabledManagers: ['test1', 'npm', 'test2'],
enabledManagers: [
'test1',
'npm',
'test2',
'custom.regex',
'custom.regex',
],
},
);
// coverage
expect(EnabledManagersMigration).not.toMigrate(
{
enabledManagers: undefined,
},
{
enabledManagers: undefined,
},
);
});

View file

@ -5,11 +5,20 @@ export class EnabledManagersMigration extends AbstractMigration {
override readonly propertyName = 'enabledManagers';
override run(value: unknown): void {
if (is.array(value)) {
const newValue = value.map((manager) =>
manager === 'yarn' ? 'npm' : manager,
);
this.rewrite(newValue);
if (!is.array<string>(value, is.string)) {
return;
}
const newValue = value.map((manager) => {
switch (manager) {
case 'yarn':
return 'npm';
case 'regex':
return 'custom.regex';
default:
return manager;
}
});
this.rewrite(newValue);
}
}

View file

@ -236,7 +236,7 @@ describe('config/validation', () => {
[
'multiple enabled managers',
{
enabledManagers: ['npm', 'gradle', 'maven', 'regex'],
enabledManagers: ['npm', 'gradle', 'maven', 'custom.regex'],
},
],
])('validates enabled managers for %s', async (_case, config) => {

View file

@ -71,7 +71,7 @@ function validatePlainObject(val: Record<string, unknown>): true | string {
function getUnsupportedEnabledManagers(enabledManagers: string[]): string[] {
return enabledManagers.filter(
(manager) => !allManagersList.includes(manager),
(manager) => !allManagersList.includes(manager.replace('custom.', '')),
);
}

View file

@ -9,6 +9,7 @@ describe('modules/manager/custom/index', () => {
it('works', () => {
expect(customManager.isCustomManager('npm')).toBe(false);
expect(customManager.isCustomManager('regex')).toBe(true);
expect(customManager.isCustomManager('custom.regex')).toBe(false);
});
});
});

View file

@ -37,6 +37,16 @@ describe('modules/manager/index', () => {
});
});
describe('getEnabledManagersList()', () => {
it('works', () => {
expect(manager.getEnabledManagersList()).toEqual(manager.allManagersList);
expect(manager.getEnabledManagersList(['custom.regex', 'npm'])).toEqual([
'npm',
'regex',
]);
});
});
it('validates', () => {
function validate(module: ManagerApi, moduleName: string): boolean {
// no need to validate custom as it is a wrapper and not an actual manager

View file

@ -103,3 +103,22 @@ export function getRangeStrategy(config: RangeConfig): RangeStrategy | null {
return config.rangeStrategy;
}
/**
* Filter a list of managers based on enabled managers.
*
* If enabledManagers is provided, this function returns a subset of allManagersList
* that matches the enabled manager names, including custom managers. If enabledManagers
* is not provided or is an empty array, it returns the full list of managers.
*/
export function getEnabledManagersList(enabledManagers?: string[]): string[] {
if (enabledManagers?.length) {
return allManagersList.filter(
(manager) =>
enabledManagers.includes(manager) ||
enabledManagers.includes(`custom.${manager}`),
);
}
return allManagersList;
}

View file

@ -19,7 +19,7 @@ describe('workers/repository/extract/extract-fingerprint-config', () => {
notStable: 'http://some.link.2',
},
},
enabledManagers: ['npm', 'regex'],
enabledManagers: ['npm', 'custom.regex'],
customManagers: [
{
customType: 'regex',

View file

@ -1,6 +1,6 @@
import { getManagerConfig, mergeChildConfig } from '../../../config';
import type { RenovateConfig } from '../../../config/types';
import { allManagersList } from '../../../modules/manager';
import { getEnabledManagersList } from '../../../modules/manager';
import { isCustomManager } from '../../../modules/manager/custom';
import type { RegexManagerTemplates } from '../../../modules/manager/custom/regex/types';
import { validMatchFields } from '../../../modules/manager/custom/regex/utils';
@ -55,13 +55,7 @@ export function generateFingerprintConfig(
config: RenovateConfig,
): FingerprintExtractConfig {
const managerExtractConfigs: WorkerExtractConfig[] = [];
let managerList: Set<string>;
const { enabledManagers } = config;
if (enabledManagers?.length) {
managerList = new Set(enabledManagers);
} else {
managerList = new Set(allManagersList);
}
const managerList = new Set(getEnabledManagersList(config.enabledManagers));
for (const manager of managerList) {
const managerConfig = getManagerConfig(config, manager);

View file

@ -42,10 +42,13 @@ describe('workers/repository/extract/index', () => {
});
it('warns if no packages found for a enabled manager', async () => {
config.enabledManagers = ['npm'];
config.enabledManagers = ['npm', 'custom.regex'];
managerFiles.getManagerPackageFiles.mockResolvedValue([]);
expect((await extractAllDependencies(config)).packageFiles).toEqual({});
expect(logger.debug).toHaveBeenCalled();
expect(logger.debug).toHaveBeenCalledWith(
{ manager: 'custom.regex' },
`Manager explicitly enabled in "enabledManagers" config, but found no results. Possible config error?`,
);
});
it('warns if packageFiles is null', async () => {

View file

@ -2,7 +2,7 @@ import is from '@sindresorhus/is';
import { getManagerConfig, mergeChildConfig } from '../../../config';
import type { ManagerConfig, RenovateConfig } from '../../../config/types';
import { logger } from '../../../logger';
import { allManagersList, hashMap } from '../../../modules/manager';
import { getEnabledManagersList, hashMap } from '../../../modules/manager';
import { isCustomManager } from '../../../modules/manager/custom';
import { scm } from '../../../modules/platform/scm';
import type { ExtractResult, WorkerExtractConfig } from '../../types';
@ -13,14 +13,7 @@ import { processSupersedesManagers } from './supersedes';
export async function extractAllDependencies(
config: RenovateConfig,
): Promise<ExtractResult> {
let managerList = allManagersList;
const { enabledManagers } = config;
if (is.nonEmptyArray(enabledManagers)) {
logger.debug('Applying enabledManagers filtering');
managerList = managerList.filter((manager) =>
enabledManagers.includes(manager),
);
}
const managerList = getEnabledManagersList(config.enabledManagers);
const extractList: WorkerExtractConfig[] = [];
const fileList = await scm.getFileList();
@ -91,7 +84,9 @@ export async function extractAllDependencies(
// If not, log a warning to indicate possible misconfiguration.
if (is.nonEmptyArray(config.enabledManagers)) {
for (const enabledManager of config.enabledManagers) {
if (!(enabledManager in extractResult.packageFiles)) {
if (
!(enabledManager.replace('custom.', '') in extractResult.packageFiles)
) {
logger.debug(
{ manager: enabledManager },
`Manager explicitly enabled in "enabledManagers" config, but found no results. Possible config error?`,