Skip to content

Commit

Permalink
Try extra release pages without Link header
Browse files Browse the repository at this point in the history
A change to the GitHub API recently removed the pagination Link header
from release json requests. This breaks the simple pagination handling
we use to find all possible releases.

Rather than relying on this to know if there are more pages to load, we
can just keep loading pages until they provide no additional versions to
parse.
  • Loading branch information
jwlawson committed Jan 19, 2021
1 parent 0a4250e commit 5cfae9c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
94 changes: 55 additions & 39 deletions __tests__/version.test.ts
Expand Up @@ -17,8 +17,6 @@ describe('When a version is needed', () => {
.query({ page: 1 })
.replyWithFile(200, path.join(dataPath, 'releases.json'), {
'Content-Type': 'application/json',
Link:
'<...releases?page=2>; rel="next", <...releases?page=2>; rel="last"',
});
// Releases file 2 contains version info for:
// 2.4.8, 2.6.4, 2.8.10.2, 2.8.12.2
Expand All @@ -29,9 +27,11 @@ describe('When a version is needed', () => {
.query({ page: 2 })
.replyWithFile(200, path.join(dataPath, 'releases2.json'), {
'Content-Type': 'application/json',
Link:
'<...releases?page=1>; rel="prev", <...releases?page=2>; rel="last"',
});
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 3 })
.reply(200, []);
});
afterEach(() => {
nock.cleanAll();
Expand Down Expand Up @@ -102,6 +102,10 @@ describe('When api token is required', () => {
.get('/repos/Kitware/CMake/releases')
.query({ page: 1 })
.replyWithError('Invalid API token');
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.reply(200, []);
});
afterEach(() => {
nock.cleanAll();
Expand All @@ -124,33 +128,39 @@ describe('When api token is required', () => {
});

describe('When using macos 3.19.2 release', () => {
const releases = {
tag_name: 'v3.19.2',
assets: [
{
name: 'cmake-3.19.2-Linux-x86_64.tar.gz',
browser_download_url:
'https://fakeaddress/cmake-3.19.2-Linux-x86_64.tar.gz',
},
{
name: 'cmake-3.19.2-macos-universal.dmg',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.2-macos-universal.dmg',
},
{
name: 'cmake-3.19.2-macos-universal.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.2-macos-universal.tar.gz',
},
],
};
const releases = [
{
tag_name: 'v3.19.2',
assets: [
{
name: 'cmake-3.19.2-Linux-x86_64.tar.gz',
browser_download_url:
'https://fakeaddress/cmake-3.19.2-Linux-x86_64.tar.gz',
},
{
name: 'cmake-3.19.2-macos-universal.dmg',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.2-macos-universal.dmg',
},
{
name: 'cmake-3.19.2-macos-universal.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.2-macos-universal.tar.gz',
},
],
},
];

beforeEach(() => {
nock.disableNetConnect();
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 1 })
.reply(200, releases);
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.reply(200, []);
});

afterEach(() => {
Expand Down Expand Up @@ -184,28 +194,34 @@ describe('When using macos 3.19.2 release', () => {
});

describe('When providing multiple different archs', () => {
const releases = {
tag_name: 'v3.19.3',
assets: [
{
name: 'cmake-3.19.3-Linux-aarch64.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.3-Linux-aarch64.tar.gz',
},
{
name: 'cmake-3.19.3-Linux-x86_64.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.3-Linux-x86_64.tar.gz',
},
],
};
const releases = [
{
tag_name: 'v3.19.3',
assets: [
{
name: 'cmake-3.19.3-Linux-aarch64.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.3-Linux-aarch64.tar.gz',
},
{
name: 'cmake-3.19.3-Linux-x86_64.tar.gz',
browser_download_url:
'https://fakeaddress.com/cmake-3.19.3-Linux-x86_64.tar.gz',
},
],
},
];

beforeEach(() => {
nock.disableNetConnect();
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 1 })
.reply(200, releases);
nock('https://api.github.com')
.get('/repos/Kitware/CMake/releases')
.query({ page: 2 })
.reply(200, []);
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions src/version.ts
@@ -1,4 +1,5 @@
import * as rest from 'typed-rest-client/RestClient';
import * as core from '@actions/core';
import * as semver from 'semver';
import * as vi from './version-info';

Expand Down Expand Up @@ -113,24 +114,22 @@ export async function getAllVersionInfo(
const client = new rest.RestClient(USER_AGENT);
let cur_page = 1;
let raw_versions: GitHubVersion[] = [];
let has_next_page = true;
while (has_next_page) {
while (cur_page < 10) {
const options = getHttpOptions(api_token, cur_page);
core.debug(`fetching page ${cur_page}`);
const version_response = await client.get<GitHubVersion[]>(
VERSION_URL,
options
);
const headers: { link?: string } = version_response.headers;
if (headers.link && headers.link.match(/rel="next"/)) {
has_next_page = true;
} else {
has_next_page = false;
}
if (version_response.result) {
if (version_response.result && version_response.result.length > 0) {
core.debug(`found ${version_response.result.length} results`);
raw_versions = raw_versions.concat(version_response.result);
} else {
break;
}
cur_page++;
}
core.debug(`overall got ${raw_versions.length} versions`);
const versions: vi.VersionInfo[] = convertToVersionInfo(raw_versions);
return versions;
}
Expand Down

0 comments on commit 5cfae9c

Please sign in to comment.