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

feat(config)!: parse JSON5/YAML self-hosted admin config #12644

Merged
merged 20 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ec32776
feat: parse JSON5/YAML self-hosted admin config
nejch Nov 14, 2021
26b6f26
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 14, 2021
9f6d819
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 15, 2021
6f9f968
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 17, 2021
42acf83
chore(config): apply review suggestions
nejch Nov 17, 2021
0470ba6
chore(config): add missing await
nejch Nov 17, 2021
1ccc4d6
test(config): test resolving and deprecating empty extension
nejch Nov 17, 2021
88fc824
chore(config): fail explicitly on invalid file type
nejch Nov 18, 2021
c143dc0
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 18, 2021
3304295
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 18, 2021
20a35aa
feat(config): explicitly fail if no config file extension
nejch Nov 27, 2021
f1d0d0d
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 27, 2021
970fb27
Merge branch 'main' into feat/config-self-hosted-yaml-json5
nejch Nov 28, 2021
696ab04
Update docs/development/configuration.md
rarkins Dec 8, 2021
b3a7fab
Merge branch 'main' into feat/config-self-hosted-yaml-json5
rarkins Dec 8, 2021
ab5357a
chore(config): fix fs import
nejch Dec 8, 2021
2638f36
Merge branch 'main' into feat/config-self-hosted-yaml-json5
rarkins Dec 9, 2021
5542d5e
lint
rarkins Dec 9, 2021
f5a5cd5
Apply suggestions from code review
viceice Dec 9, 2021
70d773e
Merge branch 'main' into feat/config-self-hosted-yaml-json5
rarkins Dec 9, 2021
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
3 changes: 3 additions & 0 deletions docs/development/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Options which have `"globalOnly": true` are reserved only for bot global configu
You can override default configuration using a configuration file, with default name `config.js` in the working directory.
If you need an alternate location or name, set it in the environment variable `RENOVATE_CONFIG_FILE`.

**Note:** `RENOVATE_CONFIG_FILE` must be provided with an explicit file extension.
rarkins marked this conversation as resolved.
Show resolved Hide resolved
If none is provided, or the file type is invalid, Renovate will fail.

Using a configuration file gives you very granular configuration options.
For instance, you can override most settings at the global (file), repository, or package level.
e.g. apply one set of labels for `backend/package.json` and a different set of labels for `frontend/package.json` in the same repository.
Expand Down
19 changes: 7 additions & 12 deletions lib/workers/global/config/parse/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,7 @@ describe('workers/global/config/parse/file', () => {
});

describe('.getConfig()', () => {
it('raises deprecation warning when no extension given', async () => {
const configFile = upath.resolve(__dirname, './__fixtures__/file');
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
expect(logger.info).toHaveBeenCalledWith(file.fileDeprecationMessage);
});

it.each([
['custom config file without extension', 'file'],
['custom config file with extension', 'file.js'],
['JSON5 config file', 'config.json5'],
['YAML config file', 'config.yaml'],
Expand Down Expand Up @@ -88,17 +81,19 @@ describe('workers/global/config/parse/file', () => {

expect(mockProcessExit).toHaveBeenCalledWith(1);
});
it('fatal error and exit if invalid config file type', async () => {

it.each([
['invalid config file type', './file.txt'],
['missing config file type', './file'],
])('fatal error and exit if %s', async (fileType, filePath) => {
const mockProcessExit = jest
.spyOn(process, 'exit')
.mockImplementation(() => undefined as never);
viceice marked this conversation as resolved.
Show resolved Hide resolved
const configFile = upath.resolve(tmp.path, './file.txt');
const configFile = upath.resolve(tmp.path, filePath);
fs.writeFileSync(configFile, `{"token": "abc"}`, { encoding: 'utf8' });
await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
expect(mockProcessExit).toHaveBeenCalledWith(1);
expect(logger.fatal).toHaveBeenCalledWith(
`Unsupported file type: ${configFile}`
);
expect(logger.fatal).toHaveBeenCalledWith('Unsupported file type');
mockProcessExit.mockRestore();
viceice marked this conversation as resolved.
Show resolved Hide resolved
fs.unlinkSync(configFile);
});
Expand Down
19 changes: 3 additions & 16 deletions lib/workers/global/config/parse/file.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import fs, { readFile } from 'fs-extra';
import { readFile } from 'fs-extra';
nejch marked this conversation as resolved.
Show resolved Hide resolved
import { load } from 'js-yaml';
import JSON5 from 'json5';
import upath from 'upath';
import { migrateConfig } from '../../../../config/migration';
import type { AllConfig, RenovateConfig } from '../../../../config/types';
import { logger } from '../../../../logger';

export const fileDeprecationMessage =
'Providing a config file without an extension is deprecated. Please use explicit file extensions.';

export async function getParsedContent(file: string): Promise<RenovateConfig> {
switch (upath.extname(file)) {
case '.yaml':
Expand All @@ -24,7 +21,7 @@ export async function getParsedContent(file: string): Promise<RenovateConfig> {
return tmpConfig.default ? tmpConfig.default : tmpConfig;
}
default:
throw new Error(`Unsupported file type: ${file}`);
throw new Error('Unsupported file type');
}
}

Expand All @@ -33,16 +30,6 @@ export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
if (!upath.isAbsolute(configFile)) {
configFile = `${process.cwd()}/${configFile}`;
}
if (!upath.extname(configFile)) {
logger.info(fileDeprecationMessage);
for (const ext of ['.js', '.json']) {
const resolved = upath.addExt(configFile, ext);
if (await fs.exists(resolved)) {
configFile = resolved;
break;
}
}
}
logger.debug('Checking for config file in ' + configFile);
let config: AllConfig = {};
try {
Expand All @@ -52,7 +39,7 @@ export async function getConfig(env: NodeJS.ProcessEnv): Promise<AllConfig> {
if (err instanceof SyntaxError || err instanceof TypeError) {
logger.fatal(`Could not parse config file \n ${err.stack}`);
process.exit(1);
} else if (err.message.startsWith(`Unsupported file type`)) {
} else if (err.message === 'Unsupported file type') {
logger.fatal(err.message);
process.exit(1);
} else if (env.RENOVATE_CONFIG_FILE) {
Expand Down