Skip to content

Commit cfcacde

Browse files
committed
✨ Update PR merge logic after creation
Remove pull_request event
1 parent 76f0f1e commit cfcacde

3 files changed

Lines changed: 97 additions & 68 deletions

File tree

‎index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = async (app) => {
2020

2121
app.on('schedule.repository', routineCheck)
2222
app.on('push', handlePush)
23-
app.on(['pull_request', 'pull_request_review'], checkPRStatus)
23+
app.on('pull_request_review', checkPRStatus)
2424

2525
async function handlePush (context) {
2626
if (context.payload.commits.filter(c => c.message.indexOf(app.CONFIG_FILENAME) > -1).length > 0) {

‎lib/pull.js‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ module.exports = class Pull {
3535
} else if (!(await this.hasDiff(base, upstream))) {
3636
this.logger.debug(`[${this.config.repoPath}] ${base} is in sync with ${upstream}`)
3737
} else {
38-
const openPR = await this.getOpenPR(base) // Get one PR opened by app/pull
38+
const openPR = await this.getOpenPR(base, upstream) // Get PR opened by pull[bot]
3939
if (openPR) {
4040
this.logger.debug(`[${this.config.repoPath}] Found a PR from ${upstream} to ${base}`)
4141
await this.checkAutoMerge(openPR)
4242
} else {
4343
this.logger.info(`[${this.config.repoPath}] Creating PR from ${upstream} to ${base}`)
44-
await this.createPR(base, upstream, assignees, reviewers)
44+
await this.checkAutoMerge(await this.createPR(base, upstream, assignees, reviewers))
4545
}
4646
}
4747
}
@@ -126,18 +126,29 @@ module.exports = class Pull {
126126
}
127127
}
128128

129-
async getOpenPR (base) {
130-
const res = await this.github.search.issuesAndPullRequests({
131-
q: `repo:${this.config.repoPath} type:pr is:open base:${base} author:app/pull`,
132-
per_page: 1
129+
async getOpenPR (base, head) {
130+
const res = await this.github.issues.listForRepo({
131+
owner: this.config.owner,
132+
repo: this.config.repo,
133+
creator: 'pull[bot]',
134+
per_page: 100
133135
})
134-
if (res.data.total_count > 0) {
135-
const pr = await this.github.pulls.get({
136-
owner: this.config.owner,
137-
repo: this.config.repo,
138-
number: res.data.items[0].number
139-
})
140-
return pr.data
136+
if (res.data.length > 0) {
137+
for (var i = 0; i < res.data.length; i++) {
138+
const pr = await this.github.pulls.get({
139+
owner: this.config.owner,
140+
repo: this.config.repo,
141+
number: res.data[i].number
142+
})
143+
if (
144+
pr.data &&
145+
pr.data.user.login === 'pull[bot]' &&
146+
pr.data.base.label.replace(`${this.config.owner}:`, '') === base.replace(`${this.config.owner}:`, '') &&
147+
pr.data.head.label.replace(`${this.config.owner}:`, '') === head.replace(`${this.config.owner}:`, '')
148+
) {
149+
return pr.data
150+
}
151+
}
141152
}
142153
return null
143154
}
@@ -170,8 +181,16 @@ module.exports = class Pull {
170181
await this.addReviewers(prNumber, reviewers)
171182

172183
this.logger.debug(`[${this.config.repoPath}]#${prNumber} Updated pull request`)
184+
185+
const pr = await this.github.pulls.get({
186+
owner: this.config.owner,
187+
repo: this.config.repo,
188+
number: createdPR.data.number
189+
})
190+
return pr.data
173191
} catch (e) {
174192
this.logger.warn(e, `[${this.config.repoPath}] Create PR from ${upstream} failed`)
193+
return null
175194
}
176195
}
177196

‎test/pull.test.js‎

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ beforeEach(() => {
2424
merge: jest.fn()
2525
},
2626
issues: {
27-
update: jest.fn()
27+
update: jest.fn(),
28+
listForRepo: jest.fn()
2829
},
2930
git: {
3031
updateRef: jest.fn()
31-
},
32-
search: {
33-
issuesAndPullRequests: jest.fn()
3432
}
3533
}
3634

@@ -95,7 +93,7 @@ describe('pull - routineCheck', () => {
9593
const pull = new Pull(github, { owner: 'wei', repo: 'fork', logger: app.log }, configs[i])
9694
await pull.routineCheck()
9795
expect(github.repos.compareCommits).not.toHaveBeenCalled()
98-
expect(github.search.issuesAndPullRequests).not.toHaveBeenCalled()
96+
expect(github.issues.listForRepo).not.toHaveBeenCalled()
9997
expect(github.pulls.create).not.toHaveBeenCalled()
10098
expect(github.issues.update).not.toHaveBeenCalled()
10199
}
@@ -115,14 +113,14 @@ describe('pull - routineCheck', () => {
115113
expect(github.repos.compareCommits).nthCalledWith(2, {
116114
owner: 'wei', repo: 'fork', base: 'feature/new-1', head: 'upstream:dev'
117115
})
118-
expect(github.search.issuesAndPullRequests).not.toHaveBeenCalled()
116+
expect(github.issues.listForRepo).not.toHaveBeenCalled()
119117
expect(github.pulls.create).not.toHaveBeenCalled()
120118
expect(github.issues.update).not.toHaveBeenCalled()
121119
})
122120

123121
test('diff too large', async () => {
124122
github.repos.compareCommits.mockImplementation(() => { throw Error('Server Error: Sorry, this diff is taking too long to generate.') })
125-
github.search.issuesAndPullRequests.mockResolvedValue({ data: { total_count: 0 } })
123+
github.issues.listForRepo.mockResolvedValue({ data: [] })
126124
github.pulls.create.mockResolvedValue({ data: {
127125
number: 12,
128126
base: { ref: 'master' },
@@ -136,7 +134,7 @@ describe('pull - routineCheck', () => {
136134
const pull = getPull()
137135
await pull.routineCheck()
138136
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
139-
expect(github.search.issuesAndPullRequests).toHaveBeenCalled()
137+
expect(github.issues.listForRepo).toHaveBeenCalled()
140138
expect(github.pulls.create).toHaveBeenCalled()
141139
expect(github.issues.update).toHaveBeenCalled()
142140
})
@@ -147,7 +145,7 @@ describe('pull - routineCheck', () => {
147145
const pull = getPull()
148146
await pull.routineCheck()
149147
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
150-
expect(github.search.issuesAndPullRequests).not.toHaveBeenCalled()
148+
expect(github.issues.listForRepo).not.toHaveBeenCalled()
151149
expect(github.pulls.create).not.toHaveBeenCalled()
152150
expect(github.issues.update).not.toHaveBeenCalled()
153151
})
@@ -158,7 +156,7 @@ describe('pull - routineCheck', () => {
158156
const pull = getPull()
159157
await pull.routineCheck()
160158
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
161-
expect(github.search.issuesAndPullRequests).not.toHaveBeenCalled()
159+
expect(github.issues.listForRepo).not.toHaveBeenCalled()
162160
expect(github.pulls.create).not.toHaveBeenCalled()
163161
expect(github.issues.update).not.toHaveBeenCalled()
164162
})
@@ -169,66 +167,78 @@ describe('pull - routineCheck', () => {
169167
const pull = getPull()
170168
await pull.routineCheck()
171169
expect(github.repos.compareCommits.mock.calls.length).toBe(3)
172-
expect(github.search.issuesAndPullRequests).not.toHaveBeenCalled()
170+
expect(github.issues.listForRepo).not.toHaveBeenCalled()
173171
expect(github.pulls.create).not.toHaveBeenCalled()
174172
expect(github.issues.update).not.toHaveBeenCalled()
175173
})
176174

177175
test('yes diff, already has PR', async () => {
178176
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
179-
github.search.issuesAndPullRequests.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 12 } ] } })
180-
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 13 } ] } })
181-
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 14 } ] } })
182-
github.pulls.get.mockResolvedValueOnce({ data: {
183-
number: 12,
184-
base: { ref: 'master' },
185-
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder-12' },
186-
state: 'open',
187-
user: { login: 'pull[bot]' },
188-
mergeable: true,
189-
rebaseable: true,
190-
mergeable_state: 'clean'
191-
} }).mockResolvedValueOnce({ data: {
192-
number: 13,
193-
base: { ref: 'feature/new-1' },
194-
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-13' },
195-
state: 'open',
196-
user: { login: 'pull[bot]' },
197-
mergeable: true,
198-
rebaseable: true,
199-
mergeable_state: 'clean'
200-
} }).mockResolvedValueOnce({ data: {
201-
number: 14,
202-
base: { ref: 'hotfix/bug-1' },
203-
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-14' },
204-
state: 'open',
205-
user: { login: 'pull[bot]' },
206-
mergeable: true,
207-
rebaseable: true,
208-
mergeable_state: 'clean'
209-
} })
177+
github.issues.listForRepo.mockResolvedValue({ data: [ { number: 13 }, { number: 12 }, { number: 14 } ] })
178+
github.pulls.get.mockImplementation(({ number }) => {
179+
switch (number) {
180+
case 12:
181+
return { data: {
182+
number: 12,
183+
base: { ref: 'master', label: 'wei:master' },
184+
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder-12' },
185+
state: 'open',
186+
user: { login: 'pull[bot]' },
187+
mergeable: true,
188+
rebaseable: true,
189+
mergeable_state: 'clean'
190+
} }
191+
case 13:
192+
return { data: {
193+
number: 13,
194+
base: { ref: 'feature/new-1', label: 'wei:feature/new-1' },
195+
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-13' },
196+
state: 'open',
197+
user: { login: 'pull[bot]' },
198+
mergeable: true,
199+
rebaseable: true,
200+
mergeable_state: 'clean'
201+
} }
202+
case 14:
203+
return { data: {
204+
number: 14,
205+
base: { ref: 'hotfix/bug-1', label: 'wei:hotfix/bug-1' },
206+
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-14' },
207+
state: 'open',
208+
user: { login: 'pull[bot]' },
209+
mergeable: true,
210+
rebaseable: true,
211+
mergeable_state: 'clean'
212+
} }
213+
default:
214+
return { data: null }
215+
}
216+
})
210217

