Add fallback for the manifest URL

This commit is contained in:
Shivam Mathur 2025-05-19 08:09:13 +05:30
parent 9c22be2b20
commit b595b415e2
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
3 changed files with 27 additions and 16 deletions

View File

@ -1,6 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as utils from '../src/utils'; import * as utils from '../src/utils';
import * as fetchModule from '../src/fetch';
/** /**
* Mock @actions/core * Mock @actions/core
@ -12,15 +13,6 @@ jest.mock('@actions/core', () => ({
info: jest.fn() info: jest.fn()
})); }));
/**
* Mock fetch.ts
*/
jest.mock('../src/fetch', () => ({
fetch: jest.fn().mockImplementation(() => {
return {data: '{ "latest": "8.1", "5.x": "5.6" }'};
})
}));
describe('Utils tests', () => { describe('Utils tests', () => {
it('checking readEnv', async () => { it('checking readEnv', async () => {
process.env['test'] = 'setup-php'; process.env['test'] = 'setup-php';
@ -43,15 +35,27 @@ describe('Utils tests', () => {
}); });
it('checking getManifestURL', async () => { it('checking getManifestURL', async () => {
expect(await utils.getManifestURL()).toContain('php-versions.json'); for (const url of await utils.getManifestURLS()) {
expect(url).toContain('php-versions.json');
}
}); });
it('checking parseVersion', async () => { it('checking parseVersion', async () => {
const fetchSpy = jest
.spyOn(fetchModule, 'fetch')
.mockResolvedValue({data: '{ "latest": "8.1", "5.x": "5.6" }'});
expect(await utils.parseVersion('latest')).toBe('8.1'); expect(await utils.parseVersion('latest')).toBe('8.1');
expect(await utils.parseVersion('7')).toBe('7.0'); expect(await utils.parseVersion('7')).toBe('7.0');
expect(await utils.parseVersion('7.4')).toBe('7.4'); expect(await utils.parseVersion('7.4')).toBe('7.4');
expect(await utils.parseVersion('5.x')).toBe('5.6'); expect(await utils.parseVersion('5.x')).toBe('5.6');
expect(await utils.parseVersion('4.x')).toBe(undefined); expect(await utils.parseVersion('4.x')).toBe(undefined);
fetchSpy.mockReset();
fetchSpy.mockResolvedValueOnce({}).mockResolvedValueOnce({});
await expect(utils.parseVersion('latest')).rejects.toThrow(
'Could not fetch the PHP version manifest.'
);
expect(fetchSpy).toHaveBeenCalledTimes(2);
}); });
it('checking parseIniFile', async () => { it('checking parseIniFile', async () => {

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -48,8 +48,11 @@ export async function getInput(
/** /**
* Function to get manifest URL * Function to get manifest URL
*/ */
export async function getManifestURL(): Promise<string> { export async function getManifestURLS(): Promise<string[]> {
return 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json'; return [
'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json',
'https://setup-php.com/php-versions.json'
];
} }
/** /**
@ -60,9 +63,13 @@ export async function getManifestURL(): Promise<string> {
export async function parseVersion(version: string): Promise<string> { export async function parseVersion(version: string): Promise<string> {
switch (true) { switch (true) {
case /^(latest|lowest|highest|nightly|\d+\.x)$/.test(version): case /^(latest|lowest|highest|nightly|\d+\.x)$/.test(version):
return JSON.parse((await fetch.fetch(await getManifestURL()))['data'])[ for (const manifestURL of await getManifestURLS()) {
version const fetchResult = await fetch.fetch(manifestURL);
]; if (fetchResult['data'] ?? false) {
return JSON.parse(fetchResult['data'])[version];
}
}
throw new Error(`Could not fetch the PHP version manifest.`);
default: default:
switch (true) { switch (true) {
case version.length > 1: case version.length > 1: