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): allow exporting async config #13075

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
14 changes: 13 additions & 1 deletion docs/usage/getting-started/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ WhiteSource Renovate On-Premises and WhiteSource Remediate both run as long-live

### Global config

Renovate's server-side/admin config is referred to as its "global" config, and can be specified using either a config file (`config.js` or `config.json`), environment variables, or CLI parameters.
Renovate's server-side/admin config is referred to as its "global" config, and can be specified using either a config file (`config.js`, `config.json`, `config.json5`, `config.yaml` or `config.yml`), environment variables, or CLI parameters.

Some config is global-only, meaning that either it is only applicable to the bot administrator or it can only be controlled by the administrator and not repository users.
Those are documented in [Self-hosted Configuration](../self-hosted-configuration.md).
Expand All @@ -108,6 +108,18 @@ If you combine both of the above then any single config option in the environmen

Note: it's also possible to change the default prefix from `RENOVATE_` using `ENV_PREFIX`. e.g. `ENV_PREFIX=RNV_ RNV_TOKEN=abc123 renovate`.

#### Using `config.js`

If you use a `config.js`, it will be expected to export a configuration via `module.exports`. The value can be either a plain JavaScript object like in this example where `config.js` exports a plain object:
pmorch marked this conversation as resolved.
Show resolved Hide resolved

```javascript
module.exports = {
token: 'abcdefg',
};
```

`config.js` may also export a `Promise` of such an object, or a function that will return either a plain Javascript object or a `Promise` of such an object. This allows one to include the results of asynchronous operations in the exported value. An example of a `config.js` that exports an async function (which is a function that returns a `Promise`) can be seen in a comment for [#10011: Allow autodiscover filtering for repo topic](https://github.com/renovatebot/renovate/issues/10011#issuecomment-992568583) and more examples can be seen in [`file.spec.ts`](https://github.com/renovatebot/renovate/blob/main/lib/workers/global/config/parse/file.spec.ts).
pmorch marked this conversation as resolved.
Show resolved Hide resolved

### Authentication

Regardless of platform, you need to select a user account for `renovate` to assume the identity of, and generate a Personal Access Token.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This is functionally equivalent to config-function-promise.js but syntactically different
viceice marked this conversation as resolved.
Show resolved Hide resolved

// @ts-ignore
module.exports = async function () {
return {
token: 'abcdefg',
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This is functionally equivalent to config-async-function.js but syntactically different

// @ts-ignore
module.exports = function () {
return new Promise(resolve => {
resolve({
token: 'abcdefg',
})
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-ignore
module.exports = function () {
return {
token: 'abcdefg',
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-ignore
module.exports = new Promise( resolve => {
resolve(
{
token: 'abcdefg',
}
);
});
17 changes: 14 additions & 3 deletions lib/workers/global/config/parse/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import { DirectoryResult, dir } from 'tmp-promise';
import upath from 'upath';
import { logger } from '../../../../logger';
import customConfig from './__fixtures__/file';
import customConfig from './__fixtures__/config';
import * as file from './file';

describe('workers/global/config/parse/file', () => {
Expand All @@ -18,7 +18,18 @@ describe('workers/global/config/parse/file', () => {

describe('.getConfig()', () => {
it.each([
['custom config file with extension', 'file.js'],
['custom js config file', 'config.js'],
['custom js config file exporting a Promise', 'config-promise.js'],
['custom js config file exporting a function', 'config-function.js'],
// The next two are different syntactic ways of expressing the same thing
[
'custom js config file exporting a function returning a Promise',
'config-function-promise.js',
],
[
'custom js config file exporting an async function',
'config-async-function.js',
],
['JSON5 config file', 'config.json5'],
['YAML config file', 'config.yaml'],
])('parses %s', async (fileType, filePath) => {
Expand All @@ -29,7 +40,7 @@ describe('workers/global/config/parse/file', () => {
});

it('migrates', async () => {
const configFile = upath.resolve(__dirname, './__fixtures__/file2.js');
const configFile = upath.resolve(__dirname, './__fixtures__/config2.js');
const res = await file.getConfig({ RENOVATE_CONFIG_FILE: configFile });
expect(res).toMatchSnapshot();
expect(res.rangeStrategy).toBe('bump');
Expand Down
8 changes: 7 additions & 1 deletion lib/workers/global/config/parse/file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import is from 'is';
import { load } from 'js-yaml';
import JSON5 from 'json5';
import upath from 'upath';
Expand All @@ -18,7 +19,12 @@ export async function getParsedContent(file: string): Promise<RenovateConfig> {
return JSON5.parse(await readFile(file, 'utf8'));
case '.js': {
const tmpConfig = await import(file);
return tmpConfig.default ? tmpConfig.default : tmpConfig;
let config = tmpConfig.default ? tmpConfig.default : tmpConfig;
// Allow the config to be a function
if (is.fn(config)) {
config = config();
}
return config;
viceice marked this conversation as resolved.
Show resolved Hide resolved
}
default:
throw new Error('Unsupported file type');
Expand Down