Files
alexei.dolgolyov 2aff22f565
Build / build (push) Successful in 10m39s
feat(triggers): first-class triggers + bindings with fan-out webhook
Promote triggers from embedded workload fields to standalone records
joined to workloads via workload_trigger_bindings. One trigger (webhook,
registry watcher, git push, manual) now fans out to many workloads with
per-binding config overrides (top-level JSON merge, binding wins).

Backend
- new triggers + workload_trigger_bindings tables with ON DELETE CASCADE
- boot-time backfill of embedded trigger config inside per-workload tx
- store.ErrUnique sentinel translates SQLite UNIQUE at store boundary
- /api/triggers CRUD + /api/triggers/{id}/{webhook,bindings}
- /api/bindings/{id} update/delete; /api/workloads/{id}/triggers list+bind
- bindTriggerToWorkload accepts trigger_id or inline {kind,name,config}
- inline-create uses CreateTriggerWithBindingTx (no orphan triggers)
- validateBindingConfig enforces 8 KiB cap + plugin Validate on merged
- ListTriggersWithBindingCount + ListBindings*WithNames remove N+1
- POST /api/webhook/triggers/{secret} resolves trigger then fans out
- bounded worker pool (4) per request; per-binding error isolation
- outcome accounting: deployed / skipped / no-match / errored
- legacy /api/webhook/workloads/{secret} route removed (clean break;
  backfill keeps secrets resolvable at the new /triggers/{secret} path)
- reconciler gate dropped from (Source && Trigger) to Source only
- MergeJSONConfig returns freshly allocated slices (no fan-out aliasing)
- WithEffectiveTrigger lets existing Trigger.Match contract stay unchanged

Frontend
- /triggers list, new wizard, [id] detail (bindings, webhook rotate)
- workload create wizard: NEW / PICK / SKIP trigger modes
- workload detail: bindings panel + Add-trigger modal (inline / pick)
- per-binding override editor with merged-preview + 8 KiB guard
- "OVERRIDES n FIELDS" row badge when binding_config is non-empty
- shared TriggerKindForm component (registry / git / manual + JSON)
- 3 raw <input type=checkbox> replaced with <ToggleSwitch>
- full EN + RU i18n: redeployTriggers.*, apps.detail.bindings.*,
  apps.new.triggers.*, nav.triggers; event-triggers nav disambiguated

Doc
- WORKLOAD_REFACTOR_TODO: trigger-split marked DONE; next focus is
  the static-source inline port + hard legacy cutover (Priority 1)
2026-05-16 02:24:31 +03:00

58 lines
2.0 KiB
Go

package plugin
import (
"encoding/json"
"fmt"
)
// MergeJSONConfig merges override on top of base at the top-level
// (binding fields win, base fields fill the rest). Both inputs are
// expected to be JSON objects ("{}" when empty); arrays or scalars are
// rejected because trigger and binding configs are always objects.
//
// Always returns a freshly allocated slice so callers may freely mutate
// the result without affecting base. The fast-path (override empty)
// returns a defensive copy of base for the same reason.
func MergeJSONConfig(base, override json.RawMessage) (json.RawMessage, error) {
if len(override) == 0 || string(override) == "{}" {
if len(base) == 0 {
return json.RawMessage("{}"), nil
}
return append(json.RawMessage(nil), base...), nil
}
if len(base) == 0 || string(base) == "{}" {
return append(json.RawMessage(nil), override...), nil
}
baseMap := map[string]json.RawMessage{}
if err := json.Unmarshal(base, &baseMap); err != nil {
return nil, fmt.Errorf("merge config: base is not a JSON object: %w", err)
}
overMap := map[string]json.RawMessage{}
if err := json.Unmarshal(override, &overMap); err != nil {
return nil, fmt.Errorf("merge config: override is not a JSON object: %w", err)
}
for k, v := range overMap {
baseMap[k] = v
}
out, err := json.Marshal(baseMap)
if err != nil {
return nil, fmt.Errorf("merge config: re-marshal: %w", err)
}
return out, nil
}
// WithEffectiveTrigger returns a copy of w with TriggerKind and
// TriggerConfig set from a resolved (trigger, binding) pair. The
// existing Trigger.Match contract reads w.TriggerConfig via
// TriggerConfigOf[T], so this is the seam that lets the trigger plugins
// stay unchanged after the trigger-split refactor.
func WithEffectiveTrigger(w Workload, kind string, triggerConfig, bindingConfig json.RawMessage) (Workload, error) {
merged, err := MergeJSONConfig(triggerConfig, bindingConfig)
if err != nil {
return Workload{}, err
}
w.TriggerKind = kind
w.TriggerConfig = merged
return w, nil
}