Skip to content

Commit 050e28d

Browse files
committed
🐛 Check app/pull PRs on routine check, merge if necessary
#16
1 parent a0d03c5 commit 050e28d

2 files changed

Lines changed: 68 additions & 12 deletions

File tree

‎lib/pull.js‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ module.exports = class Pull {
2626
this.logger.debug(`[${this.config.repoPath}] ${base} is same as ${upstream}`)
2727
} else if (!(await this.hasDiff(base, upstream))) {
2828
this.logger.info(`[${this.config.repoPath}] ${base} is in sync with ${upstream}`)
29-
} else if (await this.hasOpenPR(base)) {
30-
this.logger.info(`[${this.config.repoPath}] Already has PR from ${upstream} to ${base}`)
3129
} else {
32-
this.logger.info(`[${this.config.repoPath}] Creating PR from ${upstream} to ${base}`)
33-
await this.createPR(base, upstream, assignees, reviewers)
30+
const openPR = await this.getOpenPR(base) // Get one PR opened by app/pull
31+
if (openPR) {
32+
this.logger.info(`[${this.config.repoPath}] Found a PR from ${upstream} to ${base}`)
33+
await this.checkAutoMerge(openPR)
34+
} else {
35+
this.logger.info(`[${this.config.repoPath}] Creating PR from ${upstream} to ${base}`)
36+
await this.createPR(base, upstream, assignees, reviewers)
37+
}
3438
}
3539
}
3640
}
@@ -85,13 +89,17 @@ module.exports = class Pull {
8589
return comparison.data.total_commits > 0
8690
}
8791

88-
async hasOpenPR (base) {
92+
async getOpenPR (base) {
8993
const res = await this.github.search.issues({
9094
q: `repo:${this.config.repoPath} type:pr is:open base:${base} author:app/pull`,
91-
per_page: 1, // Cannot be 0 because it will fallback to default: 30
95+
per_page: 1,
9296
page: 1
9397
})
94-
return res.data.total_count > 0
98+
return res.data.total_count > 0 ? this.github.pullRequests.get({
99+
owner: this.config.owner,
100+
repo: this.config.repo,
101+
number: res.data.items[0].number
102+
}) : null
95103
}
96104

97105
async createPR (base, upstream, assignees, reviewers) {
@@ -151,13 +159,12 @@ module.exports = class Pull {
151159
})
152160
}
153161

154-
async mergePR (number, method) {
162+
async mergePR (number) {
155163
if (!number) return null
156164
return this.github.pullRequests.merge({
157165
owner: this.config.owner,
158166
repo: this.config.repo,
159-
number,
160-
merge_method: method
167+
number
161168
})
162169
}
163170

‎test/pull.test.js‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,79 @@ describe('pull - routineCheck', () => {
124124

125125
test('yes diff, already has PR', async () => {
126126
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
127-
github.search.issues.mockResolvedValue({ data: { total_count: 1 } })
127+
github.search.issues.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 12 } ] } })
128+
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 13 } ] } })
129+
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 14 } ] } })
130+
github.pullRequests.get.mockResolvedValueOnce({
131+
number: 12,
132+
base: { ref: 'master' },
133+
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder-12' },
134+
state: 'open',
135+
user: { login: 'pull[bot]' },
136+
mergeable: true,
137+
mergeable_state: 'clean'
138+
}).mockResolvedValueOnce({
139+
number: 13,
140+
base: { ref: 'feature/new-1' },
141+
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-13' },
142+
state: 'open',
143+
user: { login: 'pull[bot]' },
144+
mergeable: true,
145+
mergeable_state: 'clean'
146+
}).mockResolvedValueOnce({
147+
number: 14,
148+
base: { ref: 'hotfix/bug-1' },
149+
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-14' },
150+
state: 'open',
151+
user: { login: 'pull[bot]' },
152+
mergeable: true,
153+
mergeable_state: 'clean'
154+
})
128155

129156
const pull = getPull()
130157
await pull.routineCheck()
131158
expect(github.repos.compareCommits).nthCalledWith(1, {
132159
owner: 'wei', repo: 'fork', base: 'master', head: 'upstream:master'
133160
})
161+
expect(github.search.issues).toHaveBeenCalled()
162+
expect(github.pullRequests.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 12 })
163+
expect(github.pullRequests.merge).not.toHaveBeenCalledWith()
164+
134165
expect(github.repos.compareCommits).nthCalledWith(2, {
135166
owner: 'wei', repo: 'fork', base: 'feature/new-1', head: 'upstream:dev'
136167
})
168+
expect(github.search.issues).toHaveBeenCalled()
169+
expect(github.pullRequests.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 13 })
170+
expect(github.pullRequests.merge).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 13 })
171+
137172
expect(github.repos.compareCommits).nthCalledWith(3, {
138173
owner: 'wei', repo: 'fork', base: 'hotfix/bug-1', head: 'upstream:dev'
139174
})
140175
expect(github.search.issues).toHaveBeenCalled()
176+
expect(github.pullRequests.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 14 })
177+
expect(github.gitdata.updateReference).toHaveBeenCalledWith(
178+
{ owner: 'wei', repo: 'fork', ref: `heads/hotfix/bug-1`, sha: 'sha1-placeholder-14', force: true }
179+
)
180+
141181
expect(github.pullRequests.create).not.toHaveBeenCalled()
142182
expect(github.issues.edit).not.toHaveBeenCalled()
143183
})
144184

145185
test('yes diff, no PR, create PR', async () => {
146186
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
147187
github.search.issues
148-
.mockResolvedValueOnce({ data: { total_count: 1 } })
188+
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 10 } ] } })
149189
.mockResolvedValueOnce({ data: { total_count: 0 } })
150190
.mockResolvedValueOnce({ data: { total_count: 0 } })
191+
github.pullRequests.get.mockResolvedValueOnce({
192+
number: 10,
193+
base: { ref: 'master' },
194+
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder' },
195+
state: 'open',
196+
user: { login: 'pull[bot]' },
197+
mergeable: true,
198+
mergeable_state: 'clean'
199+
})
151200
github.pullRequests.create
152201
.mockResolvedValueOnce({ data: { number: 12 } })
153202
.mockResolvedValueOnce({ data: { number: 16 } })

0 commit comments

Comments
 (0)