Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/server/src/services/uptime-kuma-url.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
22 changes: 22 additions & 0 deletions packages/server/src/services/uptime-kuma-url.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(
existingUrls: Set<string>,
url: string,
add: () => Promise<T>,
): Promise<T | undefined> => {
if (existingUrls.has(normalizeMonitorUrl(url))) return undefined;
return add();
};
12 changes: 9 additions & 3 deletions packages/server/src/services/uptime-kuma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]));
Expand Down Expand Up @@ -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);
Expand Down
Loading