Skip to content

Commit

Permalink
fallback to normale package listing if request failed
Browse files Browse the repository at this point in the history
  • Loading branch information
bloep committed Mar 29, 2023
1 parent 3686dd3 commit 6ea444f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
11 changes: 8 additions & 3 deletions __tests__/myRedaxo.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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;
Expand Down
21 changes: 15 additions & 6 deletions src/myRedaxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyRedaxoPackage|null> {
export async function fetchAddonPackageData(addonKey: string, redaxoLogin?: string, redaxoApiKey?: string): Promise<MyRedaxoPackage|null> {
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;
}
}
Expand Down

0 comments on commit 6ea444f

Please sign in to comment.