Skip to content

Commit

Permalink
🐛 Check app/pull PRs on routine check, merge if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
wei committed Jul 11, 2018
1 parent a0d03c5 commit 050e28d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
27 changes: 17 additions & 10 deletions lib/pull.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ module.exports = class Pull {
this.logger.debug(`[${this.config.repoPath}] ${base} is same as ${upstream}`)
} else if (!(await this.hasDiff(base, upstream))) {
this.logger.info(`[${this.config.repoPath}] ${base} is in sync with ${upstream}`)
} else if (await this.hasOpenPR(base)) {
this.logger.info(`[${this.config.repoPath}] Already has PR from ${upstream} to ${base}`)
} else {
this.logger.info(`[${this.config.repoPath}] Creating PR from ${upstream} to ${base}`)
await this.createPR(base, upstream, assignees, reviewers)
const openPR = await this.getOpenPR(base) // Get one PR opened by app/pull
if (openPR) {
this.logger.info(`[${this.config.repoPath}] Found a PR from ${upstream} to ${base}`)
await this.checkAutoMerge(openPR)
} else {
this.logger.info(`[${this.config.repoPath}] Creating PR from ${upstream} to ${base}`)
await this.createPR(base, upstream, assignees, reviewers)
}
}
}
}
Expand Down Expand Up @@ -85,13 +89,17 @@ module.exports = class Pull {
return comparison.data.total_commits > 0
}

async hasOpenPR (base) {
async getOpenPR (base) {
const res = await this.github.search.issues({
q: `repo:${this.config.repoPath} type:pr is:open base:${base} author:app/pull`,
per_page: 1, // Cannot be 0 because it will fallback to default: 30
per_page: 1,
page: 1
})
return res.data.total_count > 0
return res.data.total_count > 0 ? this.github.pullRequests.get({
owner: this.config.owner,
repo: this.config.repo,
number: res.data.items[0].number
}) : null
}

async createPR (base, upstream, assignees, reviewers) {
Expand Down Expand Up @@ -151,13 +159,12 @@ module.exports = class Pull {
})
}

async mergePR (number, method) {
async mergePR (number) {
if (!number) return null
return this.github.pullRequests.merge({
owner: this.config.owner,
repo: this.config.repo,
number,
merge_method: method
number
})
}

Expand Down
53 changes: 51 additions & 2 deletions test/pull.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,79 @@ describe('pull - routineCheck', () => {

test('yes diff, already has PR', async () => {
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
github.search.issues.mockResolvedValue({ data: { total_count: 1 } })
github.search.issues.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 12 } ] } })
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 13 } ] } })
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 14 } ] } })
github.pullRequests.get.mockResolvedValueOnce({
number: 12,
base: { ref: 'master' },
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder-12' },
state: 'open',
user: { login: 'pull[bot]' },
mergeable: true,
mergeable_state: 'clean'
}).mockResolvedValueOnce({
number: 13,
base: { ref: 'feature/new-1' },
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-13' },
state: 'open',
user: { login: 'pull[bot]' },
mergeable: true,
mergeable_state: 'clean'
}).mockResolvedValueOnce({
number: 14,
base: { ref: 'hotfix/bug-1' },
head: { ref: 'dev', label: 'upstream:dev', sha: 'sha1-placeholder-14' },
state: 'open',
user: { login: 'pull[bot]' },
mergeable: true,
mergeable_state: 'clean'
})

const pull = getPull()
await pull.routineCheck()
expect(github.repos.compareCommits).nthCalledWith(1, {
owner: 'wei', repo: 'fork', base: 'master', head: 'upstream:master'
})
expect(github.search.issues).toHaveBeenCalled()
expect(github.pullRequests.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 12 })
expect(github.pullRequests.merge).not.toHaveBeenCalledWith()

expect(github.repos.compareCommits).nthCalledWith(2, {
owner: 'wei', repo: 'fork', base: 'feature/new-1', head: 'upstream:dev'
})
expect(github.search.issues).toHaveBeenCalled()
expect(github.pullRequests.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 13 })
expect(github.pullRequests.merge).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 13 })

expect(github.repos.compareCommits).nthCalledWith(3, {
owner: 'wei', repo: 'fork', base: 'hotfix/bug-1', head: 'upstream:dev'
})
expect(github.search.issues).toHaveBeenCalled()
expect(github.pullRequests.get).toHaveBeenCalledWith({ owner: 'wei', repo: 'fork', number: 14 })
expect(github.gitdata.updateReference).toHaveBeenCalledWith(
{ owner: 'wei', repo: 'fork', ref: `heads/hotfix/bug-1`, sha: 'sha1-placeholder-14', force: true }
)

expect(github.pullRequests.create).not.toHaveBeenCalled()
expect(github.issues.edit).not.toHaveBeenCalled()
})

test('yes diff, no PR, create PR', async () => {
github.repos.compareCommits.mockResolvedValue({ data: { total_commits: 1 } })
github.search.issues
.mockResolvedValueOnce({ data: { total_count: 1 } })
.mockResolvedValueOnce({ data: { total_count: 1, items: [ { number: 10 } ] } })
.mockResolvedValueOnce({ data: { total_count: 0 } })
.mockResolvedValueOnce({ data: { total_count: 0 } })
github.pullRequests.get.mockResolvedValueOnce({
number: 10,
base: { ref: 'master' },
head: { ref: 'master', label: 'upstream:master', sha: 'sha1-placeholder' },
state: 'open',
user: { login: 'pull[bot]' },
mergeable: true,
mergeable_state: 'clean'
})
github.pullRequests.create
.mockResolvedValueOnce({ data: { number: 12 } })
.mockResolvedValueOnce({ data: { number: 16 } })
Expand Down

0 comments on commit 050e28d

Please sign in to comment.