211218
const pull = getPull()
212219
await pull.routineCheck()
213220
expect(github.repos.compareCommits).nthCalledWith(1, {
214221
owner: 'wei', repo: 'fork', base: 'master', head: 'upstream:master'
215222
})
216-
expect(github.search.issuesAndPullRequests).toHaveBeenCalled()
217-
expect(github.pulls.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 12 })
223+
expect(github.issues.listForRepo).toHaveBeenCalled()
224+
expect(github.pulls.get).nthCalledWith(1, { owner: 'wei', repo: 'fork', number: 13 })
225+
expect(github.pulls.get).nthCalledWith(2, { owner: 'wei', repo: 'fork', number: 12 })
218226
expect(github.pulls.merge).not.toHaveBeenCalledWith()
219227

220228
expect(github.repos.compareCommits).nthCalledWith(2, {
221229
owner: 'wei', repo: 'fork', base: 'feature/new-1', head: 'upstream:dev'
222230
})
223-
expect(github.search.issuesAndPullRequests).toHaveBeenCalled()
224-
expect(github.pulls.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 13 })
231+
expect(github.issues.listForRepo).toHaveBeenCalled()
232+
expect(github.pulls.get).nthCalledWith(3, { owner: 'wei', repo: 'fork', number: 13 })
225233
expect(github.pulls.merge).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 13, merge_method: 'rebase' })
226234

