refactor widget-only integrations
This commit is contained in:
122 files changed
+2259
-3016
No files matched your search
@@ -1,8 +1,7 @@
|
||||
import SuperJSON from "superjson";
|
||||
|
||||
import { createId } from "@homarr/common";
|
||||
import type { IntegrationKind } from "@homarr/definitions";
|
||||
import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { releaseProviderKinds } from "@homarr/definitions";
|
||||
|
||||
import { eq } from "../..";
|
||||
import type { Database } from "../..";
|
||||
@@ -13,19 +12,6 @@ export async function migrateReleaseWidgetProviderToOptionsAsync(db: Database) {
|
||||
where: (items, { eq }) => eq(items.kind, "releases"),
|
||||
});
|
||||
|
||||
const integrationKinds = getIntegrationKindsByCategory("releasesProvider");
|
||||
const providerIntegrations = await db.query.integrations.findMany({
|
||||
where: (integrations, { inArray }) => inArray(integrations.kind, integrationKinds),
|
||||
columns: {
|
||||
id: true,
|
||||
kind: true,
|
||||
},
|
||||
});
|
||||
|
||||
const providerIntegrationMap = new Map<IntegrationKind, string>(
|
||||
providerIntegrations.map((integration) => [integration.kind, integration.id]),
|
||||
);
|
||||
|
||||
const updates: {
|
||||
id: string;
|
||||
options: object;
|
||||
@@ -44,7 +30,7 @@ export async function migrateReleaseWidgetProviderToOptionsAsync(db: Database) {
|
||||
|
||||
return {
|
||||
id: createId(),
|
||||
providerIntegrationId: providerIntegrationMap.get(provider as IntegrationKind) ?? null,
|
||||
provider: releaseProviderKinds.find((kind) => kind === provider),
|
||||
...otherFields,
|
||||
};
|
||||
},
|
||||
@@ -68,5 +54,5 @@ export async function migrateReleaseWidgetProviderToOptionsAsync(db: Database) {
|
||||
.where(eq(items.id, update.id));
|
||||
}
|
||||
|
||||
console.log(`Migrated release widget providers to integrations count="${updates.length}"`);
|
||||
console.log(`Migrated release widget providers to options count="${updates.length}"`);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import SuperJSON from "superjson";
|
||||
|
||||
import { eq, inArray } from "../..";
|
||||
import type { Database } from "../..";
|
||||
import { integrationItems, integrations, items } from "../../schema";
|
||||
|
||||
const removedIntegrationKinds = [
|
||||
"github",
|
||||
"dockerHub",
|
||||
"gitlab",
|
||||
"npm",
|
||||
"codeberg",
|
||||
"linuxServerIO",
|
||||
"gitHubContainerRegistry",
|
||||
"quay",
|
||||
"searchCh",
|
||||
"archiveTeamWarrior",
|
||||
] as const;
|
||||
|
||||
type RemovedIntegrationKind = (typeof removedIntegrationKinds)[number];
|
||||
|
||||
const releaseProviderKinds = [
|
||||
"github",
|
||||
"dockerHub",
|
||||
"gitlab",
|
||||
"npm",
|
||||
"codeberg",
|
||||
"linuxServerIO",
|
||||
"gitHubContainerRegistry",
|
||||
"quay",
|
||||
] as const;
|
||||
|
||||
const isReleaseProviderKind = (kind: RemovedIntegrationKind): kind is (typeof releaseProviderKinds)[number] =>
|
||||
releaseProviderKinds.some((provider) => provider === kind);
|
||||
|
||||
export async function migrateWidgetOnlyIntegrationsToOptionsAsync(db: Database) {
|
||||
const oldIntegrations = await db.query.integrations.findMany({
|
||||
columns: { id: true, kind: true, url: true },
|
||||
});
|
||||
const oldIntegrationById = new Map(
|
||||
oldIntegrations
|
||||
.filter((integration) => removedIntegrationKinds.includes(integration.kind as RemovedIntegrationKind))
|
||||
.map((integration) => [
|
||||
integration.id,
|
||||
{ kind: integration.kind as RemovedIntegrationKind, url: integration.url },
|
||||
]),
|
||||
);
|
||||
|
||||
if (oldIntegrationById.size === 0) return;
|
||||
|
||||
const oldIntegrationIds = Array.from(oldIntegrationById.keys());
|
||||
const oldIntegrationItems = await db.query.integrationItems.findMany();
|
||||
const oldIntegrationItemByItemId = new Map(
|
||||
oldIntegrationItems
|
||||
.filter((item) => oldIntegrationById.has(item.integrationId))
|
||||
.map((item) => [item.itemId, oldIntegrationById.get(item.integrationId)]),
|
||||
);
|
||||
const oldIntegrationItemIdsByItemId = oldIntegrationItems
|
||||
.filter((item) => oldIntegrationById.has(item.integrationId))
|
||||
.reduce<Map<string, string[]>>((acc, item) => {
|
||||
const itemIds = acc.get(item.itemId) ?? [];
|
||||
itemIds.push(item.integrationId);
|
||||
acc.set(item.itemId, itemIds);
|
||||
return acc;
|
||||
}, new Map());
|
||||
|
||||
const existingItems = await db.query.items.findMany({
|
||||
where: (items, { inArray }) => inArray(items.kind, ["releases", "timetable", "archiveTeamWarrior"]),
|
||||
});
|
||||
|
||||
const updatedItems: { id: string; options: object }[] = [];
|
||||
const unlinkItemIds: string[] = [];
|
||||
|
||||
for (const item of existingItems) {
|
||||
const options = SuperJSON.parse<Record<string, unknown>>(item.options);
|
||||
const linkedIntegration = oldIntegrationItemByItemId.get(item.id);
|
||||
|
||||
if (item.kind === "releases" && Array.isArray(options.repositories)) {
|
||||
const repositories = options.repositories.map((repository) => {
|
||||
if (typeof repository !== "object" || repository === null) return repository;
|
||||
const { providerIntegrationId, ...rest } = repository as Record<string, unknown>;
|
||||
const provider =
|
||||
typeof providerIntegrationId === "string" ? oldIntegrationById.get(providerIntegrationId) : undefined;
|
||||
const existingProviderUrl =
|
||||
typeof rest.providerUrl === "string" && rest.providerUrl.length > 0 ? rest.providerUrl : undefined;
|
||||
return {
|
||||
...rest,
|
||||
provider: provider && isReleaseProviderKind(provider.kind) ? provider.kind : rest.provider,
|
||||
providerUrl: provider?.url ?? existingProviderUrl,
|
||||
};
|
||||
});
|
||||
updatedItems.push({ id: item.id, options: { ...options, repositories } });
|
||||
if (oldIntegrationItemIdsByItemId.has(item.id)) unlinkItemIds.push(item.id);
|
||||
}
|
||||
|
||||
if (item.kind === "timetable" && linkedIntegration?.kind === "searchCh") {
|
||||
updatedItems.push({ id: item.id, options: { ...options, baseUrl: linkedIntegration.url } });
|
||||
unlinkItemIds.push(item.id);
|
||||
}
|
||||
|
||||
if (item.kind === "archiveTeamWarrior" && linkedIntegration?.kind === "archiveTeamWarrior") {
|
||||
updatedItems.push({ id: item.id, options: { ...options, url: linkedIntegration.url } });
|
||||
unlinkItemIds.push(item.id);
|
||||
}
|
||||
}
|
||||
|
||||
for (const update of updatedItems) {
|
||||
await db
|
||||
.update(items)
|
||||
.set({ options: SuperJSON.stringify(update.options) })
|
||||
.where(eq(items.id, update.id));
|
||||
}
|
||||
|
||||
if (unlinkItemIds.length > 0) {
|
||||
await db.delete(integrationItems).where(inArray(integrationItems.itemId, unlinkItemIds));
|
||||
}
|
||||
|
||||
await db.delete(integrations).where(inArray(integrations.id, oldIntegrationIds));
|
||||
|
||||
console.log(`Migrated widget-only integrations to widget options count="${updatedItems.length}"`);
|
||||
}
|
||||
@@ -2,9 +2,11 @@ import type { Database } from "../..";
|
||||
import { migrateReleaseWidgetProviderToOptionsAsync } from "./0000_release_widget_provider_to_options";
|
||||
import { migrateOpnsenseCredentialsAsync } from "./0001_opnsense_credentials";
|
||||
import { migrateAppWidgetShowDescriptionTooltipToDisplayModeAsync } from "./0002_app_widget_show_description_tooltip_to_display_mode";
|
||||
import { migrateWidgetOnlyIntegrationsToOptionsAsync } from "./0003_remove_widget_only_integrations";
|
||||
|
||||
export const applyCustomMigrationsAsync = async (db: Database) => {
|
||||
await migrateReleaseWidgetProviderToOptionsAsync(db);
|
||||
await migrateOpnsenseCredentialsAsync(db);
|
||||
await migrateAppWidgetShowDescriptionTooltipToDisplayModeAsync(db);
|
||||
await migrateWidgetOnlyIntegrationsToOptionsAsync(db);
|
||||
};
|
||||
Reference in new issue
Block a user