Skip to content

Commit

Permalink
refactor(enabled-managers): implement custom.<customMgrName> syntax (
Browse files Browse the repository at this point in the history
…#24079)

Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
3 people committed Nov 8, 2023
1 parent f1fa4c1 commit 79556f4
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 31 deletions.
10 changes: 10 additions & 0 deletions docs/usage/configuration-options.md
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions lib/config/migrations/custom/enabled-managers-migration.spec.ts
@@ -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,
},
);
});
Expand Down
19 changes: 14 additions & 5 deletions lib/config/migrations/custom/enabled-managers-migration.ts
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion lib/config/validation.spec.ts
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/config/validation.ts
Expand Up @@ -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.', '')),
);
}

Expand Down
1 change: 1 addition & 0 deletions lib/modules/manager/custom/index.spec.ts
Expand Up @@ -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);
});
});
});
10 changes: 10 additions & 0 deletions lib/modules/manager/index.spec.ts
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/modules/manager/index.ts
Expand Up @@ -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;
}
Expand Up @@ -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',
Expand Down
10 changes: 2 additions & 8 deletions lib/workers/repository/extract/extract-fingerprint-config.ts
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions lib/workers/repository/extract/index.spec.ts
Expand Up @@ -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 () => {
Expand Down
15 changes: 5 additions & 10 deletions lib/workers/repository/extract/index.ts
Expand Up @@ -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';
Expand All @@ -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();

Expand Down Expand Up @@ -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?`,
Expand Down

0 comments on commit 79556f4

Please sign in to comment.