Skip to content

Commit fd0db3a

Browse files
authored
chore(logging): Use bunyan for logging and add debug logs in checks. (mergeability#155)
* chore(logging): Use bunyan for logging and add debug logs in checks. * test(logging): update tests to reflect changes to using log.child
1 parent 6cec9a1 commit fd0db3a

File tree

4 files changed

+45
-30
lines changed

4 files changed

+45
-30
lines changed

__fixtures__/helper.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ const throwNotFound = () => {
77
}
88

99
module.exports = {
10-
mockContext: (options) => {
11-
if (!options) options = {}
12-
10+
mockContext: (options = {}) => {
1311
return {
1412
repo: (properties) => { return Object.assign({ owner: 'owner', repo: 'repo' }, properties) },
1513
event: (options.event) ? options.event : 'pull_request',
1614
payload: {
15+
repository: {
16+
full_name: 'name'
17+
},
1718
pull_request: {
1819
user: {
1920
login: 'creator'
@@ -36,9 +37,13 @@ module.exports = {
3637
}
3738
},
3839
log: {
39-
debug: (s) => console.log(`TEST[debug] > ${JSON.stringify(s)}`),
40-
info: (s) => console.log(`TEST[info] > ${JSON.stringify(s)}`),
41-
warn: (s) => console.log(`TEST[warn] > ${JSON.stringify(s)}`)
40+
child: (s) => {
41+
return {
42+
debug: (s) => console.log(`TEST[debug] > ${JSON.stringify(s)}`),
43+
info: (s) => console.log(`TEST[info] > ${JSON.stringify(s)}`),
44+
warn: (s) => console.log(`TEST[warn] > ${JSON.stringify(s)}`)
45+
}
46+
}
4247
},
4348
github: {
4449
repos: {

__tests__/mergeable.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ const startMergeable = (mode, version) => {
4343
const mockRobot = {
4444
on: jest.fn(),
4545
log: {
46-
warn: jest.fn(),
47-
info: jest.fn()
46+
child: () => {
47+
return {
48+
debug: jest.fn(),
49+
info: jest.fn()
50+
}
51+
}
4852
}
4953
}

lib/actions/checks.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ const createChecks = async (context, name, status, output) => {
2222
}
2323
status = !status ? 'in_progress' : status
2424

25-
return context.github.checks.create(
26-
createParams({context, headBranch, headSha, name, status, output})
27-
)
25+
let log = context.log.child({name: 'mergeable'})
26+
let params = createParams({context, headBranch, headSha, name, status, output})
27+
log.debug(`Creating Check for ${context.payload.repository.full_name} - status=${status}`)
28+
log.debug(params)
29+
return context.github.checks.create(params)
2830
}
2931

3032
const updateChecks = async (context, id, name, status, conclusion, output) => {
@@ -36,12 +38,13 @@ const updateChecks = async (context, id, name, status, conclusion, output) => {
3638
}
3739

3840
status = !status ? 'completed' : status
39-
4041
conclusion = !conclusion ? 'success' : conclusion
4142

42-
await context.github.checks.update(
43-
updateParams({context, name, status, output, id, conclusion})
44-
)
43+
let log = context.log.child({name: 'mergeable'})
44+
let params = updateParams({context, name, status, output, id, conclusion})
45+
log.debug(`Updating Check for ${context.payload.repository.full_name} - status=${status}`)
46+
log.debug(params)
47+
await context.github.checks.update(params)
4548
}
4649

4750
const createParams = ({context, headBranch, headSha, name, status, output}) => {

lib/mergeable.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ class Mergeable {
1212
}
1313

1414
start (robot) {
15+
let log = robot.log.child({name: 'mergeable'})
16+
let intervalSecs = 2
1517
if (this.mode === 'development') {
16-
robot.log.info('In DEVELOPMENT mode.')
17-
robot.log.info('Starting scheduler at 2 second intervals.')
18-
this.schedule(robot, { interval: 60 * 60 * 2 })
18+
log.info('In DEVELOPMENT mode.')
1919
} else {
20-
robot.log.info('In PRODUCTION mode.')
21-
if (process.env.MERGEABLE_SCHEDULER === 'true') {
22-
robot.log.info('Starting scheduler at 1 hour intervals')
23-
this.schedule(robot, { interval: 60 * 60 * 1000 })
24-
} else {
25-
robot.log.info(`Scheduler: ${'off'.bold.white}!`)
26-
}
20+
log.info('In PRODUCTION mode.')
21+
intervalSecs = 1000
22+
}
23+
24+
if (process.env.MERGEABLE_SCHEDULER === 'true') {
25+
log.info('Starting scheduler at 2 second intervals.')
26+
this.schedule(robot, { interval: 60 * 60 * intervalSecs })
27+
} else {
28+
log.info(`Scheduler: ${'off'.bold.white}!`)
2729
}
2830

29-
robot.log.info(`Version: ${this.version.bold.white}`)
31+
log.info(`Version: ${this.version.bold.white}`)
3032

3133
if (this.version === 'flex') {
3234
this.flex(robot)
@@ -42,15 +44,16 @@ class Mergeable {
4244
// version 2 of mergeable WIP as we refactor the code base.
4345
flex (robot) {
4446
robot.on('*', async context => {
47+
let log = context.log.child({name: 'mergeable'})
4548
if (_.isUndefined(context.payload.repository)) {
46-
context.log.warn('Unable to log repository name. There is no repository in the payload:')
47-
context.log.warn(context.payload)
49+
log.warn('Unable to log repository name. There is no repository in the payload:')
50+
log.warn(context.payload)
4851
} else {
49-
context.log.info({Repo: context.payload.repository.full_name,
52+
log.info({Repo: context.payload.repository.full_name,
5053
url: context.payload.repository.html_url,
5154
private: context.payload.repository.private})
5255
}
53-
context.log.info({event: context.event, action: context.payload.action})
56+
log.info({event: context.event, action: context.payload.action})
5457
flexExecutor(context)
5558
})
5659
}

0 commit comments

Comments
 (0)