From e8838f5ef338128575011ae37154bab335701631 Mon Sep 17 00:00:00 2001 From: Robin Mehner Date: Wed, 31 Jan 2018 18:32:31 +0100 Subject: [PATCH] chore(badges): make badge host configurable via environment --- jobs/create-initial-branch.js | 6 +-- lib/env.js | 1 + test/jobs/create-initial-branch.js | 73 +++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/jobs/create-initial-branch.js b/jobs/create-initial-branch.js index 41a95f1e..7e4520aa 100644 --- a/jobs/create-initial-branch.js +++ b/jobs/create-initial-branch.js @@ -144,10 +144,10 @@ module.exports = async function ({ repositoryId }) { const badgesTokenMaybe = repoDoc.private ? `?token=${tokenHash}&ts=${Date.now()}` : '' - const badgeUrl = `https://badges.greenkeeper.io/${slug}.svg${badgesTokenMaybe}` + const badgeUrl = `https://${env.BADGES_HOST}/${slug}.svg${badgesTokenMaybe}` log.info('badge: url', {badgeUrl}) - const privateBadgeRegex = /https:\/\/badges\.(staging\.)?greenkeeper\.io\/.+?\.svg\?token=\w+(&ts=\d+)?/ + const privateBadgeRegex = new RegExp(`https://${env.BADGES_HOST}.+?.svg\\?token=\\w+(&ts=\\d+)?`) let badgeAlreadyAdded = false const transforms = [ @@ -187,7 +187,7 @@ module.exports = async function ({ repositoryId }) { badgeAlreadyAdded = _.includes( readme, - 'https://badges.greenkeeper.io/' + `https://${env.BADGES_HOST}/` ) if (!repoDoc.private && badgeAlreadyAdded) { log.info('badge: Repository already has badge') diff --git a/lib/env.js b/lib/env.js index 495399e4..b3396c1b 100644 --- a/lib/env.js +++ b/lib/env.js @@ -29,6 +29,7 @@ module.exports = envalid.cleanEnv(process.env, { NPM_REGISTRY: url({ default: 'https://registry.npmjs.org/' }), GITHUB_URL: url({ default: 'https://github.com' }), BADGES_SECRET: str({ devDefault: 'badges-secret' }), + BADGES_HOST: str({default: 'badges.greenkeeper.io'}), NPMHOOKS_SECRET: str({ devDefault: 'make-secrets-great-again' }), STRIPE_SECRET_KEY: str({ devDefault: 'stripe-token' }), IS_ENTERPRISE: bool({ default: false }), diff --git a/test/jobs/create-initial-branch.js b/test/jobs/create-initial-branch.js index 8d107ccf..35137052 100644 --- a/test/jobs/create-initial-branch.js +++ b/test/jobs/create-initial-branch.js @@ -10,6 +10,7 @@ const dbs = require('../../lib/dbs') test('create-initial-branch', async t => { t.beforeEach(() => { delete process.env.IS_ENTERPRISE + delete process.env.BADGES_HOST cleanCache('../../lib/env') return Promise.resolve() }) @@ -127,6 +128,76 @@ test('create-initial-branch', async t => { ) }) + t.test('uses the BADGES_HOST to create the badge url', async t => { + process.env.BADGES_HOST = 'badges.staging.greenkeeper.io' + await repositories.put({ + _id: '47', + accountId: '123', + fullName: 'finnp/test' + }) + + nock('https://api.github.com') + .post('/installations/37/access_tokens') + .reply(200, { + token: 'secret' + }) + .get('/rate_limit') + .reply(200, {}) + .get('/repos/finnp/test/contents/package.json') + .reply(200, { + path: 'package.json', + content: encodePkg({ devDependencies }) + }) + .get('/repos/finnp/test') + .reply(200, { + default_branch: 'custom' + }) + .post('/repos/finnp/test/labels', { + name: 'greenkeeper', + color: '00c775' + }) + .reply(201) + + nock('https://registry.npmjs.org') + .get('/@finnpauls%2Fdep') + .reply(200, { + 'dist-tags': { + latest: '2.0.0' + } + }) + .get('/@finnpauls%2Fdep2') + .reply(200, { + 'dist-tags': { + latest: '3.0.0-rc1' + }, + versions: { + '3.0.0-rc1': true + } + }) + + const worker = proxyquire('../../jobs/create-initial-branch', { + '../lib/create-branch': ({ transforms }) => { + transforms[2].transform( + 'readme-badger\n=============\n', + 'README.md' + ) + + return '1234abcd' + } + }) + + await worker({ + repositoryId: 47 + }) + + const newBranch = await repositories.get('47:branch:1234abcd') + t.is( + newBranch.badgeUrl, + 'https://badges.staging.greenkeeper.io/finnp/test.svg', + 'badgeUrl' + ) + }) + t.test('badge already added', async t => { await repositories.put({ _id: '44', @@ -306,7 +377,7 @@ tearDown(async () => { await Promise.all([ removeIfExists(installations, '123'), removeIfExists(payments, '123'), - removeIfExists(repositories, '42', '43', '44', '45', '46', '42:branch:1234abcd') + removeIfExists(repositories, '42', '43', '44', '45', '46', '47', '42:branch:1234abcd', '47:branch:1234abcd') ]) })