mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(config): dedupe bindings migrations
This commit is contained in:
@@ -6,80 +6,85 @@ import {
|
||||
mergeMissing,
|
||||
} from "./legacy.shared.js";
|
||||
|
||||
function migrateBindings(
|
||||
raw: Record<string, unknown>,
|
||||
changes: string[],
|
||||
changeNote: string,
|
||||
mutator: (match: Record<string, unknown>) => boolean,
|
||||
) {
|
||||
const bindings = Array.isArray(raw.bindings) ? raw.bindings : null;
|
||||
if (!bindings) {
|
||||
return;
|
||||
}
|
||||
|
||||
let touched = false;
|
||||
for (const entry of bindings) {
|
||||
if (!isRecord(entry)) {
|
||||
continue;
|
||||
}
|
||||
const match = getRecord(entry.match);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
if (!mutator(match)) {
|
||||
continue;
|
||||
}
|
||||
entry.match = match;
|
||||
touched = true;
|
||||
}
|
||||
|
||||
if (touched) {
|
||||
raw.bindings = bindings;
|
||||
changes.push(changeNote);
|
||||
}
|
||||
}
|
||||
|
||||
export const LEGACY_CONFIG_MIGRATIONS_PART_1: LegacyConfigMigration[] = [
|
||||
{
|
||||
id: "bindings.match.provider->bindings.match.channel",
|
||||
describe: "Move bindings[].match.provider to bindings[].match.channel",
|
||||
apply: (raw, changes) => {
|
||||
const bindings = Array.isArray(raw.bindings) ? raw.bindings : null;
|
||||
if (!bindings) {
|
||||
return;
|
||||
}
|
||||
|
||||
let touched = false;
|
||||
for (const entry of bindings) {
|
||||
if (!isRecord(entry)) {
|
||||
continue;
|
||||
}
|
||||
const match = getRecord(entry.match);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
if (typeof match.channel === "string" && match.channel.trim()) {
|
||||
continue;
|
||||
}
|
||||
const provider = typeof match.provider === "string" ? match.provider.trim() : "";
|
||||
if (!provider) {
|
||||
continue;
|
||||
}
|
||||
match.channel = provider;
|
||||
delete match.provider;
|
||||
entry.match = match;
|
||||
touched = true;
|
||||
}
|
||||
|
||||
if (touched) {
|
||||
raw.bindings = bindings;
|
||||
changes.push("Moved bindings[].match.provider → bindings[].match.channel.");
|
||||
}
|
||||
migrateBindings(
|
||||
raw,
|
||||
changes,
|
||||
"Moved bindings[].match.provider → bindings[].match.channel.",
|
||||
(match) => {
|
||||
if (typeof match.channel === "string" && match.channel.trim()) {
|
||||
return false;
|
||||
}
|
||||
const provider = typeof match.provider === "string" ? match.provider.trim() : "";
|
||||
if (!provider) {
|
||||
return false;
|
||||
}
|
||||
match.channel = provider;
|
||||
delete match.provider;
|
||||
return true;
|
||||
},
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "bindings.match.accountID->bindings.match.accountId",
|
||||
describe: "Move bindings[].match.accountID to bindings[].match.accountId",
|
||||
apply: (raw, changes) => {
|
||||
const bindings = Array.isArray(raw.bindings) ? raw.bindings : null;
|
||||
if (!bindings) {
|
||||
return;
|
||||
}
|
||||
|
||||
let touched = false;
|
||||
for (const entry of bindings) {
|
||||
if (!isRecord(entry)) {
|
||||
continue;
|
||||
}
|
||||
const match = getRecord(entry.match);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
if (match.accountId !== undefined) {
|
||||
continue;
|
||||
}
|
||||
const accountID =
|
||||
typeof match.accountID === "string" ? match.accountID.trim() : match.accountID;
|
||||
if (!accountID) {
|
||||
continue;
|
||||
}
|
||||
match.accountId = accountID;
|
||||
delete match.accountID;
|
||||
entry.match = match;
|
||||
touched = true;
|
||||
}
|
||||
|
||||
if (touched) {
|
||||
raw.bindings = bindings;
|
||||
changes.push("Moved bindings[].match.accountID → bindings[].match.accountId.");
|
||||
}
|
||||
migrateBindings(
|
||||
raw,
|
||||
changes,
|
||||
"Moved bindings[].match.accountID → bindings[].match.accountId.",
|
||||
(match) => {
|
||||
if (match.accountId !== undefined) {
|
||||
return false;
|
||||
}
|
||||
const accountID =
|
||||
typeof match.accountID === "string" ? match.accountID.trim() : match.accountID;
|
||||
if (!accountID) {
|
||||
return false;
|
||||
}
|
||||
match.accountId = accountID;
|
||||
delete match.accountID;
|
||||
return true;
|
||||
},
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user