From ee3240908b0737d0a429cf04b7cc3717c948199b Mon Sep 17 00:00:00 2001 From: Rhys Arkins <rhys@arkins.net> Date: Wed, 21 Mar 2018 10:08:55 +0100 Subject: [PATCH] feat: requireConfig (#1695) Adds option to bot owners to require a renovate config be in place. Closes #1694 --- lib/config/definitions.js | 9 ++++++ .../repository/onboarding/branch/check.js | 29 ++++++++++++++++--- lib/workers/repository/onboarding/pr/index.js | 8 +++-- .../onboarding/branch/index.spec.js | 23 +++++++++++++++ 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/config/definitions.js b/lib/config/definitions.js index 6e8675bf1a..e5580278dc 100644 --- a/lib/config/definitions.js +++ b/lib/config/definitions.js @@ -108,6 +108,15 @@ const options = [ default: false, admin: true, }, + { + name: 'requireConfig', + description: + 'Set to true if repositories must have a config to activate Renovate.', + stage: 'repository', + type: 'boolean', + default: false, + admin: true, + }, // encryption { name: 'privateKey', diff --git a/lib/workers/repository/onboarding/branch/check.js b/lib/workers/repository/onboarding/branch/check.js index 5e3f9e35ff..c956a433ac 100644 --- a/lib/workers/repository/onboarding/branch/check.js +++ b/lib/workers/repository/onboarding/branch/check.js @@ -39,12 +39,33 @@ const isOnboarded = async config => { logger.debug('package.json contains renovate config'); return true; } - if (await closedPrExists(config)) { - logger.debug('Found closed onboarding PR'); + const pr = await closedPrExists(config); + if (!pr) { + logger.debug('Found no closed onboarding PR'); + return false; + } + logger.debug('Found closed onboarding PR'); + if (!config.requireConfig) { + logger.debug('Config not mandatory so repo is considered onboarded'); return true; } - logger.debug('Found no closed onboarding PR'); - return false; + const mergedPrExists = (await platform.getPrList()).some( + p => p.state === 'merged' && p.branchName.startsWith(config.branchPrefix) + ); + if (mergedPrExists) { + logger.info( + 'Repo is not onboarded but at least one merged PR exists - treat as onboarded' + ); + return true; + } + logger.info('Repo is not onboarded and no merged PRs exist'); + // ensure PR comment + await platform.ensureComment( + pr.number, + 'Renovate is disabled', + 'Renovate is disabled due to lack of config. If you wish to reenable it, you can either (a) commit a Renovate config to your base branch, or (b) rename this closed PR to trigger a replacement onboarding PR.' + ); + throw new Error('disabled'); }; const onboardingPrExists = () => diff --git a/lib/workers/repository/onboarding/pr/index.js b/lib/workers/repository/onboarding/pr/index.js index 35840a29db..c6a03f3cd5 100644 --- a/lib/workers/repository/onboarding/pr/index.js +++ b/lib/workers/repository/onboarding/pr/index.js @@ -3,11 +3,13 @@ const { getErrors, getWarnings } = require('./errors-warnings'); const { getBaseBranchDesc } = require('./base-branch'); const { getPrList } = require('./pr-list'); -const prTemplate = `Welcome to [Renovate](https://renovateapp.com)! +const prTemplate = `Welcome to [Renovate](https://renovateapp.com)! This is an onboarding PR to help you understand and configure settings before regular Pull Requests begin. -This is an onboarding PR to help you understand and configure Renovate before regular Pull Requests begin. Once you merge or close this Pull Request, Renovate will begin keeping your dependencies up-to-date. +Renovate will begin keeping your dependencies up-to-date only once you merge this Pull Request. +If you close this Pull Request unmerged, then Renovate will be disabled. -If you have any questions, try reading our [Docs](https://renovateapp.com/docs), particularly the Getting Started section. You can post questions in [our Config Help repository](https://github.com/renovateapp/config-help/issues) or @ the app author @rarkins in this PR and he'll probably see it. +If you have any questions, try reading our [Docs](https://renovateapp.com/docs), particularly the Getting Started section. +You can post questions in [our Config Help repository](https://github.com/renovateapp/config-help/issues) or @ the app author @rarkins in this PR and he'll probably see it. --- {{PACKAGE FILES}} diff --git a/test/workers/repository/onboarding/branch/index.spec.js b/test/workers/repository/onboarding/branch/index.spec.js index 4eb3339280..a61d7cc2d1 100644 --- a/test/workers/repository/onboarding/branch/index.spec.js +++ b/test/workers/repository/onboarding/branch/index.spec.js @@ -55,6 +55,29 @@ describe('workers/repository/onboarding/branch', () => { const res = await checkOnboardingBranch(config); expect(res.repoIsOnboarded).toBe(true); }); + it('detects repo is onboarded via PR and merged', async () => { + config.requireConfig = true; + platform.findPr.mockReturnValue(true); + platform.getPrList.mockReturnValueOnce([ + { branchName: 'renovate/something', state: 'merged' }, + ]); + const res = await checkOnboardingBranch(config); + expect(res.repoIsOnboarded).toBe(true); + }); + it('throws if no required config', async () => { + config.requireConfig = true; + platform.findPr.mockReturnValue(true); + platform.getPrList.mockReturnValueOnce([ + { branchName: 'renovate/something', state: 'open' }, + ]); + let e; + try { + await checkOnboardingBranch(config); + } catch (err) { + e = err; + } + expect(e).toBeDefined(); + }); it('creates onboarding branch', async () => { platform.getFileList.mockReturnValue(['package.json']); const res = await checkOnboardingBranch(config); -- GitLab