diff --git a/packages/server/src/services/uptime-kuma-url.test.ts b/packages/server/src/services/uptime-kuma-url.test.ts new file mode 100644 index 000000000..b89f65776 --- /dev/null +++ b/packages/server/src/services/uptime-kuma-url.test.ts @@ -0,0 +1,25 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { mock } from "node:test"; +import { addMonitorUnlessMatching, normalizeMonitorUrl } from "./uptime-kuma-url"; + +test("monitor URL matching adopts scheme, www, and path variants", () => { + assert.equal(normalizeMonitorUrl("https://www.foo.com/health"), "foo.com"); + assert.equal(normalizeMonitorUrl("http://foo.com"), "foo.com"); +}); + +test("sync adoption skips creating a duplicate for a matching manual monitor", async () => { + const existingUrls = new Set([ + normalizeMonitorUrl("https://www.foo.com/health"), + ]); + const addMonitor = mock.fn(async () => 42); + + const id = await addMonitorUnlessMatching( + existingUrls, + "https://foo.com", + addMonitor, + ); + + assert.equal(id, undefined); + assert.equal(addMonitor.mock.callCount(), 0); +}); diff --git a/packages/server/src/services/uptime-kuma-url.ts b/packages/server/src/services/uptime-kuma-url.ts new file mode 100644 index 000000000..80eb4715c --- /dev/null +++ b/packages/server/src/services/uptime-kuma-url.ts @@ -0,0 +1,22 @@ +export const normalizeMonitorUrl = (url: string): string => { + try { + const parsed = new URL(url); + const hostname = parsed.hostname.toLowerCase().replace(/^www\./, ""); + return `${hostname}${parsed.port ? `:${parsed.port}` : ""}`; + } catch { + return url + .toLowerCase() + .replace(/^[a-z][a-z\d+.-]*:\/\//, "") + .replace(/^www\./, "") + .split(/[/?#]/, 1)[0]; + } +}; + +export const addMonitorUnlessMatching = async ( + existingUrls: Set, + url: string, + add: () => Promise, +): Promise => { + if (existingUrls.has(normalizeMonitorUrl(url))) return undefined; + return add(); +}; diff --git a/packages/server/src/services/uptime-kuma.ts b/packages/server/src/services/uptime-kuma.ts index ca16c4c0f..d020f1994 100644 --- a/packages/server/src/services/uptime-kuma.ts +++ b/packages/server/src/services/uptime-kuma.ts @@ -8,6 +8,10 @@ import { type UptimeKumaConn, withUptimeKuma, } from "../utils/monitoring/uptime-kuma-client"; +import { + addMonitorUnlessMatching, + normalizeMonitorUrl, +} from "./uptime-kuma-url"; // Generated default hosts are throwaway preview URLs — never worth an external // uptime monitor. Mirrors the check in routers/project.ts. @@ -191,7 +195,7 @@ export const syncUptimeKumaMonitors = async ( const result = await withUptimeKuma(connOf(row), async (s: KumaSession) => { const existingUrls = new Set( - s.monitors.map((m) => (m.url ?? "").replace(/\/$/, "")), + s.monitors.map((m) => normalizeMonitorUrl(m.url ?? "")), ); const existingIds = new Set(s.monitors.map((m) => m.id)); const monitorById = new Map(s.monitors.map((m) => [m.id, m])); @@ -223,9 +227,11 @@ export const syncUptimeKumaMonitors = async ( } // A user-made monitor already covers this URL — adopt by skipping (no dupe, // and we won't delete it since it's not recorded as managed). - if (existingUrls.has(d.url.replace(/\/$/, ""))) continue; try { - const id = await s.addMonitor(d.host, d.url, notifIds); + const id = await addMonitorUnlessMatching(existingUrls, d.url, () => + s.addMonitor(d.host, d.url, notifIds), + ); + if (id === undefined) continue; managed[d.host] = id; added++; await ensureTagged(id, d.host);