Skip to content

Commit 1d71f54

Browse files
committed
🔇 Better logging
1 parent f2ba3c1 commit 1d71f54

3 files changed

Lines changed: 90 additions & 17 deletions

File tree

‎index.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ module.exports = async (app) => {
7676
}
7777

7878
if (!context.payload.repository.fork) {
79-
app.log.info(`[${context.payload.repository.full_name}] Not a fork, unscheduled`)
79+
app.log.debug(`[${context.payload.repository.full_name}] Not a fork, unscheduled`)
8080
scheduler.stop(context.payload.repository)
8181
return null
8282
}
8383

8484
const config = await getConfig(context, PULL_CONFIG) ||
8585
await getDefaultConfig(context.github, context.repo({ logger: app.log }))
8686
if (!config) {
87-
app.log.info(`[${context.payload.repository.full_name}] Unable to fetch config, unscheduled`)
87+
app.log.warn({}, `[${context.payload.repository.full_name}] Unable to fetch config, unscheduled`)
8888
scheduler.stop(context.payload.repository)
8989
return null
9090
}
@@ -104,7 +104,7 @@ module.exports = async (app) => {
104104
const defaultBranch = repoInfo.data.parent.default_branch
105105

106106
if (upstreamOwner && defaultBranch) {
107-
logger.info(`[${owner}/${repo}] Using default config ${defaultBranch}...${upstreamOwner}:${defaultBranch}`)
107+
logger.debug(`[${owner}/${repo}] Using default config ${defaultBranch}...${upstreamOwner}:${defaultBranch}`)
108108
return {
109109
version: '1',
110110
rules: [

‎lib/pull.js‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ module.exports = class Pull {
2525
if (base.toLowerCase() === upstream.toLowerCase().replace(`${this.config.owner.toLowerCase()}:`, '')) {
2626
this.logger.debug(`[${this.config.repoPath}] ${base} is same as ${upstream}`)
2727
} else if (!(await this.hasDiff(base, upstream))) {
28-
this.logger.info(`[${this.config.repoPath}] ${base} is in sync with ${upstream}`)
28+
this.logger.debug(`[${this.config.repoPath}] ${base} is in sync with ${upstream}`)
2929
} else {
3030
const openPR = await this.getOpenPR(base) // Get one PR opened by app/pull
3131
if (openPR) {
32-
this.logger.info(`[${this.config.repoPath}] Found a PR from ${upstream} to ${base}`)
32+
this.logger.debug(`[${this.config.repoPath}] Found a PR from ${upstream} to ${base}`)
3333
await this.checkAutoMerge(openPR)
3434
} else {
3535
this.logger.info(`[${this.config.repoPath}] Creating PR from ${upstream} to ${base}`)
@@ -60,8 +60,8 @@ module.exports = class Pull {
6060
await this.hardResetCommit(incomingPR.base.ref, incomingPR.head.sha)
6161
this.logger.info(`[${this.config.repoPath}]#${prNumber} Hard reset successful`)
6262
return true
63-
} catch (_) {
64-
this.logger.debug(`[${this.config.repoPath}]#${prNumber} Hard reset failed`)
63+
} catch (error) {
64+
this.logger.warn({ error }, `[${this.config.repoPath}]#${prNumber} Hard reset failed`)
6565
}
6666
} else {
6767
const mergable = incomingPR.mergeable_state === 'clean' ||
@@ -80,20 +80,39 @@ module.exports = class Pull {
8080
}
8181

8282
async hasDiff (base, upstream) {
83-
const comparison = await this.github.repos.compareCommits({
84-
owner: this.config.owner,
85-
repo: this.config.repo,
86-
head: upstream,
87-
base
88-
})
89-
return comparison.data.total_commits > 0
83+
try {
84+
const comparison = await this.github.repos.compareCommits({
85+
owner: this.config.owner,
86+
repo: this.config.repo,
87+
head: upstream,
88+
base
89+
})
90+
return comparison.data.total_commits > 0
91+
} catch (ex) {
92+
if (ex.message.match(/this diff is taking too long to generate/i)) {
93+
return true
94+
} else if (ex.message.match(/not found/i)) {
95+
this.logger.debug(`[${this.config.repoPath}] ${this.config.owner}:${this.config.base}...${upstream} Not found`)
96+
return false
97+
} else if (ex.message.match(/no common ancestor/i)) {
98+
this.logger.debug(`[${this.config.repoPath}] ${this.config.owner}:${this.config.base}...${upstream} No common ancestor`)
99+
return false
100+
} else {
101+
this.logger.warn({
102+
err: ex,
103+
owner: this.config.owner,
104+
repo: this.config.repo,
105+
head: upstream
106+
}, `[${this.config.repoPath}] Unable to fetch diff`)
107+
return false
108+
}
109+
}
90110
}
91111

92112
async getOpenPR (base) {
93113
const res = await this.github.search.issues({
94114
q: `repo:${this.config.repoPath} type:pr is:open base:${base} author:app/pull`,
95-
per_page: 1,
96-
page: 1
115+
per_page: 1
97116
})
98117
if (res.data.total_count > 0) {
99118
const pr = await this.github.pullRequests.get({
@@ -132,7 +151,7 @@ module.exports = class Pull {
132151

133152
await this.addReviewers(prNumber, reviewers)
134153

135-
this.logger.info(`[${this.config.repoPath}]#${prNumber} Updated pull request`)
154+
this.logger.debug(`[${this.config.repoPath}]#${prNumber} Updated pull request`)
136155
}
137156

138157
async isMergeable (number, config = {}) {

‎test/pull.test.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,60 @@ describe('pull - routineCheck', () => {
122122
expect(github.issues.edit).not.toHaveBeenCalled()
123123
})
124124

125+
test('diff too large', async () => {
126+
github.repos.compareCommits.mockImplementation(() => { throw Error('Server Error: Sorry, this diff is taking too long to generate.') })
127+
github.search.issues.mockResolvedValue({ data: { total_count: 0 } })
128+
github.pullRequests.create.mockResolvedValue({ data: {
129+
number: 12,
130+
base: { ref: 'master' },
131+
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder-12' },
132+
state: 'open',
133+
user: { login: 'pull[bot]' },
134+
mergeable: true,
135+
mergeable_state: 'clean'
136+
}})
137+
138+
const pull = getPull()
139+
await pull.routineCheck()
140+
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
141+
expect(github.search.issues).toHaveBeenCalled()
142+
expect(github.pullRequests.create).toHaveBeenCalled()
143+
expect(github.issues.edit).toHaveBeenCalled()
144+
})
145+
146+
test('diff not found', async () => {
147+
github.repos.compareCommits.mockImplementation(() => { throw Error('Not Found') })
148+
149+
const pull = getPull()
150+
await pull.routineCheck()
151+
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
152+
expect(github.search.issues).not.toHaveBeenCalled()
153+
expect(github.pullRequests.create).not.toHaveBeenCalled()
154+
expect(github.issues.edit).not.toHaveBeenCalled()
155+
})
156+
157+
test('diff no common ancestor', async () => {
158+
github.repos.compareCommits.mockImplementation(() => { throw Error('No common ancestor between --- and ---:---') })
159+
160+
const pull = getPull()
161+
await pull.routineCheck()
162+
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
163+
expect(github.search.issues).not.toHaveBeenCalled()
164+
expect(github.pullRequests.create).not.toHaveBeenCalled()
165+
expect(github.issues.edit).not.toHaveBeenCalled()
166+
})
167+
168+
test('diff other error', async () => {
169+
github.repos.compareCommits.mockImplementation(() => { throw Error('Internal Server Error') })
170+
171+
const pull = getPull()
172+
await pull.routineCheck()
173+
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
174+
expect(github.search.issues).not.toHaveBeenCalled()
175+
expect(github.pullRequests.create).not.toHaveBeenCalled()
176+
expect(github.issues.edit).not.toHaveBeenCalled()
177+
})
178+
125179
test('yes diff, already has PR', async () => {
126180
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
127181
github.search.issues.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 12 } ] } })

0 commit comments

Comments
 (0)