Skip to content

Commit d5071ac

Browse files
committed
Add fallback for the manifest URL
1 parent 9c22be2 commit d5071ac

3 files changed

Lines changed: 35 additions & 16 deletions

File tree

__tests__/utils.test.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'fs';
22
import * as path from 'path';
33
import * as utils from '../src/utils';
4+
import * as fetchModule from '../src/fetch';
45

56
/**
67
* Mock @actions/core
@@ -12,15 +13,6 @@ jest.mock('@actions/core', () => ({
1213
info: jest.fn()
1314
}));
1415

15-
/**
16-
* Mock fetch.ts
17-
*/
18-
jest.mock('../src/fetch', () => ({
19-
fetch: jest.fn().mockImplementation(() => {
20-
return {data: '{ "latest": "8.1", "5.x": "5.6" }'};
21-
})
22-
}));
23-
2416
describe('Utils tests', () => {
2517
it('checking readEnv', async () => {
2618
process.env['test'] = 'setup-php';
@@ -43,15 +35,35 @@ describe('Utils tests', () => {
4335
});
4436

4537
it('checking getManifestURL', async () => {
46-
expect(await utils.getManifestURL()).toContain('php-versions.json');
38+
for (const url of await utils.getManifestURLS()) {
39+
expect(url).toContain('php-versions.json');
40+
}
4741
});
4842

4943
it('checking parseVersion', async () => {
44+
jest
45+
.spyOn(utils, 'getManifestURLS')
46+
.mockResolvedValue([
47+
'https://u1/versions.json',
48+
'https://u2/versions.json'
49+
]);
50+
const fetchSpy = jest
51+
.spyOn(fetchModule, 'fetch')
52+
.mockResolvedValue({data: '{ "latest": "8.1", "5.x": "5.6" }'});
5053
expect(await utils.parseVersion('latest')).toBe('8.1');
5154
expect(await utils.parseVersion('7')).toBe('7.0');
5255
expect(await utils.parseVersion('7.4')).toBe('7.4');
5356
expect(await utils.parseVersion('5.x')).toBe('5.6');
5457
expect(await utils.parseVersion('4.x')).toBe(undefined);
58+
59+
fetchSpy.mockReset();
60+
fetchSpy.mockResolvedValueOnce({}).mockResolvedValueOnce({});
61+
62+
await expect(utils.parseVersion('latest')).rejects.toThrow(
63+
'Could not fetch the PHP version manifest.'
64+
);
65+
66+
expect(fetchSpy).toHaveBeenCalledTimes(2);
5567
});
5668

5769
it('checking parseIniFile', async () => {

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ export async function getInput(
4848
/**
4949
* Function to get manifest URL
5050
*/
51-
export async function getManifestURL(): Promise<string> {
52-
return 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json';
51+
export async function getManifestURLS(): Promise<string[]> {
52+
return [
53+
'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json',
54+
'https://setup-php.com/php-versions.json'
55+
];
5356
}
5457

5558
/**
@@ -60,9 +63,13 @@ export async function getManifestURL(): Promise<string> {
6063
export async function parseVersion(version: string): Promise<string> {
6164
switch (true) {
6265
case /^(latest|lowest|highest|nightly|\d+\.x)$/.test(version):
63-
return JSON.parse((await fetch.fetch(await getManifestURL()))['data'])[
64-
version
65-
];
66+
for (const manifestURL of await getManifestURLS()) {
67+
const fetchResult = await fetch.fetch(manifestURL);
68+
if (fetchResult['data'] ?? false) {
69+
return JSON.parse(fetchResult['data'])[version];
70+
}
71+
}
72+
throw new Error(`Could not fetch the PHP version manifest.`);
6673
default:
6774
switch (true) {
6875
case version.length > 1:

0 commit comments

Comments
 (0)