Skip to content

Commit ad3903c

Browse files
authored
chore: update @octokit/rest from v16 to v17 (#371)
* chore: replace deprecated .hasLastPage() with pagination This is a prerequisite for updating @octokit/rest. * chore: update @octokit/rest from v16 to v17
1 parent ae642ed commit ad3903c

10 files changed

Lines changed: 8329 additions & 370 deletions

lib/push-jenkins-update.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,16 @@ function findPrInRef (gitRef) {
7777
return parseInt(gitRef.split('/')[2], 10)
7878
}
7979

80-
async function findLatestCommitInPr (options, pageNumber = 1) {
81-
const res = await githubClient.pulls.listCommits({
80+
async function findLatestCommitInPr (options) {
81+
const { Octokit } = require('@octokit/rest')
82+
const githubClient = new Octokit({})
83+
const paginateOptions = githubClient.pulls.listCommits.endpoint.merge({
8284
owner: options.owner,
8385
repo: options.repo,
84-
pull_number: options.pr,
85-
page: pageNumber,
86-
per_page: 100
86+
pull_number: options.pr
8787
})
88+
const commitMetas = await githubClient.paginate(paginateOptions)
8889

89-
const commitMetas = res.data || []
90-
const lastPageURL = githubClient.hasLastPage(res)
91-
if (lastPageURL) {
92-
return findLatestCommitInPr(options, pageNumberFromURL(lastPageURL))
93-
}
9490
const lastCommitMeta = commitMetas.pop()
9591
const lastCommit = {
9692
sha: lastCommitMeta.sha,
@@ -123,10 +119,6 @@ function validate (payload) {
123119
return ['identifier', 'status', 'message', 'commit', 'url'].every(isString)
124120
}
125121

126-
function pageNumberFromURL (githubUrl) {
127-
return new URL(githubUrl).searchParams.get('page')
128-
}
129-
130122
exports.validate = validate
131123
exports.pushStarted = pushStarted
132124
exports.pushEnded = pushEnded

package-lock.json

Lines changed: 129 additions & 186 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"private": true,
1414
"license": "MIT",
1515
"dependencies": {
16-
"@octokit/rest": "^16.43.2",
16+
"@octokit/rest": "^17.11.2",
1717
"aigle": "^1.14.1",
1818
"async": "3.2.4",
1919
"basic-auth": "^2.0.1",

test/_fixtures/pull-request-commits-page-1.json

Lines changed: 2335 additions & 0 deletions
Large diffs are not rendered by default.

test/_fixtures/pull-request-commits-page-2.json

Lines changed: 2373 additions & 0 deletions
Large diffs are not rendered by default.

test/_fixtures/pull-request-commits-page-3.json

Lines changed: 2354 additions & 0 deletions
Large diffs are not rendered by default.

test/_fixtures/pull-request-commits-page-4.json

Lines changed: 1109 additions & 0 deletions
Large diffs are not rendered by default.

test/_fixtures/pull-requests-commits-page-1.json

Lines changed: 0 additions & 78 deletions
This file was deleted.

test/_fixtures/pull-requests-commits-page-104.json

Lines changed: 0 additions & 78 deletions
This file was deleted.

test/unit/push-jenkins-update.test.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,41 @@ const { findLatestCommitInPr } = require('../../lib/push-jenkins-update')
44

55
const nock = require('nock')
66
const readFixture = require('../read-fixture')
7-
const { ignoreQueryParams } = require('../common')
87

98
tap.test('findLatestCommitInPr: paginates results when more than 100 commits in a PR', async (t) => {
10-
const commitsFixturePage1 = readFixture('pull-requests-commits-page-1.json')
11-
const commitsFixturePage104 = readFixture('pull-requests-commits-page-104.json')
9+
const commitsFixturePage1 = readFixture('pull-request-commits-page-1.json')
10+
const commitsFixturePage2 = readFixture('pull-request-commits-page-2.json')
11+
const commitsFixturePage3 = readFixture('pull-request-commits-page-3.json')
12+
const commitsFixturePage4 = readFixture('pull-request-commits-page-4.json')
1213
const owner = 'nodejs'
1314
const repo = 'node'
1415
const pr = 9745
1516

16-
const headers = {
17-
Link: '<https://api.github.com/repos/nodejs/node/pulls/9745/commits?page=104>; rel="last"'
18-
}
1917
const firstPageScope = nock('https://api.github.com')
20-
.filteringPath(ignoreQueryParams)
2118
.get(`/repos/${owner}/${repo}/pulls/${pr}/commits`)
22-
.reply(200, commitsFixturePage1, headers)
19+
.reply(200, commitsFixturePage1, { link: '<https://api.github.com/repositories/27193779/pulls/9745/commits?page=2>; rel="next", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=4>; rel="last"' })
2320

24-
const lastPageScope = nock('https://api.github.com')
25-
.get(`/repos/${owner}/${repo}/pulls/${pr}/commits`)
26-
.query({ page: 104, per_page: 100 })
27-
.reply(200, commitsFixturePage104)
21+
const secondPageScope = nock('https://api.github.com')
22+
.get(`/repositories/27193779/pulls/${pr}/commits`)
23+
.query({ page: 2 })
24+
.reply(200, commitsFixturePage2, { link: '<https://api.github.com/repositories/27193779/pulls/9745/commits?page=1>; rel="prev", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=3>; rel="next", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=4>; rel="last", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=1>; rel="first"' })
25+
26+
const thirdPageScope = nock('https://api.github.com')
27+
.get(`/repositories/27193779/pulls/${pr}/commits`)
28+
.query({ page: 3 })
29+
.reply(200, commitsFixturePage3, { link: '<https://api.github.com/repositories/27193779/pulls/9745/commits?page=2>; rel="prev", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=4>; rel="next", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=4>; rel="last", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=1>; rel="first"' })
30+
31+
const fourthPageScope = nock('https://api.github.com')
32+
.get(`/repositories/27193779/pulls/${pr}/commits`)
33+
.query({ page: 4 })
34+
.reply(200, commitsFixturePage4, { link: '<https://api.github.com/repositories/27193779/pulls/9745/commits?page=3>; rel="prev", <https://api.github.com/repositories/27193779/pulls/9745/commits?page=1>; rel="first"' })
2835

2936
t.plan(1)
3037

3138
const commit = await findLatestCommitInPr({ owner, repo, pr })
3239
t.equal(commit.sha, 'c1aa949064892dbe693750686c06f4ad5673e577')
3340
firstPageScope.done()
34-
lastPageScope.done()
41+
secondPageScope.done()
42+
thirdPageScope.done()
43+
fourthPageScope.done()
3544
})

0 commit comments

Comments
 (0)