From 6ea444fd3a66703f4acda738a1cc5a7fcecc1a4e Mon Sep 17 00:00:00 2001 From: Marcel Kuhmann Date: Wed, 29 Mar 2023 22:50:58 +0200 Subject: [PATCH] fallback to normale package listing if request failed --- __tests__/myRedaxo.test.ts | 11 ++++++++--- src/index.ts | 4 ++-- src/myRedaxo.ts | 21 +++++++++++++++------ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/__tests__/myRedaxo.test.ts b/__tests__/myRedaxo.test.ts index 932ef3c..74325bd 100644 --- a/__tests__/myRedaxo.test.ts +++ b/__tests__/myRedaxo.test.ts @@ -1,16 +1,21 @@ -import {fetchAddonPackageYml, versionExists} from "../src/myRedaxo"; +import {fetchAddonPackageData, versionExists} from "../src/myRedaxo"; const testMyRedaxoPackage = require('./data/myRedaxoPackage.json'); describe('myRedaxo', () => { describe('fetchAddon', () => { test('should error with 404', async () => { - const packageYml = await fetchAddonPackageYml('non_existing_addon'); + const packageYml = await fetchAddonPackageData('non_existing_addon'); expect(packageYml).toBe(null); }); test('should return addon data', async () => { - const packageYml = await fetchAddonPackageYml('yform'); + const packageYml = await fetchAddonPackageData('yform'); + + expect(packageYml).toHaveProperty('name'); + }); + test('should use fallback', async () => { + const packageYml = await fetchAddonPackageData('multinewsletter'); expect(packageYml).toHaveProperty('name'); }); diff --git a/src/index.ts b/src/index.ts index d90245f..3d88cf8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import * as Core from '@actions/core'; import {md5_file, readPackageYml, validatePackageVersion} from "./package"; import {cacheFile, zip} from "./file"; -import {fetchAddonPackageYml, uploadArchive, versionExists} from "./myRedaxo"; +import {fetchAddonPackageData, uploadArchive, versionExists} from "./myRedaxo"; const packageDir = Core.getInput('cwd'); const archiveFilePath = cacheFile(); @@ -23,7 +23,7 @@ const archiveFilePath = cacheFile(); return; } - const existingPackageYml = await fetchAddonPackageYml(packageName, myRedaxoUsername, myRedaxoApiKey); + const existingPackageYml = await fetchAddonPackageData(packageName, myRedaxoUsername, myRedaxoApiKey); if (!existingPackageYml) { Core.setFailed(`Could not fetch addon ${packageName}. Please check your addon key and your MyRedaxo credentials.`); return; diff --git a/src/myRedaxo.ts b/src/myRedaxo.ts index 654085c..0684c9a 100644 --- a/src/myRedaxo.ts +++ b/src/myRedaxo.ts @@ -52,21 +52,30 @@ export async function uploadArchive(addonKey: string, redaxoLogin: string, redax return response.data.error; } -export async function fetchAddonPackageYml(addonKey: string, redaxoLogin?: string, redaxoApiKey?: string): Promise { +export async function fetchAddonPackageData(addonKey: string, redaxoLogin?: string, redaxoApiKey?: string): Promise { try { Core.info(`Fetch package.yml from redaxo.org for addon ${addonKey}`); - let queryString = ''; + let queryString = '?rex_version=5.x'; if (redaxoLogin && redaxoApiKey) { - queryString = `?api_login=${redaxoLogin}&api_key=${redaxoApiKey}`; + queryString += `&api_login=${redaxoLogin}&api_key=${redaxoApiKey}`; Core.info(`Using login: ${redaxoLogin}`); } else { Core.info(`No credentials provided, using anonymous access`); } - const response = await axios.get(`https://www.redaxo.org/de/ws/packages/${addonKey}/${queryString}`); - return response.data; + const responsePackage = await axios.get(`https://www.redaxo.org/de/ws/packages/${addonKey}/${queryString}`); + if (!responsePackage.data.error) { + return responsePackage.data; + } + + const responsePackages = await axios.get(`https://www.redaxo.org/de/ws/packages/${queryString}`); + if (responsePackages.data.error) { + Core.info(`Could not fetch addon ${addonKey}: ${responsePackages.data.error}`); + return null; + } + return responsePackages.data[addonKey] ?? null; } catch(e) { - console.error(Error(`Could not fetch addon ${addonKey}`)); + Core.info(`Could not fetch addon ${addonKey}`); return null; } }