Skip to content

Commit 98c4db3

Browse files
committed
fix(api): add a migration script to rename *arr tags with spaces
This PR adds a migration script that will run at the startup of the app to remove the spaces from the *arr tags of Jellyseerr. fix #1897 re #1913 re Radarr/Radarr#11251
1 parent 872fc45 commit 98c4db3

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

server/api/servarr/base.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,25 @@ class ServarrBase<QueueItemAppendT> extends ExternalAPI {
198198
}
199199
};
200200

201+
public renameTag = async ({
202+
id,
203+
label,
204+
}: {
205+
id: number;
206+
label: string;
207+
}): Promise<Tag> => {
208+
try {
209+
const response = await this.axios.put<Tag>(`/tag/${id}`, {
210+
id,
211+
label,
212+
});
213+
214+
return response.data;
215+
} catch (e) {
216+
throw new Error(`[${this.apiName}] Failed to rename tag: ${e.message}`);
217+
}
218+
};
219+
201220
async refreshMonitoredDownloads(): Promise<void> {
202221
await this.runCommand('RefreshMonitoredDownloads', {});
203222
}

server/lib/settings/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ export interface AllSettings {
363363
jobs: Record<JobId, JobSettings>;
364364
network: NetworkSettings;
365365
metadataSettings: MetadataSettings;
366+
migrations: string[];
366367
}
367368

368369
const SETTINGS_PATH = process.env.CONFIG_DIRECTORY
@@ -593,6 +594,7 @@ class Settings {
593594
forceMaxTtl: -1,
594595
},
595596
},
597+
migrations: [],
596598
};
597599
if (initialSettings) {
598600
this.data = merge(this.data, initialSettings);
@@ -722,6 +724,14 @@ class Settings {
722724
this.data.network = data;
723725
}
724726

727+
get migrations(): string[] {
728+
return this.data.migrations;
729+
}
730+
731+
set migrations(data: string[]) {
732+
this.data.migrations = data;
733+
}
734+
725735
get clientId(): string {
726736
return this.data.clientId;
727737
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import RadarrAPI from '@server/api/servarr/radarr';
2+
import SonarrAPI from '@server/api/servarr/sonarr';
3+
import { getRepository } from '@server/datasource';
4+
import { User } from '@server/entity/User';
5+
import type { AllSettings } from '@server/lib/settings';
6+
7+
const migrationArrTags = async (settings: any): Promise<AllSettings> => {
8+
if (
9+
Array.isArray(settings.migrations) &&
10+
settings.migrations.includes('0007_migrate_arr_tags')
11+
) {
12+
return settings;
13+
}
14+
15+
const userRepository = getRepository(User);
16+
const users = await userRepository.find({
17+
select: ['id'],
18+
});
19+
20+
let errorOccurred = false;
21+
22+
for (const radarrSettings of settings.radarr || []) {
23+
if (!radarrSettings.tagRequests) {
24+
continue;
25+
}
26+
try {
27+
const radarr = new RadarrAPI({
28+
apiKey: radarrSettings.apiKey,
29+
url: RadarrAPI.buildUrl(radarrSettings, '/api/v3'),
30+
});
31+
const radarrTags = await radarr.getTags();
32+
for (const user of users) {
33+
const userTag = radarrTags.find((v) =>
34+
v.label.startsWith(user.id + ' - ')
35+
);
36+
if (!userTag) {
37+
continue;
38+
}
39+
await radarr.renameTag({
40+
id: userTag.id,
41+
label: userTag.label.replace(`${user.id} - `, `${user.id}-`),
42+
});
43+
}
44+
} catch {
45+
console.error(
46+
`Unable to rename Radarr tags to the new format. Please check your Radarr connection settings for the instance "${radarrSettings.name}".`
47+
);
48+
errorOccurred = true;
49+
}
50+
}
51+
52+
for (const sonarrSettings of settings.sonarr || []) {
53+
if (!sonarrSettings.tagRequests) {
54+
continue;
55+
}
56+
try {
57+
const sonarr = new SonarrAPI({
58+
apiKey: sonarrSettings.apiKey,
59+
url: SonarrAPI.buildUrl(sonarrSettings, '/api/v3'),
60+
});
61+
const sonarrTags = await sonarr.getTags();
62+
for (const user of users) {
63+
const userTag = sonarrTags.find((v) =>
64+
v.label.startsWith(user.id + ' - ')
65+
);
66+
if (!userTag) {
67+
continue;
68+
}
69+
await sonarr.renameTag({
70+
id: userTag.id,
71+
label: userTag.label.replace(`${user.id} - `, `${user.id}-`),
72+
});
73+
}
74+
} catch {
75+
console.error(
76+
`Unable to rename Sonarr tags to the new format. Please check your Sonarr connection settings for the instance "${sonarrSettings.name}".`
77+
);
78+
errorOccurred = true;
79+
}
80+
}
81+
82+
if (!errorOccurred) {
83+
if (!Array.isArray(settings.migrations)) {
84+
settings.migrations = [];
85+
}
86+
settings.migrations.push('0007_migrate_arr_tags');
87+
}
88+
return settings;
89+
};
90+
91+
export default migrationArrTags;

0 commit comments

Comments
 (0)