Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GraalPy support #694

Merged
merged 18 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Utilize pagination when querying GraalPy GitHub releases
  • Loading branch information
msimacek committed Aug 29, 2023
commit feb18948f530cf02720fccad3fa9165754be6666
25 changes: 24 additions & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
isCacheFeatureAvailable,
getVersionInputFromFile,
getVersionInputFromPlainFile,
getVersionInputFromTomlFile
getVersionInputFromTomlFile,
getNextPageUrl
} from '../src/utils';

jest.mock('@actions/cache');
Expand Down Expand Up @@ -136,3 +137,25 @@ describe('Version from file test', () => {
}
);
});

describe('getNextPageUrl', () => {
it('GitHub API pagination next page is parsed correctly', () => {
function generateResponse(link: string) {
return {
statusCode: 200,
result: null,
headers: {
link: link
}
};
}
const page1Links =
'<https://api.github.com/repositories/129883600/releases?page=2>; rel="next", <https://api.github.com/repositories/129883600/releases?page=3>; rel="last"';
expect(getNextPageUrl(generateResponse(page1Links))).toStrictEqual(
'https://api.github.com/repositories/129883600/releases?page=2'
);
const page2Links =
'<https://api.github.com/repositories/129883600/releases?page=1>; rel="prev", <https://api.github.com/repositories/129883600/releases?page=1>; rel="first"';
expect(getNextPageUrl(generateResponse(page2Links))).toBeNull();
});
});
26 changes: 17 additions & 9 deletions src/install-graalpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
IGraalPyManifestRelease,
createSymlinkInFolder,
isNightlyKeyword,
getBinaryDirectory
getBinaryDirectory,
getNextPageUrl
} from './utils';

const TOKEN = core.getInput('token');
Expand Down Expand Up @@ -106,22 +107,29 @@ export async function installGraalPy(
}

export async function getAvailableGraalPyVersions() {
timfel marked this conversation as resolved.
Show resolved Hide resolved
const url = 'https://api.github.com/repos/oracle/graalpython/releases';
const http: httpm.HttpClient = new httpm.HttpClient('tool-cache');
timfel marked this conversation as resolved.
Show resolved Hide resolved

let headers: ifm.IHeaders = {};
if (AUTH) {
headers.authorization = AUTH;
}

const response = await http.getJson<IGraalPyManifestRelease[]>(url, headers);
if (!response.result) {
throw new Error(
`Unable to retrieve the list of available GraalPy versions from '${url}'`
);
}
let url: string | null =
'https://api.github.com/repos/oracle/graalpython/releases';
const result: IGraalPyManifestRelease[] = [];
do {
const response: ifm.ITypedResponse<IGraalPyManifestRelease[]> =
await http.getJson(url, headers);
if (!response.result) {
throw new Error(
`Unable to retrieve the list of available GraalPy versions from '${url}'`
);
}
result.push(...response.result);
url = getNextPageUrl(response);
} while (url);

return response.result;
return result;
}

async function createGraalPySymlink(
Expand Down
23 changes: 23 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as path from 'path';
import * as semver from 'semver';
import * as toml from '@iarna/toml';
import * as exec from '@actions/exec';
import * as ifm from '@actions/http-client/interfaces';

export const IS_WINDOWS = process.platform === 'win32';
export const IS_LINUX = process.platform === 'linux';
Expand Down Expand Up @@ -271,3 +272,25 @@ export function getVersionInputFromFile(versionFile: string): string[] {
export function getBinaryDirectory(installDir: string) {
return IS_WINDOWS ? installDir : path.join(installDir, 'bin');
}

/**
* Extract next page URL from a HTTP response "link" header. Such headers are used in GitHub APIs.
*/
export function getNextPageUrl<T>(response: ifm.ITypedResponse<T>) {
const responseHeaders = <ifm.IHeaders>response.headers;
const linkHeader = responseHeaders.link;
if (typeof linkHeader === 'string') {
for (let link of linkHeader.split(/\s*,\s*/)) {
const match = link.match(/<([^>]+)>(.*)/);
if (match) {
const url = match[1];
for (let param of match[2].split(/\s*;\s*/)) {
if (param.match(/rel="?next"?/)) {
return url;
}
}
}
}
}
return null;
}