Skip to content

Commit

Permalink
chore(badges): make badge host configurable via environment
Browse files Browse the repository at this point in the history
  • Loading branch information
rmehner committed Feb 1, 2018
1 parent 37264e1 commit e8838f5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
6 changes: 3 additions & 3 deletions jobs/create-initial-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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')
Expand Down
1 change: 1 addition & 0 deletions lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
73 changes: 72 additions & 1 deletion test/jobs/create-initial-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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')
])
})

Expand Down

0 comments on commit e8838f5

Please sign in to comment.