import type { Construct } from '../../constructs/construct.js'; import type { Project } from '../../constructs/project.js'; import type { DiffEntry } from '../../rest/projects.js'; import type { AssertionBuilderName } from './helper-edit.js'; /** * Turns the remote changes of a deploy plan into edits of the construct * source files, and applies them: checks, groups, alert channels, private * locations, dashboards, maintenance windows and status pages with their * services, components and automation rules. * * A remote change is a property that moved in the Checkly account since the * last deploy (`origin: 'remote'`, or `'both'` when the code moved too). The * value written is the account's current one, read from the entry's `before` * — the deployed resource in the import format, which a full-detail preview * carries — at the path the change names. The change list decides *which* * paths are written; `before` supplies the values, because a change can * carry a hash where `before` carries the text, and a set element change * names one element where the code holds the whole list. * * Only what can be written without guessing is written. A property has to * be one this module knows the construct's spelling of (the table below): a * literal of the same shape, or a helper expression — `Frequency.EVERY_*`, * `RetryStrategyBuilder`, `AlertEscalationBuilder`, an assertion builder — * that `helper-edit.ts` renders from the value the way `checkly import` * would; a value Checkly withholds — a credential the redaction table * blanks, a masked secret, a hash — is never written; and when the change's * own report of the current value disagrees with `before`, neither is * trusted. Everything refused is listed with its reason. */ export interface WriteBackOptions { diff: readonly DiffEntry[]; project: Project; /** The directory file names are shown relative to. */ cwd: string; } export interface WriteBackLine { /** The edited file, relative to `cwd`. */ file: string; type: string; logicalId: string; /** The construct property, dotted. */ property: string; /** The source text replaced, or undefined when the property was added. */ previous?: string; /** The source text written. */ rendered: string; /** Whether the code had moved too (`origin: 'both'`), so a local edit is being replaced. */ replacesLocalEdit: boolean; } /** A remote change that was not written, or a resource none could be written for. */ export interface WriteBackSkip { type: string; logicalId: string; /** The property or path the reason is about; none when it is about the whole resource. */ property?: string; reason: string; } /** * The source of one edited construct, from the line its options open on to * the line they close on, before its edits and after them: what a reader * needs to see the edits in place. */ export interface WriteBackConstruct { /** The edited file, relative to `cwd`. */ file: string; type: string; logicalId: string; before: string; after: string; } export interface WriteBackPlan { /** Each file's new text, with the text it was planned from. */ files: { path: string; text: string; original: string; }[]; applied: WriteBackLine[]; /** One entry per construct `applied` has lines for, in the order they were edited. */ constructs: WriteBackConstruct[]; skipped: WriteBackSkip[]; /** The helper classes added to each edited file's `checkly/constructs` import. */ imports: { file: string; names: string[]; }[]; } /** How an import-format path maps onto a construct property. */ export interface Rule { /** Segments of the import-format pointer. */ pointer: string[]; /** The construct property path the pointer maps to. */ target: string[]; /** A list the API reports per element rather than whole; the code holds the whole list. */ set?: boolean; /** Properties that only mean something together are written together or not at all. */ group?: string; /** The helper the construct spells the property with; the value is written as its expression. */ helper?: { kind: 'frequency' | 'retryStrategy' | 'date'; } | { kind: 'alertEscalation'; policy: AlertPolicyHolder; } | { kind: 'assertions'; builder: AssertionBuilderName; }; /** Further pointers whose `before` values the expression needs (`frequencyOffset` beside `frequency`). */ reads?: string[][]; /** Sibling keys of the property that make the edit wrong (`doubleCheck` beside `retryStrategy`). */ unless?: string[]; /** * Respells the deployed value the way the construct takes it, before it is * written: a check's intent is stored and reported as two outcome lists, * which the construct spells as one `constraints` list. The changes are * still checked against the deployed value as reported. */ reshape?: (value: unknown) => unknown; /** * A rule that routes the changes at its pointer onto another rule's * target (the one whose `target` it shares) rather than anchoring a * candidate of its own: `/frequencyOffset` belongs to `frequency`, * `/useGlobalAlertSettings` to `alertEscalationPolicy`, and a code-origin * `/doubleCheck` marks `retryStrategy` as locally changed. */ companion?: true; /** For a companion, the reason its changes are refused given the deployed resource, if they are. */ refuse?: (before: unknown) => string | undefined; } /** * Who holds the alert policy: a check or a v1 group spells "the global * policy" by having none, which nothing is ever removed from the code for; * a v2 group has the word `'global'` for it, and a group of either version * can also have no policy of its own while its checks keep theirs. */ type AlertPolicyHolder = 'check' | 'group' | 'group-v2'; /** * The properties this module writes, per construct class: the ones whose * import-format spelling and construct spelling are both literals with the * same shape, and the ones the construct spells with a helper this module * can render (`frequency`, `retryStrategy`, `alertEscalationPolicy`, the * `assertions` of a request), and the SSL request whose wire keys map onto * the construct's (`SSL_REQUEST_RULES`). A rule may likewise map a wire * pointer onto a prop spelled or nested differently in the construct (an * alert channel's `config/` onto its flat prop, a component's * `configuration/`, `coolDownWindowMinutes` onto `coolDownMinutes`), * and a `refusing` rule names the reason for a leaf the construct fixes or * derives. Anything else — references, scripts, a request key the * construct does not take — is left to the user. * * Keyed by the exact class, not by `instanceof`: a class this table does not * name gets nothing rather than a base class's rules, so a construct that * omits one of them (`PlaywrightCheck` takes no `retryStrategy`, as its * codegen's omitted props say) can never be handed it. The spec checks that every * construct class `checkly/constructs` exports is listed here or excluded * on purpose, and that the omitted props are absent. */ /** A construct class, as a map key. */ export type ConstructClass = abstract new (...args: any[]) => Construct; export declare const RULES_BY_CLASS: ReadonlyMap; /** The top-level props keys each class's rules write, as the build-time assertion above knows them; the spec holds `RULES_BY_CLASS` to it. */ export declare const WRITTEN_BY_CLASS: ReadonlyMap; /** * Whether the plan holds at least one change the planner would try to * write: a remote change on a construct of a known class, at a path the * class's table covers. Cheap enough to decide whether to offer the * write-back at all, without reading any file. */ export declare function hasWritableChanges(diff: readonly DiffEntry[], project: Project): boolean; export declare function planWriteBack({ diff, project, cwd }: WriteBackOptions): Promise; /** * Writes every planned file through a temporary file in the same directory, * flushed to disk and then renamed over the target, so an interrupted write * leaves the old file or the new one rather than a torn one. The target is * the file itself, not a symlink to it, and it keeps its mode. * * @throws Error naming the files already rewritten when a later one fails. */ export declare function applyWriteBack(plan: Pick): Promise; export {};