Skip to content

Commit

Permalink
feat: correctly handle new field minUpgradeFormatScore and add field …
Browse files Browse the repository at this point in the history
…in config (optional)
  • Loading branch information
BlackDark committed Oct 28, 2024
1 parent a296034 commit ab08228
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
36 changes: 24 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/__generated__/ky-client.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/__generated__/radarr/data-contracts.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/quality-profiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
14 changes: 14 additions & 0 deletions src/quality-profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ab08228

Please sign in to comment.