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

MyRedaxo: add fallback for failed package requests #162

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
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