Skip to content

Commit

Permalink
feat(invalid-config-file): open issue if config is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Realtin authored and hulkoba committed Mar 26, 2018
1 parent 56c1f35 commit 9477c6f
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 7 deletions.
4 changes: 2 additions & 2 deletions content/invalid-config-issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const md = require('./template')

// TODO: write something better!

module.exports = ({errors}) =>
module.exports = ({message, errors}) =>
md`We have detected a Problem with your Greenkeeper Config File 🚨
Errors: ${errors}
${message}
Example of a valid config object link
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function (doc) {
if(doc.type === 'issue' && doc.invalidConfig && doc.state === 'open') {
emit(doc.repositoryId);
}
}
20 changes: 15 additions & 5 deletions jobs/invalid-config-file.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
const _ = require('lodash')

const dbs = require('../lib/dbs')
// const statsd = require('../lib/statsd')
const statsd = require('../lib/statsd')
const githubQueue = require('../lib/github-queue')
const updatedAt = require('../lib/updated-at')
const invalidConfigBody = require('../content/invalid-config-issue')
const getConfig = require('../lib/get-config')

module.exports = async function ({ repositoryId, accountId, errors }) {
module.exports = async function ({ repositoryId, accountId, message }) {
const { installations, repositories } = await dbs()
const installation = await installations.get(String(accountId))
const installationId = installation.installation

// TODO: don't send too many issues!!
const openIssues = _.get(
await repositories.query('open_invalid_config_issue', {
key: repositoryId,
include_docs: true
}),
'rows'
)
// don't send too many issues!
if (openIssues && openIssues.length) return

// TODO: bring errors messages in a format that can be used in the issue body
// error example:
Expand All @@ -31,11 +41,11 @@ module.exports = async function ({ repositoryId, accountId, errors }) {
owner,
repo,
title: `Invalid Greenkeeper Configuration file`,
body: invalidConfigBody({ errors }),
body: invalidConfigBody({ message }),
labels: [label]
}))

// statsd.increment('initial_issues')
statsd.increment('invalid_config_issues')

await repositories.put(
updatedAt({
Expand Down
173 changes: 173 additions & 0 deletions test/jobs/invalid-config-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
const nock = require('nock')
const _ = require('lodash')

const dbs = require('../../lib/dbs')
const removeIfExists = require('../helpers/remove-if-exists')

const invalidConfigFile = require('../../jobs/invalid-config-file')

describe('invalid-config-file', async () => {
beforeAll(async () => {
const { installations, repositories } = await dbs()
await installations.put({
_id: '2020',
installation: 37
})
await repositories.put({
_id: 'invalid-config1',
fullName: 'lisa/monorepo'
})
})

test('create new issue', async () => {
const githubMock = nock('https://api.github.com')
.post('/installations/37/access_tokens')
.reply(200, {
token: 'secret'
})
.get('/rate_limit')
.reply(200, {})
.post('/repos/lisa/monorepo/issues', ({ title, body, labels }) => {
expect(title).toBeTruthy()
expect(body).toBeTruthy()
expect(labels).toContain('greenkeeper')
return true
})
.reply(201, () => {
// issue created
expect(true).toBeTruthy()
return {
number: 10
}
})

const newJobs = await invalidConfigFile({
repositoryId: 'invalid-config1',
accountId: '2020',
message: '"#invalid#groupname#" is not allowed'
})
expect(newJobs).toBeFalsy()

const { repositories } = await dbs()
const issue = await repositories.get('invalid-config1:issue:10')
expect(issue.initial).toBeFalsy()
expect(issue.invalidConfig).toBeTruthy()
expect(issue.type).toEqual('issue')
expect(issue.number).toBe(10)
expect(issue.repositoryId).toBe('invalid-config1')
githubMock.done()
})

test('an open issue already exists', async () => {
nock('https://api.github.com') // no request should be made
expect.assertions(2)

const { installations, repositories } = await dbs()
await installations.put({
_id: '2121',
installation: 38
})
await repositories.put({
_id: 'invalid-config2',
fullName: 'lisa/monorepo'
})
await repositories.put({
_id: 'invalid-config2:issue:10',
type: 'issue',
initial: false,
invalidConfig: true,
repositoryId: 'invalid-config2',
number: 10,
state: 'open'
})

const newJobs = await invalidConfigFile({
repositoryId: 'invalid-config2',
accountId: 2121
})

expect(newJobs).toBeFalsy()

const openIssues = _.get(
await repositories.query('open_invalid_config_issue', {
key: 'invalid-config2',
include_docs: true
}),
'rows'
)

expect(openIssues).toHaveLength(1)
})

test('a closed issue already exists', async () => {
expect.assertions(7)

const { installations, repositories } = await dbs()
await installations.put({
_id: '2222',
installation: 39
})
await repositories.put({
_id: 'invalid-config3',
fullName: 'lisa/monorepo'
})
await repositories.put({
_id: 'invalid-config3:issue:10',
type: 'issue',
initial: false,
invalidConfig: true,
repositoryId: 'invalid-config3',
number: 10,
state: 'closed'
})

nock('https://api.github.com')
.post('/installations/39/access_tokens')
.reply(200, {
token: 'secret'
})
.get('/rate_limit')
.reply(200, {})
.post('/repos/lisa/monorepo/issues', ({ title, body, labels }) => {
expect(title).toBeTruthy()
expect(body).toBeTruthy()
expect(labels).toContain('greenkeeper')
return true
})
.reply(201, () => {
// issue created
expect(true).toBeTruthy()
return {
number: 11
}
})

const newJobs = await invalidConfigFile({
repositoryId: 'invalid-config3',
accountId: 2222
})

expect(newJobs).toBeFalsy()

const openIssues = _.get(
await repositories.query('open_invalid_config_issue', {
key: 'invalid-config3',
include_docs: true
}),
'rows'
)

expect(openIssues).toHaveLength(1)
expect(openIssues[0].doc._id).toEqual('invalid-config3:issue:11')
})
})

afterAll(async () => {
const { repositories, installations } = await dbs()
await Promise.all([
removeIfExists(installations, '2020', '2121'),
removeIfExists(repositories, 'invalid-config1', 'invalid-config1:issue:10',
'invalid-config2', 'invalid-config2:issue:10',
'invalid-config3', 'invalid-config3:issue:10', 'invalid-config3:issue:11')
])
})

0 comments on commit 9477c6f

Please sign in to comment.