2026-04-20 13:57:30 +02:00
|
|
|
import { initialForm, type FormState } from './form-state';
|
|
|
|
|
import type { AlertRuleResponse } from '../../../api/queries/alertRules';
|
|
|
|
|
|
2026-04-20 14:04:04 +02:00
|
|
|
export interface PrefillWarning {
|
|
|
|
|
field: string;
|
|
|
|
|
message: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface PrefillOptions {
|
|
|
|
|
targetEnvAppSlugs?: string[];
|
|
|
|
|
/** IDs of outbound connections allowed in the target env. */
|
|
|
|
|
targetEnvAllowedConnectionIds?: string[];
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 13:57:30 +02:00
|
|
|
/**
|
2026-04-20 14:04:04 +02:00
|
|
|
* Client-side prefill when promoting a rule from another env. Emits warnings for
|
|
|
|
|
* fields that cross env boundaries (agent IDs, apps missing in target env,
|
|
|
|
|
* outbound connections not allowed in target env).
|
2026-04-20 13:57:30 +02:00
|
|
|
*/
|
2026-04-20 14:04:04 +02:00
|
|
|
export function prefillFromPromotion(
|
|
|
|
|
source: AlertRuleResponse,
|
|
|
|
|
opts: PrefillOptions = {},
|
|
|
|
|
): { form: FormState; warnings: PrefillWarning[] } {
|
|
|
|
|
const form = initialForm(source);
|
|
|
|
|
form.name = `${source.name ?? 'rule'} (copy)`;
|
|
|
|
|
const warnings: PrefillWarning[] = [];
|
|
|
|
|
|
|
|
|
|
// Agent IDs are per-env, can't transfer.
|
|
|
|
|
if (form.agentId) {
|
|
|
|
|
warnings.push({
|
|
|
|
|
field: 'scope.agentId',
|
|
|
|
|
message: `Agent \`${form.agentId}\` is specific to the source env \u2014 cleared for target env.`,
|
|
|
|
|
});
|
|
|
|
|
form.agentId = '';
|
|
|
|
|
if (form.scopeKind === 'agent') form.scopeKind = 'app';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// App slug: warn if not present in target env.
|
|
|
|
|
if (form.appSlug && opts.targetEnvAppSlugs && !opts.targetEnvAppSlugs.includes(form.appSlug)) {
|
|
|
|
|
warnings.push({
|
|
|
|
|
field: 'scope.appSlug',
|
|
|
|
|
message: `App \`${form.appSlug}\` does not exist in the target env. Update before saving.`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Webhook connections: warn if connection is not allowed in target env.
|
|
|
|
|
if (opts.targetEnvAllowedConnectionIds) {
|
|
|
|
|
for (const w of form.webhooks) {
|
|
|
|
|
if (!opts.targetEnvAllowedConnectionIds.includes(w.outboundConnectionId)) {
|
|
|
|
|
warnings.push({
|
|
|
|
|
field: `webhooks[${w.outboundConnectionId}]`,
|
|
|
|
|
message: `Outbound connection is not allowed in the target env \u2014 remove or pick another before saving.`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { form, warnings };
|
2026-04-20 13:57:30 +02:00
|
|
|
}
|