From ab08228734068d49d937b300a0dc310286154a66 Mon Sep 17 00:00:00 2001 From: Eduard Marbach Date: Mon, 28 Oct 2024 17:09:11 +0100 Subject: [PATCH] feat: correctly handle new field minUpgradeFormatScore and add field in config (optional) --- index.ts | 36 ++++++++++++++-------- src/__generated__/ky-client.ts | 2 ++ src/__generated__/radarr/data-contracts.ts | 4 ++- src/quality-profiles.test.ts | 12 ++++++++ src/quality-profiles.ts | 14 +++++++++ src/types.ts | 1 + 6 files changed, 56 insertions(+), 13 deletions(-) diff --git a/index.ts b/index.ts index 70fe1e8..74307bf 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,6 @@ import "dotenv/config"; +import { HTTPError } from "ky"; import fs from "node:fs"; import { MergedCustomFormatResource } from "./src/__generated__/mergedTypes"; import { configureRadarrApi, configureSonarrApi, getArrApi, unsetApi } from "./src/api"; @@ -211,19 +212,30 @@ const pipeline = async (value: ConfigArrInstance, arrType: ArrType) => { } catch (error: any) { let message; - if (error.response) { - logger.info(error.response); - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - message = `Failed creating QualityProfile (${element.name}): Data ${JSON.stringify(error.response.data)}`; - } else if (error.request) { - // The request was made but no response was received - // `error.request` is an instance of XMLHttpRequest in the browser and an instance of - // http.ClientRequest in node.js - logger.info(error.request); + logger.debug(`Error creating QualityProfile: ${error?.name}`); + + if (error instanceof HTTPError) { + if (error.response) { + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + const errorJson = await error.response.json(); + logger.error(errorJson, `Failed creating QualityProfile (${element.name})`); + message = `Failed creating QualityProfile (${element.name}): Data ${JSON.stringify(errorJson)}`; + } else if (error.request) { + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + const errorJson = await error.request.json(); + logger.error(errorJson, `Failed creating QualityProfile (${element.name}) during request (probably some connection errors)`); + message = `Failed creating QualityProfile (${element.name}): Data ${JSON.stringify(errorJson)}`; + } else { + // Something happened in setting up the request that triggered an Error + logger.error("Error", error.message); + } } else { - // Something happened in setting up the request that triggered an Error - logger.info("Error", error.message); + message = `An not expected error happened. Feel free to open an issue with details to improve handling.`; + logger.error(message); + throw error; } throw new Error(message); diff --git a/src/__generated__/ky-client.ts b/src/__generated__/ky-client.ts index c291fc8..1131c33 100644 --- a/src/__generated__/ky-client.ts +++ b/src/__generated__/ky-client.ts @@ -183,6 +183,8 @@ export class HttpClient { hooks, }); + // TODO maybe handle request errors here? + return requestPromise; // Explicitly returning a typed promise }; } diff --git a/src/__generated__/radarr/data-contracts.ts b/src/__generated__/radarr/data-contracts.ts index 2aa00bc..8d46e50 100644 --- a/src/__generated__/radarr/data-contracts.ts +++ b/src/__generated__/radarr/data-contracts.ts @@ -1579,11 +1579,13 @@ export enum TMDbCountryCode { Fr = "fr", De = "de", Gb = "gb", + In = "in", Ie = "ie", It = "it", + Nz = "nz", + Ro = "ro", Es = "es", Us = "us", - Nz = "nz", } export interface TagDetailsResource { diff --git a/src/quality-profiles.test.ts b/src/quality-profiles.test.ts index 2afc123..ded5276 100644 --- a/src/quality-profiles.test.ts +++ b/src/quality-profiles.test.ts @@ -183,4 +183,16 @@ describe("QualityProfiles", async () => { expect(result[2].name).toBe("WEB 1080p"); expect(result[2].allowed).toBe(true); }); + + test("calculateQualityProfilesDiff - should diff if minUpgradeFormatScore is different", async ({}) => { + // TODO + }); + + test("calculateQualityProfilesDiff - should not diff if minUpgradeFormatScore is equal", async ({}) => { + // TODO + }); + + test("calculateQualityProfilesDiff - should not diff if minUpgradeFormatScore is not configured", async ({}) => { + // TODO + }); }); diff --git a/src/quality-profiles.ts b/src/quality-profiles.ts index 42c8b78..0c0879b 100644 --- a/src/quality-profiles.ts +++ b/src/quality-profiles.ts @@ -297,6 +297,8 @@ export const calculateQualityProfilesDiff = async ( minFormatScore: value.min_format_score, upgradeAllowed: value.upgrade.allowed, formatItems: customFormatsMapped, + // required since sonarr 4.0.10 (radarr also) + minUpgradeFormatScore: value.upgrade.min_format_score ?? 1, }); continue; } @@ -409,6 +411,18 @@ export const calculateQualityProfilesDiff = async ( changeList.push(`Upgrade until score diff: server: ${serverMatch.cutoffFormatScore} - expected: ${value.upgrade.until_score}`); } + + const configMinUpgradeFormatScore = value.upgrade.min_format_score ?? 1; + + // if not configured ignore + if (value.upgrade.min_format_score != null && serverMatch.minUpgradeFormatScore !== configMinUpgradeFormatScore) { + updatedServerObject.minUpgradeFormatScore = configMinUpgradeFormatScore; + diffExist = true; + + changeList.push( + `Min upgrade format score diff: server: ${serverMatch.cutoffFormatScore} - expected: ${configMinUpgradeFormatScore}`, + ); + } } // CFs matching diff --git a/src/types.ts b/src/types.ts index a5db638..b597c6a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -133,6 +133,7 @@ export type ConfigQualityProfile = { allowed: boolean; until_quality: string; until_score: number; + min_format_score?: number; // default 1 }; min_format_score: number; score_set: keyof TrashScores;