227235
expect(github.repos.compareCommits).nthCalledWith(3, {
228236
owner: 'wei', repo: 'fork', base: 'hotfix/bug-1', head: 'upstream:dev'
229237
})
230-
expect(github.search.issuesAndPullRequests).toHaveBeenCalled()
231-
expect(github.pulls.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 14 })
238+
expect(github.issues.listForRepo).toHaveBeenCalled()
239+
expect(github.pulls.get).nthCalledWith(4, { owner: 'wei', repo: 'fork', number: 13 })
240+
expect(github.pulls.get).nthCalledWith(5, { owner: 'wei', repo: 'fork', number: 12 })
241+
expect(github.pulls.get).nthCalledWith(6, { owner: 'wei', repo: 'fork', number: 14 })
232242
expect(github.git.updateRef).toHaveBeenCalledWith(
233243
{ owner: 'wei', repo: 'fork', ref: `heads/hotfix/bug-1`, sha: 'sha1-placeholder-14', force: true }
234244
)
@@ -239,13 +249,13 @@ describe('pull - routineCheck', () => {
239249

240250
test('yes diff, no PR, create PR', async () => {
241251
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
242-
github.search.issuesAndPullRequests
243-
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 10 } ] } })
244-
.mockResolvedValueOnce({ data: { total_count: 0 } })
245-
.mockResolvedValueOnce({ data: { total_count: 0 } })
252+
github.issues.listForRepo
253+
.mockResolvedValueOnce({ data: [ { number: 10 } ] })
254+
.mockResolvedValueOnce({ data: [] })
255+
.mockResolvedValueOnce({ data: [] })
246256
github.pulls.get.mockResolvedValueOnce({ data: {
247257
number: 10,
248-
base: { ref: 'master' },
258+
base: { ref: 'master', label: 'wei:master' },
249259
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder' },
250260
state: 'open',
251261
user: { login: 'pull[bot]' },
@@ -268,7 +278,7 @@ describe('pull - routineCheck', () => {
268278
expect(github.repos.compareCommits).nthCalledWith(3, {
269279
owner: 'wei', repo: 'fork', base: 'hotfix/bug-1', head: 'upstream:dev'
270280
})
271-
expect(github.search.issuesAndPullRequests).toHaveBeenCalledTimes(3)
281+
expect(github.issues.listForRepo).toHaveBeenCalledTimes(3)
272282
expect(github.pulls.create).toHaveBeenCalledTimes(2)
273283
expect(github.pulls.create).nthCalledWith(1, {
274284
owner: 'wei', repo: 'fork', base: 'feature/new-1', head: 'upstream:dev', maintainer_can_modify: false, title: helper.getPRTitle('feature/new-1', 'upstream:dev'), body: helper.getPRBody('wei/fork')

0 commit comments

Comments
 (0)