feat(config): gate channel config writes
This commit is contained in:
113
src/slack/channel-migration.test.ts
Normal file
113
src/slack/channel-migration.test.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { migrateSlackChannelConfig } from "./channel-migration.js";
|
||||
|
||||
describe("migrateSlackChannelConfig", () => {
|
||||
it("migrates global channel ids", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
slack: {
|
||||
channels: {
|
||||
C123: { requireMention: false },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = migrateSlackChannelConfig({
|
||||
cfg,
|
||||
accountId: "default",
|
||||
oldChannelId: "C123",
|
||||
newChannelId: "C999",
|
||||
});
|
||||
|
||||
expect(result.migrated).toBe(true);
|
||||
expect(cfg.channels.slack.channels).toEqual({
|
||||
C999: { requireMention: false },
|
||||
});
|
||||
});
|
||||
|
||||
it("migrates account-scoped channels", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
slack: {
|
||||
accounts: {
|
||||
primary: {
|
||||
channels: {
|
||||
C123: { requireMention: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = migrateSlackChannelConfig({
|
||||
cfg,
|
||||
accountId: "primary",
|
||||
oldChannelId: "C123",
|
||||
newChannelId: "C999",
|
||||
});
|
||||
|
||||
expect(result.migrated).toBe(true);
|
||||
expect(result.scopes).toEqual(["account"]);
|
||||
expect(cfg.channels.slack.accounts.primary.channels).toEqual({
|
||||
C999: { requireMention: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("matches account ids case-insensitively", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
slack: {
|
||||
accounts: {
|
||||
Primary: {
|
||||
channels: {
|
||||
C123: {},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = migrateSlackChannelConfig({
|
||||
cfg,
|
||||
accountId: "primary",
|
||||
oldChannelId: "C123",
|
||||
newChannelId: "C999",
|
||||
});
|
||||
|
||||
expect(result.migrated).toBe(true);
|
||||
expect(cfg.channels.slack.accounts.Primary.channels).toEqual({
|
||||
C999: {},
|
||||
});
|
||||
});
|
||||
|
||||
it("skips migration when new id already exists", () => {
|
||||
const cfg = {
|
||||
channels: {
|
||||
slack: {
|
||||
channels: {
|
||||
C123: { requireMention: true },
|
||||
C999: { requireMention: false },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const result = migrateSlackChannelConfig({
|
||||
cfg,
|
||||
accountId: "default",
|
||||
oldChannelId: "C123",
|
||||
newChannelId: "C999",
|
||||
});
|
||||
|
||||
expect(result.migrated).toBe(false);
|
||||
expect(result.skippedExisting).toBe(true);
|
||||
expect(cfg.channels.slack.channels).toEqual({
|
||||
C123: { requireMention: true },
|
||||
C999: { requireMention: false },
|
||||
});
|
||||
});
|
||||
});
|
||||
84
src/slack/channel-migration.ts
Normal file
84
src/slack/channel-migration.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import type { SlackChannelConfig } from "../config/types.slack.js";
|
||||
import { normalizeAccountId } from "../routing/session-key.js";
|
||||
|
||||
type SlackChannels = Record<string, SlackChannelConfig>;
|
||||
|
||||
type MigrationScope = "account" | "global";
|
||||
|
||||
export type SlackChannelMigrationResult = {
|
||||
migrated: boolean;
|
||||
skippedExisting: boolean;
|
||||
scopes: MigrationScope[];
|
||||
};
|
||||
|
||||
function resolveAccountChannels(
|
||||
cfg: ClawdbotConfig,
|
||||
accountId?: string | null,
|
||||
): { channels?: SlackChannels } {
|
||||
if (!accountId) return {};
|
||||
const normalized = normalizeAccountId(accountId);
|
||||
const accounts = cfg.channels?.slack?.accounts;
|
||||
if (!accounts || typeof accounts !== "object") return {};
|
||||
const exact = accounts[normalized];
|
||||
if (exact?.channels) return { channels: exact.channels };
|
||||
const matchKey = Object.keys(accounts).find(
|
||||
(key) => key.toLowerCase() === normalized.toLowerCase(),
|
||||
);
|
||||
return { channels: matchKey ? accounts[matchKey]?.channels : undefined };
|
||||
}
|
||||
|
||||
export function migrateSlackChannelsInPlace(
|
||||
channels: SlackChannels | undefined,
|
||||
oldChannelId: string,
|
||||
newChannelId: string,
|
||||
): { migrated: boolean; skippedExisting: boolean } {
|
||||
if (!channels) return { migrated: false, skippedExisting: false };
|
||||
if (oldChannelId === newChannelId) return { migrated: false, skippedExisting: false };
|
||||
if (!Object.hasOwn(channels, oldChannelId)) return { migrated: false, skippedExisting: false };
|
||||
if (Object.hasOwn(channels, newChannelId)) return { migrated: false, skippedExisting: true };
|
||||
channels[newChannelId] = channels[oldChannelId];
|
||||
delete channels[oldChannelId];
|
||||
return { migrated: true, skippedExisting: false };
|
||||
}
|
||||
|
||||
export function migrateSlackChannelConfig(params: {
|
||||
cfg: ClawdbotConfig;
|
||||
accountId?: string | null;
|
||||
oldChannelId: string;
|
||||
newChannelId: string;
|
||||
}): SlackChannelMigrationResult {
|
||||
const scopes: MigrationScope[] = [];
|
||||
let migrated = false;
|
||||
let skippedExisting = false;
|
||||
|
||||
const accountChannels = resolveAccountChannels(params.cfg, params.accountId).channels;
|
||||
if (accountChannels) {
|
||||
const result = migrateSlackChannelsInPlace(
|
||||
accountChannels,
|
||||
params.oldChannelId,
|
||||
params.newChannelId,
|
||||
);
|
||||
if (result.migrated) {
|
||||
migrated = true;
|
||||
scopes.push("account");
|
||||
}
|
||||
if (result.skippedExisting) skippedExisting = true;
|
||||
}
|
||||
|
||||
const globalChannels = params.cfg.channels?.slack?.channels;
|
||||
if (globalChannels) {
|
||||
const result = migrateSlackChannelsInPlace(
|
||||
globalChannels,
|
||||
params.oldChannelId,
|
||||
params.newChannelId,
|
||||
);
|
||||
if (result.migrated) {
|
||||
migrated = true;
|
||||
scopes.push("global");
|
||||
}
|
||||
if (result.skippedExisting) skippedExisting = true;
|
||||
}
|
||||
|
||||
return { migrated, skippedExisting, scopes };
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
import type { SlackEventMiddlewareArgs } from "@slack/bolt";
|
||||
|
||||
import { danger } from "../../../globals.js";
|
||||
import { loadConfig, writeConfigFile } from "../../../config/config.js";
|
||||
import { resolveChannelConfigWrites } from "../../../channels/plugins/config-writes.js";
|
||||
import { danger, warn } from "../../../globals.js";
|
||||
import { enqueueSystemEvent } from "../../../infra/system-events.js";
|
||||
|
||||
import { resolveSlackChannelLabel } from "../channel-config.js";
|
||||
import type { SlackMonitorContext } from "../context.js";
|
||||
import type { SlackChannelCreatedEvent, SlackChannelRenamedEvent } from "../types.js";
|
||||
import { migrateSlackChannelConfig } from "../../channel-migration.js";
|
||||
import type {
|
||||
SlackChannelCreatedEvent,
|
||||
SlackChannelIdChangedEvent,
|
||||
SlackChannelRenamedEvent,
|
||||
} from "../types.js";
|
||||
|
||||
export function registerSlackChannelEvents(params: { ctx: SlackMonitorContext }) {
|
||||
const { ctx } = params;
|
||||
@@ -75,4 +82,74 @@ export function registerSlackChannelEvents(params: { ctx: SlackMonitorContext })
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
ctx.app.event(
|
||||
"channel_id_changed",
|
||||
async ({ event, body }: SlackEventMiddlewareArgs<"channel_id_changed">) => {
|
||||
try {
|
||||
if (ctx.shouldDropMismatchedSlackEvent(body)) return;
|
||||
|
||||
const payload = event as SlackChannelIdChangedEvent;
|
||||
const oldChannelId = payload.old_channel_id;
|
||||
const newChannelId = payload.new_channel_id;
|
||||
if (!oldChannelId || !newChannelId) return;
|
||||
|
||||
const channelInfo = await ctx.resolveChannelName(newChannelId);
|
||||
const label = resolveSlackChannelLabel({
|
||||
channelId: newChannelId,
|
||||
channelName: channelInfo?.name,
|
||||
});
|
||||
|
||||
ctx.runtime.log?.(
|
||||
warn(`[slack] Channel ID changed: ${oldChannelId} → ${newChannelId} (${label})`),
|
||||
);
|
||||
|
||||
if (
|
||||
!resolveChannelConfigWrites({
|
||||
cfg: ctx.cfg,
|
||||
channelId: "slack",
|
||||
accountId: ctx.accountId,
|
||||
})
|
||||
) {
|
||||
ctx.runtime.log?.(
|
||||
warn("[slack] Config writes disabled; skipping channel config migration."),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const currentConfig = loadConfig();
|
||||
const migration = migrateSlackChannelConfig({
|
||||
cfg: currentConfig,
|
||||
accountId: ctx.accountId,
|
||||
oldChannelId,
|
||||
newChannelId,
|
||||
});
|
||||
|
||||
if (migration.migrated) {
|
||||
migrateSlackChannelConfig({
|
||||
cfg: ctx.cfg,
|
||||
accountId: ctx.accountId,
|
||||
oldChannelId,
|
||||
newChannelId,
|
||||
});
|
||||
await writeConfigFile(currentConfig);
|
||||
ctx.runtime.log?.(warn("[slack] Channel config migrated and saved successfully."));
|
||||
} else if (migration.skippedExisting) {
|
||||
ctx.runtime.log?.(
|
||||
warn(
|
||||
`[slack] Channel config already exists for ${newChannelId}; leaving ${oldChannelId} unchanged`,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
ctx.runtime.log?.(
|
||||
warn(
|
||||
`[slack] No config found for old channel ID ${oldChannelId}; migration logged only`,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
ctx.runtime.error?.(danger(`slack channel_id_changed handler failed: ${String(err)}`));
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -46,6 +46,13 @@ export type SlackChannelRenamedEvent = {
|
||||
event_ts?: string;
|
||||
};
|
||||
|
||||
export type SlackChannelIdChangedEvent = {
|
||||
type: "channel_id_changed";
|
||||
old_channel_id?: string;
|
||||
new_channel_id?: string;
|
||||
event_ts?: string;
|
||||
};
|
||||
|
||||
export type SlackPinEvent = {
|
||||
type: "pin_added" | "pin_removed";
|
||||
channel_id?: string;
|
||||
|
||||
Reference in New Issue
Block a user