Add semantic sync-test naming, rule mapping and rules map command

This commit is contained in:
gitea committed 2026-09-11 12:05:23 +08:00
1 parent 8db4b87385
commit 3d9e25fd06
7 files changed
+541 -11

No files matched your search

+84 -6
View File
@@ -22,6 +22,11 @@ import { SealCookieMapSchema, SealHeaderMapSchema } from "../../core/config/type
import type { CorpConfig, CorpEnvironment } from "../../core/config/types.js";
import * as api from "../../domains/seal/api.js";
import { getMe } from "../../domains/seal/api.js";
import {
computeNextSyncIteration,
formatSyncVersionName,
saveRuleMappingSnapshot
} from "../../domains/seal/rule-mapping.js";
import { resolveLiveSealEnterpriseConfig } from "../../domains/seal/source.js";
import { selectCorp } from "../helpers.js";
import { confirmationRequired, usageError } from "../errors.js";
@@ -284,6 +289,17 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
const prodRules = await api.listApprovalRules(prodClient);
const testRules = await api.listApprovalRules(testClient);
const prodVersions = await api.listApprovalRuleVersions(prodClient).catch(() => []);
const testVersions = await api.listApprovalRuleVersions(testClient).catch(() => []);
const sortedProdVersions = [...prodVersions].sort((a, b) => b.versionNumber - a.versionNumber);
const latestProd = sortedProdVersions[0];
const prodVersionNumber = latestProd?.versionNumber;
const nextIteration = computeNextSyncIteration(prodCorp.name, prodVersionNumber, testVersions);
const customVersionName = stringOption(args.options["version-name"]) ?? stringOption(args.options.versionName);
const versionName = customVersionName ?? formatSyncVersionName(prodCorp.name, prodVersionNumber, nextIteration);
const updatedTestPref = {
...testPref,
tone: prodPref.tone,
@@ -297,10 +313,6 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
thinkingDepthPreference: prodPref.thinkingDepthPreference
};
const customVersionName = stringOption(args.options["version-name"]) ?? stringOption(args.options.versionName);
const dateStr = new Date().toISOString().slice(0, 10);
const versionName = customVersionName ?? `Sync from ${prodCorp.name} (${dateStr})`;
const dryRun = booleanOption(args.options["dry-run"]) === true || booleanOption(args.options.dryRun) === true;
if (dryRun) {
printJson({
@@ -308,13 +320,24 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
source: {
id: prodCorp.id,
name: prodCorp.name,
environment: prodCorp.environment
environment: prodCorp.environment,
latestPublishedVersion: latestProd ? {
versionNumber: latestProd.versionNumber,
versionName: latestProd.versionName
} : null
},
target: {
id: testCorp.id,
name: testCorp.name,
environment: testCorp.environment
},
versionMappingPlan: {
sourceProdVersionNumber: prodVersionNumber ?? null,
sourceProdVersionName: latestProd?.versionName ?? null,
targetTestExpectedVersionNumber: (testVersions[0]?.versionNumber ?? 0) + 1,
targetTestPublishVersionName: versionName,
iteration: nextIteration
},
preferencesDiff: {
decisionPreferences: {
before: testPref.decisionPreferences,
@@ -332,6 +355,9 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
toCreateCount: prodRules.rules.length,
rulesToSync: prodRules.rules.map((r, idx) => ({
index: idx + 1,
sourceRuleId: r.id,
sourceRuleCode: r.code ?? idx + 1,
expectedTargetCode: `rule-${idx + 1}`,
description: r.description,
scope: r.scope,
strictness: r.strictness
@@ -367,7 +393,7 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
}
// 3. Create new draft rules in test enterprise
const createdRules = [];
const createdRules: any[] = [];
for (const rule of prodRules.rules) {
const created = await api.createApprovalRule(testClient, {
description: rule.description,
@@ -383,6 +409,51 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
// 5. Publish new version in test enterprise
const published = await api.publishApprovalRuleVersion(testClient, versionName);
const publishedRules = published.version.rules ?? [];
const ruleMapping = prodRules.rules.map((prodRule, idx) => {
const testDraft = createdRules[idx];
const publishedRule = publishedRules[idx];
return {
index: idx + 1,
sourceRuleId: prodRule.id,
sourceRuleCode: prodRule.code ?? idx + 1,
targetRuleId: testDraft?.id ?? "",
targetRuleCode: publishedRule?.code ?? `rule-${idx + 1}`,
description: prodRule.description,
scope: prodRule.scope,
strictness: prodRule.strictness
};
});
const versionMapping = {
sourceProdVersionNumber: prodVersionNumber ?? null,
sourceProdVersionName: latestProd?.versionName ?? null,
targetTestVersionNumber: published.version.versionNumber,
targetTestVersionName: published.version.versionName ?? null
};
let mappingSavedTo: string | undefined;
try {
mappingSavedTo = saveRuleMappingSnapshot({
syncedAt: new Date().toISOString(),
source: {
id: prodCorp.id,
name: prodCorp.name,
latestPublishedVersionNumber: prodVersionNumber ?? null,
latestPublishedVersionName: latestProd?.versionName ?? null
},
target: {
id: testCorp.id,
name: testCorp.name,
publishedVersionNumber: published.version.versionNumber,
publishedVersionName: published.version.versionName,
rulesCount: published.rulesCount
},
versionMapping,
ruleMapping
});
} catch {}
printJson({
success: true,
syncedAt: new Date().toISOString(),
@@ -390,6 +461,10 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
id: prodCorp.id,
name: prodCorp.name,
environment: prodCorp.environment,
latestPublishedVersion: latestProd ? {
versionNumber: latestProd.versionNumber,
versionName: latestProd.versionName
} : null,
draftRulesCount: prodRules.rules.length
},
target: {
@@ -405,6 +480,9 @@ export async function handleCorpsCommand(action: string | undefined, maybeName:
rulesCount: published.rulesCount
}
},
versionMapping,
ruleMapping,
...(mappingSavedTo ? { mappingSavedTo } : {}),
productionGuarantees: {
prodRulesStayDraft: true,
prodVersionPublished: false