diff --git a/changelog.d/added-workflowmodel-store-bindings.md b/changelog.d/added-workflowmodel-store-bindings.md new file mode 100644 index 0000000000..c369578e87 --- /dev/null +++ b/changelog.d/added-workflowmodel-store-bindings.md @@ -0,0 +1 @@ +- Workflow models, RFC phase 2 ([#17620](https://github.com/hivecommons/hive/issues/17620)): models now have a home and a way to reach the engine. `pkg/workflowmodel/store` keeps versioned models under `/workflows//.json` (plus `index.json`); `hive-default` is virtual and read-only, drafts save but do not compile, every entry reserves `adoption`/`ratings`/`benchmarks` fields for the marketplace and carries them verbatim across saves. New owner-gated, audited `/api/workflows` routes list, save, read, delete and compile models; `project.workflow_bindings: [{repo, name, version}]` binds a repository to a compiling model (the binding is refused for a draft, and a bound model cannot be deleted). When `workflow.enabled` is on, the stage reconciler derives bound repositories through the bound model and everything else through the built-in derivers exactly as before. Settings → Repos gains a per-repo Workflow select, and the Activity Monitor response gains a `workflows` block (default columns plus per-repo columns and stage numbers) that the tracker uses to label the model column and to render a per-row column strip, with an amber "Not covered" chevron when a bound model abstains. Nothing changes for unbound repositories. diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 8897966794..c3e38c477d 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -83,6 +83,10 @@ { "name": "Swarm", "description": "Repo swarm mode status, lifecycle, and history" + }, + { + "name": "Workflows", + "description": "Workflow models (RFC hivecommons/hive#17620): the spoke-local library of ingested/authored workflow definitions, per-repository bindings, and compilation. Writes are owner-gated and audited (workflow_save, workflow_delete, workflow_bind, workflow_unbind). Every route answers 503 when the hive has no workflow store." } ], "paths": { @@ -38008,22 +38012,59 @@ }, "/api/repos/{owner}/{repo}/items/{number}/blocker/handoff": { "post": { - "tags": ["Status"], + "tags": [ + "Status" + ], "summary": "Hand a workflows-blocked issue to a capable lane", "description": "Requires a signed-in repository writer, an open parked issue without a hold, a workflows permission blocker, and an eligible lane with installation permission. Records the decision and routes the issue to that lane.", "parameters": [ - {"name": "owner", "in": "path", "required": true, "schema": {"type": "string"}}, - {"name": "repo", "in": "path", "required": true, "schema": {"type": "string"}}, - {"name": "number", "in": "path", "required": true, "schema": {"type": "integer"}} + { + "name": "owner", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "repo", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "number", + "in": "path", + "required": true, + "schema": { + "type": "integer" + } + } ], "responses": { - "200": {"description": "Handoff recorded"}, - "401": {"description": "GitHub user session required"}, - "403": {"description": "Repository write access required"}, - "404": {"description": "No blocker is recorded"}, - "409": {"description": "Issue is held, not parked, or no capable lane is available"}, - "502": {"description": "GitHub operation failed"}, - "503": {"description": "GitHub client unavailable"} + "200": { + "description": "Handoff recorded" + }, + "401": { + "description": "GitHub user session required" + }, + "403": { + "description": "Repository write access required" + }, + "404": { + "description": "No blocker is recorded" + }, + "409": { + "description": "Issue is held, not parked, or no capable lane is available" + }, + "502": { + "description": "GitHub operation failed" + }, + "503": { + "description": "GitHub client unavailable" + } } } }, @@ -38822,6 +38863,398 @@ } } } + }, + "/api/workflows": { + "get": { + "tags": [ + "Workflows" + ], + "summary": "List workflow models", + "description": "Every stored model version plus the virtual read-only hive-default, the current repository bindings, and whether model-driven derivation (workflow.enabled) is on.", + "responses": { + "200": { + "description": "Library", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "workflows": { + "type": "array", + "items": { + "$ref": "#/components/schemas/WorkflowEntry" + } + }, + "bindings": { + "type": "array", + "items": { + "$ref": "#/components/schemas/WorkflowBinding" + } + }, + "enabled": { + "type": "boolean" + } + } + } + } + } + }, + "503": { + "description": "No workflow store" + } + } + }, + "post": { + "tags": [ + "Workflows" + ], + "summary": "Save a workflow model", + "description": "Owner only. Compiles the model; a draft that fails compilation is still stored (compiled=false) so it can be edited, but cannot be bound. Audited as workflow_save.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowModel" + } + } + } + }, + "responses": { + "201": { + "description": "Stored", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "entry": { + "$ref": "#/components/schemas/WorkflowEntry" + } + } + } + } + } + }, + "400": { + "description": "Bad name/version or invalid model" + }, + "403": { + "description": "Not an owner, or the version is read-only (hive-default)" + }, + "503": { + "description": "No workflow store" + } + } + } + }, + "/api/workflows/compile": { + "post": { + "tags": [ + "Workflows" + ], + "summary": "Compile a workflow model without storing it", + "description": "Returns the compiler diagnostics and, when it compiles, the profile. Used by the editor and by ingest previews.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowModel" + } + } + } + }, + "responses": { + "200": { + "description": "Compilation result", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "compiled": { + "type": "boolean" + }, + "diagnostics": { + "type": "array", + "items": { + "$ref": "#/components/schemas/WorkflowDiagnostic" + } + }, + "profile": { + "$ref": "#/components/schemas/WorkflowProfile" + } + } + } + } + } + }, + "400": { + "description": "Malformed body" + }, + "503": { + "description": "No workflow store" + } + } + } + }, + "/api/workflows/bindings": { + "get": { + "tags": [ + "Workflows" + ], + "summary": "List repository → workflow bindings", + "description": "project.workflow_bindings from hive.yaml plus the live workflow.enabled flag. Unbound repositories derive with hive-default.", + "responses": { + "200": { + "description": "Bindings", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "bindings": { + "type": "array", + "items": { + "$ref": "#/components/schemas/WorkflowBinding" + } + }, + "enabled": { + "type": "boolean" + } + } + } + } + } + }, + "503": { + "description": "No workflow store or config" + } + } + }, + "put": { + "tags": [ + "Workflows" + ], + "summary": "Bind or unbind a repository", + "description": "Owner only. Body {repo, name, version}; an empty name unbinds. The model must exist and compile. Persists hive.yaml and takes effect on the next derivation pass. Audited as workflow_bind / workflow_unbind.", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowBinding" + } + } + } + }, + "responses": { + "200": { + "description": "Bindings after the change", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "changed": { + "type": "boolean" + }, + "bindings": { + "type": "array", + "items": { + "$ref": "#/components/schemas/WorkflowBinding" + } + } + } + } + } + } + }, + "400": { + "description": "Missing repo, or the model does not compile" + }, + "403": { + "description": "Not an owner" + }, + "404": { + "description": "Model not found" + }, + "503": { + "description": "No workflow store or config" + } + } + } + }, + "/api/workflows/{name}/{version}": { + "get": { + "tags": [ + "Workflows" + ], + "summary": "Fetch one workflow model version", + "parameters": [ + { + "name": "name", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Model", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "entry": { + "$ref": "#/components/schemas/WorkflowEntry" + }, + "model": { + "$ref": "#/components/schemas/WorkflowModel" + }, + "profile": { + "$ref": "#/components/schemas/WorkflowProfile" + } + } + } + } + } + }, + "404": { + "description": "Not found" + }, + "503": { + "description": "No workflow store" + } + } + }, + "put": { + "tags": [ + "Workflows" + ], + "summary": "Save a workflow model at an explicit name@version", + "description": "Owner only. Same as POST /api/workflows but the path must agree with the body name/version. Audited as workflow_save.", + "parameters": [ + { + "name": "name", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/WorkflowModel" + } + } + } + }, + "responses": { + "201": { + "description": "Stored", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "entry": { + "$ref": "#/components/schemas/WorkflowEntry" + } + } + } + } + } + }, + "400": { + "description": "Path and body disagree, or invalid model" + }, + "403": { + "description": "Not an owner, or read-only" + }, + "503": { + "description": "No workflow store" + } + } + }, + "delete": { + "tags": [ + "Workflows" + ], + "summary": "Delete a workflow model version", + "description": "Owner only. Refused with 409 while any repository is bound to it; hive-default cannot be deleted. Audited as workflow_delete.", + "parameters": [ + { + "name": "name", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "version", + "in": "path", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Deleted", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "ok": { + "type": "boolean" + } + } + } + } + } + }, + "403": { + "description": "Not an owner, or read-only" + }, + "404": { + "description": "Not found" + }, + "409": { + "description": "Still bound" + }, + "503": { + "description": "No workflow store" + } + } + } } }, "components": { @@ -39126,6 +39559,105 @@ } } } + }, + "WorkflowEntry": { + "type": "object", + "description": "Index row for one stored model version.", + "properties": { + "name": { + "type": "string" + }, + "version": { + "type": "string" + }, + "hash": { + "type": "string", + "description": "sha256 of the stored model document" + }, + "title": { + "type": "string" + }, + "provenance": { + "type": "string", + "enum": [ + "builtin", + "local", + "ingested", + "imported" + ] + }, + "compiled": { + "type": "boolean", + "description": "false for a draft that does not compile; drafts can be stored but not bound" + }, + "builtin": { + "type": "boolean", + "description": "true for the read-only hive-default model" + }, + "created_at": { + "type": "string", + "format": "date-time" + }, + "updated_at": { + "type": "string", + "format": "date-time" + }, + "ratings": { + "description": "Reserved for the marketplace (benchmarks, votes); preserved verbatim across saves.", + "nullable": true + } + } + }, + "WorkflowBinding": { + "type": "object", + "properties": { + "repo": { + "type": "string", + "description": "owner/repo or bare repo name" + }, + "name": { + "type": "string" + }, + "version": { + "type": "string" + } + }, + "required": [ + "repo", + "name", + "version" + ] + }, + "WorkflowDiagnostic": { + "type": "object", + "properties": { + "severity": { + "type": "string", + "enum": [ + "error", + "warning" + ] + }, + "code": { + "type": "string" + }, + "message": { + "type": "string" + }, + "path": { + "type": "string" + } + } + }, + "WorkflowModel": { + "type": "object", + "description": "A workflow.hivecommons.dev/v1alpha1 model document (states, transitions, columns, derivation, events, gates, roles, agents, checks, bookkeeping, labels, meta).", + "additionalProperties": true + }, + "WorkflowProfile": { + "type": "object", + "description": "Compiled, derivation-ready form of a model (hash, states, columns, ordered rules). Never trusted from the wire: the spoke recompiles on save and on bind.", + "additionalProperties": true } } } diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 74055b4905..631231f642 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "encoding/json" "fmt" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" "io" "log/slog" "net/url" @@ -1085,6 +1086,7 @@ func (b *boot) wireBootClosures() { } return &dashboard.Dependencies{ Config: b.cfg, + WorkflowStore: wfstore.Open(wfstore.Root(b.cfg.Data.WorkflowsDir)), AgentMgr: b.agentMgr, CruxRuntimeHealth: cruxRuntimeHealthFunc(b.cruxSupervision), // Provider gateways (#5565 slice 3): concrete openrouter/watsonx/ diff --git a/src/cmd/hive/stage_engine.go b/src/cmd/hive/stage_engine.go index 58b5ae5e5d..72d5f41066 100644 --- a/src/cmd/hive/stage_engine.go +++ b/src/cmd/hive/stage_engine.go @@ -8,6 +8,8 @@ import ( "github.com/hivecommons/hive/pkg/github" "github.com/hivecommons/hive/pkg/stageengine" "github.com/hivecommons/hive/pkg/stagelabels" + "github.com/hivecommons/hive/pkg/workflowmodel" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" ) // The central stage engine (ADR-0025, hivecommons/hive#12999) is built once @@ -38,17 +40,52 @@ func stageEngineOptions(cfg *config.Config, logger *slog.Logger) *stageengine.Op logger = slog.Default() } stageEngine.opts = &stageengine.Options{ - Mode: stageEngineConfiguredMode(cfg), - Store: stagelabels.Shared(), - Issue: github.IssueDeriver(), - PR: stageengine.PRDeriver(), - Logger: logger, - Metrics: stageengine.DefaultCounters(), + Mode: stageEngineConfiguredMode(cfg), + Store: stagelabels.Shared(), + Issue: github.IssueDeriver(), + PR: stageengine.PRDeriver(), + Logger: logger, + Metrics: stageengine.DefaultCounters(), + WorkflowFor: stageEngineWorkflowFor(cfg, wfstore.Open(wfstore.Root(cfg.Data.WorkflowsDir)), logger), } }) return stageEngine.opts } +// stageEngineWorkflowFor resolves the workflow model bound to a repository +// (hivecommons/hive#17620). cfg is read on every call — hive.yaml reloads +// and dashboard binding edits mutate it in place — so a binding or the +// workflow.enabled flag takes effect on the next derivation. A bound model +// that is missing or no longer compiles is logged once per key and the +// repository falls back to hive-default rather than going unlabelled. +func stageEngineWorkflowFor(cfg *config.Config, st *wfstore.Store, logger *slog.Logger) func(owner, repo string) *workflowmodel.Profile { + if cfg == nil || st == nil { + return nil + } + var warned sync.Map + return func(owner, repo string) *workflowmodel.Profile { + if !cfg.Workflow.Enabled { + return nil + } + full := repo + if owner != "" { + full = owner + "/" + repo + } + b, ok := cfg.WorkflowBindingFor(full) + if !ok { + return nil + } + p, err := st.Profile(b.Name, b.Version) + if err != nil { + if _, dup := warned.LoadOrStore(b.Key()+"|"+err.Error(), true); !dup && logger != nil { + logger.Warn("workflow binding unusable; repository falls back to hive-default", "repo", full, "workflow", b.Key(), "err", err) + } + return nil + } + return p + } +} + func stageEngineConfiguredMode(cfg *config.Config) stageengine.Mode { if cfg == nil { return config.DefaultStageEngineMode diff --git a/src/cmd/hive/stage_engine_workflow_test.go b/src/cmd/hive/stage_engine_workflow_test.go new file mode 100644 index 0000000000..a41bc124de --- /dev/null +++ b/src/cmd/hive/stage_engine_workflow_test.go @@ -0,0 +1,64 @@ +package main + +import ( + "encoding/json" + "os" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/workflowmodel" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" +) + +func TestStageEngineWorkflowFor(t *testing.T) { + if stageEngineWorkflowFor(nil, nil, nil) != nil { + t.Fatal("nil inputs") + } + st := wfstore.Open(t.TempDir()) + b, err := os.ReadFile("../../pkg/workflowmodel/testdata/models/techdocs.json") + if err != nil { + t.Fatal(err) + } + var m workflowmodel.Model + if err := json.Unmarshal(b, &m); err != nil { + t.Fatal(err) + } + if _, err := st.Save(&m, "test", time.Now()); err != nil { + t.Fatal(err) + } + cfg := &config.Config{Project: config.ProjectConfig{Org: "cncf"}} + if _, err := cfg.SetWorkflowBinding("techdocs", "cncf-techdocs", "1"); err != nil { + t.Fatal(err) + } + if _, err := cfg.SetWorkflowBinding("ghost", "nope", "1"); err != nil { + t.Fatal(err) + } + lookup := stageEngineWorkflowFor(cfg, st, discardLogger()) + + if lookup("cncf", "techdocs") != nil { + t.Fatal("flag off must resolve nothing") + } + cfg.Workflow.Enabled = true + if p := lookup("cncf", "techdocs"); p == nil || p.Name != "cncf-techdocs" { + t.Fatalf("%+v", p) + } + if lookup("", "techdocs") == nil { + t.Fatal("bare repo should qualify with the org") + } + if lookup("cncf", "other") != nil { + t.Fatal("unbound repo resolved a model") + } + // Twice: the second call exercises the warn-once path. + for i := 0; i < 2; i++ { + if lookup("cncf", "ghost") != nil { + t.Fatal("missing model must fall back") + } + } + if _, err := cfg.SetWorkflowBinding("techdocs", "", ""); err != nil { + t.Fatal(err) + } + if lookup("cncf", "techdocs") != nil { + t.Fatal("unbinding must take effect live") + } +} diff --git a/src/docs/design/workflow-models.md b/src/docs/design/workflow-models.md index 2a3d679c7c..13c080eeb2 100644 --- a/src/docs/design/workflow-models.md +++ b/src/docs/design/workflow-models.md @@ -101,3 +101,27 @@ kind is not covered. Models are whole (no inheritance) through phase 4. - Pipelines-as-Code `on-cel-expression` triggers are recorded as `cel` events and not classified into roles, so a tag-push expressed in CEL does not add *Release*. - Bare Tekton `Pipeline` library files (catalogs) have no trigger and bind nowhere (`no-trigger`). + +## Store, bindings and engine wiring (RFC phase 2) + +- **Store** (`pkg/workflowmodel/store`): `/workflows//.json` plus an + `index.json` rebuilt on write; names/versions are regex-guarded, writes are atomic. + `hive-default` is virtual and read-only. Drafts (models that do not compile) save but + `Profile()` returns `ErrNotCompiled`. Entries reserve `adoption`, `ratings`, `benchmarks` + (raw JSON, carried verbatim) so the phase-7 marketplace can add them without a migration. +- **Binding** (`pkg/config/workflow_binding.go`): `project.workflow_bindings: [{repo, name, + version}]`, mirroring `repo_policies`. `WorkflowBindingFor` is keyed like repo pauses + (lowercased, org-qualified) and reads the live config, so a PUT takes effect without a + restart. Binding requires the model to compile; deleting a bound model is a 409. +- **API** (`pkg/dashboard/api_workflows.go`): `GET/POST /api/workflows`, `POST + /api/workflows/compile`, `GET/PUT /api/workflows/bindings`, `GET/PUT/DELETE + /api/workflows/{name}/{version}`. Writes need the owner role and are audited; 503 when no + store is configured. +- **Engine**: `stageengine.Options.WorkflowFor(owner, repo) *Profile` and `DeriveForRepo` (a + per-profile, hash-cached deriver) route bound repositories through `DeriveFromModel`; unbound + repositories and `workflow.enabled: false` take the builtin path unchanged. An unusable + binding (missing or non-compiling model) warns once and falls back. +- **Tracker**: the response gains `workflows: {default: {...}, repos: {: {name, version, + columns, numbers, error}}}` (nil when nothing is bound). The JS labels the model column, + renders a per-row column strip by stage number, and shows an amber "Not covered" chevron for + abstentions. Settings → Repos has a per-repo Workflow select that PUTs the binding. diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index f98585c3b4..1d20038166 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -296,6 +296,10 @@ type DataConfig struct { // panel configuration (#11694). Default /data/nous; HIVE_NOUS_DATA_DIR // overrides it at runtime. NousDir string `yaml:"nous_dir,omitempty"` + // WorkflowsDir is the durable root for stored workflow models + // (hivecommons/hive#17620); models live under /workflows. Empty + // means the hive data dir (/data); HIVE_WORKFLOWS_DATA_DIR overrides. + WorkflowsDir string `yaml:"workflows_dir,omitempty"` // SessionRetentionDays is how many days of CLI session-state directories to // keep. Session directories are created per CLI invocation and were never diff --git a/src/pkg/config/project_config.go b/src/pkg/config/project_config.go index fa40be3ef5..c25aed0f66 100644 --- a/src/pkg/config/project_config.go +++ b/src/pkg/config/project_config.go @@ -79,6 +79,9 @@ type ProjectConfig struct { // repo name. project.repos remains the watched-repo identity list; this // sidecar list lets existing string-list configs keep round-tripping. RepoPolicies []RepoPolicy `yaml:"repo_policies,omitempty" json:"repo_policies,omitempty"` + // WorkflowBindings assigns stored workflow models to repositories + // (hivecommons/hive#17620). Unbound repositories use hive-default. + WorkflowBindings []WorkflowBinding `yaml:"workflow_bindings,omitempty" json:"workflow_bindings,omitempty"` // MergeFrontTimeout is the hive-wide front timeout of the serialized merge // lane (hivecommons/hive#12952) as a Go duration ("90m", "2h"); empty // keeps the lane default of 60 minutes. repo_policies[].merge_front_timeout diff --git a/src/pkg/config/testdata/agent_runtime_golden.json b/src/pkg/config/testdata/agent_runtime_golden.json index 8f6cb5a40a..9758a5abaa 100644 --- a/src/pkg/config/testdata/agent_runtime_golden.json +++ b/src/pkg/config/testdata/agent_runtime_golden.json @@ -494,6 +494,7 @@ "BobSessionsDir": "/data/home/.bob", "AgentsDir": "/data/agent-configs", "NousDir": "/data/nous", + "WorkflowsDir": "", "SessionRetentionDays": null }, "deployment": {}, diff --git a/src/pkg/config/validate.go b/src/pkg/config/validate.go index 86d7532e6c..2d94665a8e 100644 --- a/src/pkg/config/validate.go +++ b/src/pkg/config/validate.go @@ -97,6 +97,9 @@ func (c *Config) ValidateWithOptions(opts ValidateOptions) error { if err := ValidateWritingGuide(c.Project.WritingGuide); err != nil { return err } + if err := validateWorkflowBindings(c.Project.WorkflowBindings); err != nil { + return err + } for _, rp := range c.Project.RepoPolicies { if err := ValidateRepoMergeStrategy(rp.Repo, rp.MergeStrategy); err != nil { return err diff --git a/src/pkg/config/workflow_binding.go b/src/pkg/config/workflow_binding.go new file mode 100644 index 0000000000..f6dcb51c31 --- /dev/null +++ b/src/pkg/config/workflow_binding.go @@ -0,0 +1,95 @@ +package config + +import ( + "fmt" + "strings" +) + +// WorkflowBinding assigns a stored workflow model (name@version) to a +// repository (hivecommons/hive#17620). Repositories without a binding keep +// the built-in hive-default behaviour unchanged. +type WorkflowBinding struct { + Repo string `yaml:"repo" json:"repo"` + Name string `yaml:"name" json:"name"` + Version string `yaml:"version" json:"version"` +} + +// Key returns the name@version identity of the bound model. +func (b WorkflowBinding) Key() string { return b.Name + "@" + b.Version } + +// WorkflowBindingFor returns the binding covering repo, if any. repo may be +// bare or org-qualified. +func (c *Config) WorkflowBindingFor(repo string) (WorkflowBinding, bool) { + if c == nil { + return WorkflowBinding{}, false + } + key := repoPauseKey(c.Project.Org, repo) + if key == "" { + return WorkflowBinding{}, false + } + repoPauseMu.RLock() + defer repoPauseMu.RUnlock() + for _, b := range c.Project.WorkflowBindings { + if repoPauseKey(c.Project.Org, b.Repo) == key { + return b, true + } + } + return WorkflowBinding{}, false +} + +// SetWorkflowBinding binds repo to name@version, replacing any existing +// binding for the repo. An empty name removes the binding. It reports whether +// the config changed. +func (c *Config) SetWorkflowBinding(repo, name, version string) (bool, error) { + if c == nil { + return false, fmt.Errorf("nil config") + } + repo = strings.TrimSpace(repo) + key := repoPauseKey(c.Project.Org, repo) + if key == "" { + return false, fmt.Errorf("workflow binding: empty repo") + } + name, version = strings.TrimSpace(name), strings.TrimSpace(version) + if name != "" && version == "" { + return false, fmt.Errorf("workflow binding: %s has no version", name) + } + repoPauseMu.Lock() + defer repoPauseMu.Unlock() + kept := c.Project.WorkflowBindings[:0:0] + changed := false + for _, b := range c.Project.WorkflowBindings { + if repoPauseKey(c.Project.Org, b.Repo) == key { + changed = changed || name == "" || b.Name != name || b.Version != version + continue + } + kept = append(kept, b) + } + if name != "" { + if len(kept) == len(c.Project.WorkflowBindings) { + changed = true + } + kept = append(kept, WorkflowBinding{Repo: repo, Name: name, Version: version}) + } + if changed { + c.Project.WorkflowBindings = kept + } + return changed, nil +} + +func validateWorkflowBindings(bs []WorkflowBinding) error { + seen := map[string]bool{} + for i, b := range bs { + if strings.TrimSpace(b.Repo) == "" { + return fmt.Errorf("project.workflow_bindings[%d]: repo is required", i) + } + if strings.TrimSpace(b.Name) == "" || strings.TrimSpace(b.Version) == "" { + return fmt.Errorf("project.workflow_bindings[%d] (%s): name and version are required", i, b.Repo) + } + k := strings.ToLower(b.Repo) + if seen[k] { + return fmt.Errorf("project.workflow_bindings: %s bound twice", b.Repo) + } + seen[k] = true + } + return nil +} diff --git a/src/pkg/config/workflow_binding_test.go b/src/pkg/config/workflow_binding_test.go new file mode 100644 index 0000000000..55410374e6 --- /dev/null +++ b/src/pkg/config/workflow_binding_test.go @@ -0,0 +1,68 @@ +package config + +import ( + "strings" + "testing" +) + +func TestWorkflowBindings(t *testing.T) { + var nilCfg *Config + if _, ok := nilCfg.WorkflowBindingFor("x"); ok { + t.Fatal("nil config bound") + } + if _, err := nilCfg.SetWorkflowBinding("x", "a", "1"); err == nil { + t.Fatal("nil config set") + } + c := &Config{Project: ProjectConfig{Org: "hivecommons"}} + if _, ok := c.WorkflowBindingFor(""); ok { + t.Fatal("empty repo bound") + } + if _, err := c.SetWorkflowBinding(" ", "a", "1"); err == nil { + t.Fatal("empty repo accepted") + } + if _, err := c.SetWorkflowBinding("docs", "a", ""); err == nil { + t.Fatal("missing version accepted") + } + changed, err := c.SetWorkflowBinding("docs", "cncf-techdocs", "1") + if err != nil || !changed { + t.Fatalf("%v %v", changed, err) + } + b, ok := c.WorkflowBindingFor("HiveCommons/Docs") + if !ok || b.Key() != "cncf-techdocs@1" { + t.Fatalf("%+v %v", b, ok) + } + if changed, _ := c.SetWorkflowBinding("hivecommons/docs", "cncf-techdocs", "1"); changed { + t.Fatal("same binding reported changed") + } + if changed, _ := c.SetWorkflowBinding("docs", "cncf-techdocs", "2"); !changed || len(c.Project.WorkflowBindings) != 1 { + t.Fatalf("%+v", c.Project.WorkflowBindings) + } + if changed, _ := c.SetWorkflowBinding("other", "", ""); changed { + t.Fatal("unbinding an unbound repo changed") + } + if changed, _ := c.SetWorkflowBinding("docs", "", ""); !changed || len(c.Project.WorkflowBindings) != 0 { + t.Fatalf("%+v", c.Project.WorkflowBindings) + } +} + +func TestValidateWorkflowBindings(t *testing.T) { + cases := []struct { + bs []WorkflowBinding + want string + }{ + {nil, ""}, + {[]WorkflowBinding{{Repo: "a", Name: "m", Version: "1"}}, ""}, + {[]WorkflowBinding{{Name: "m", Version: "1"}}, "repo is required"}, + {[]WorkflowBinding{{Repo: "a", Name: "m"}}, "name and version"}, + {[]WorkflowBinding{{Repo: "a", Name: "m", Version: "1"}, {Repo: "A", Name: "n", Version: "1"}}, "bound twice"}, + } + for _, tc := range cases { + err := validateWorkflowBindings(tc.bs) + if tc.want == "" && err != nil { + t.Fatalf("%+v: %v", tc.bs, err) + } + if tc.want != "" && (err == nil || !strings.Contains(err.Error(), tc.want)) { + t.Fatalf("%+v: %v", tc.bs, err) + } + } +} diff --git a/src/pkg/dashboard/api.go b/src/pkg/dashboard/api.go index fa159bbe1a..ca71ee6b1b 100644 --- a/src/pkg/dashboard/api.go +++ b/src/pkg/dashboard/api.go @@ -35,6 +35,7 @@ func (s *Server) RegisterAPI(deps *Dependencies) { // Issue claims (hivecommons/hive#8380). Routes register unconditionally; // handlers answer "not enabled" when deps.IssueClaims is nil. s.registerClaimsRoutes() + s.registerWorkflowRoutes() // Approval desk (RFC #4000). Routes register unconditionally; the handlers // report "not enabled" when deps.ApprovalInbox is nil, so a disabled desk is // an honest 200 the panel can render rather than a 404 that looks broken. diff --git a/src/pkg/dashboard/api_workflows.go b/src/pkg/dashboard/api_workflows.go new file mode 100644 index 0000000000..025d1241f3 --- /dev/null +++ b/src/pkg/dashboard/api_workflows.go @@ -0,0 +1,222 @@ +package dashboard + +import ( + "errors" + "fmt" + "net/http" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/workflowmodel" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" +) + +// Workflow-model library and bindings (hivecommons/hive#17620, RFC phase 2). +// +// Reads are open to any dashboard user; writes are owner-gated and audited. +// The store is optional: without one every endpoint answers 503 so the UI can +// say "workflows not enabled" instead of showing an empty library. +func (s *Server) registerWorkflowRoutes() { + s.mux.HandleFunc("GET /api/workflows", s.handleWorkflowList) + s.mux.HandleFunc("POST /api/workflows", s.handleWorkflowSave) + s.mux.HandleFunc("POST /api/workflows/compile", s.handleWorkflowCompile) + s.mux.HandleFunc("GET /api/workflows/bindings", s.handleWorkflowBindingsGet) + s.mux.HandleFunc("PUT /api/workflows/bindings", s.handleWorkflowBindingPut) + s.mux.HandleFunc("GET /api/workflows/{name}/{version}", s.handleWorkflowGet) + s.mux.HandleFunc("PUT /api/workflows/{name}/{version}", s.handleWorkflowSave) + s.mux.HandleFunc("DELETE /api/workflows/{name}/{version}", s.handleWorkflowDelete) +} + +func (s *Server) workflowStore(w http.ResponseWriter) *wfstore.Store { + if s.deps == nil || s.deps.WorkflowStore == nil { + jsonError(w, "workflow store not enabled", http.StatusServiceUnavailable) + return nil + } + return s.deps.WorkflowStore +} + +// workflowProfileLookup is the store's Profile method, or nil without a store. +func (s *Server) workflowProfileLookup() workflowProfileLookup { + if s.deps == nil || s.deps.WorkflowStore == nil { + return nil + } + return s.deps.WorkflowStore.Profile +} + +func workflowStoreError(w http.ResponseWriter, err error) { + switch { + case errors.Is(err, wfstore.ErrNotFound): + jsonError(w, err.Error(), http.StatusNotFound) + case errors.Is(err, wfstore.ErrReadOnly): + jsonError(w, err.Error(), http.StatusForbidden) + case errors.Is(err, wfstore.ErrBadName), errors.Is(err, wfstore.ErrBadVersion), errors.Is(err, wfstore.ErrNotCompiled): + jsonError(w, err.Error(), http.StatusBadRequest) + default: + jsonError(w, err.Error(), http.StatusInternalServerError) + } +} + +func (s *Server) handleWorkflowList(w http.ResponseWriter, r *http.Request) { + st := s.workflowStore(w) + if st == nil { + return + } + entries, err := st.List() + if err != nil { + workflowStoreError(w, err) + return + } + jsonResponse(w, map[string]interface{}{ + "workflows": entries, + "bindings": s.workflowBindings(), + "enabled": s.deps.Config != nil && s.deps.Config.Workflow.Enabled, + }) +} + +func (s *Server) handleWorkflowGet(w http.ResponseWriter, r *http.Request) { + st := s.workflowStore(w) + if st == nil { + return + } + m, e, err := st.Get(r.PathValue("name"), r.PathValue("version")) + if err != nil { + workflowStoreError(w, err) + return + } + resp := map[string]interface{}{"entry": e, "model": m} + if p, _, cerr := workflowmodel.Compile(m); cerr == nil { + resp["profile"] = p + } + jsonResponse(w, resp) +} + +// handleWorkflowCompile compiles a model without saving it so the editor can +// show diagnostics as the operator types. +func (s *Server) handleWorkflowCompile(w http.ResponseWriter, r *http.Request) { + var m workflowmodel.Model + if err := decodeBody(r, &m); err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + p, diags, err := workflowmodel.Compile(&m) + resp := map[string]interface{}{"compiled": err == nil, "diagnostics": diags} + if diags == nil { + resp["diagnostics"] = []workflowmodel.Diagnostic{} + } + if p != nil { + resp["profile"] = p + } + jsonResponse(w, resp) +} + +func (s *Server) handleWorkflowSave(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + st := s.workflowStore(w) + if st == nil { + return + } + var m workflowmodel.Model + if err := decodeBody(r, &m); err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + if name := r.PathValue("name"); name != "" && (name != m.Metadata.Name || r.PathValue("version") != m.Spec.Meta.Version) { + jsonError(w, fmt.Sprintf("body is %s@%s, path is %s@%s", m.Metadata.Name, m.Spec.Meta.Version, name, r.PathValue("version")), http.StatusBadRequest) + return + } + e, err := st.Save(&m, requestUser(r), time.Now().UTC()) + if err != nil { + workflowStoreError(w, err) + return + } + s.auditFromRequest(r, "workflow_save", fmt.Sprintf("%s compiled=%v", e.Key(), e.Compiled), "") + jsonStatusResponse(w, http.StatusCreated, map[string]interface{}{"entry": e}) +} + +func (s *Server) handleWorkflowDelete(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + st := s.workflowStore(w) + if st == nil { + return + } + name, version := r.PathValue("name"), r.PathValue("version") + if s.deps.Config != nil { + for _, b := range s.deps.Config.Project.WorkflowBindings { + if b.Name == name && b.Version == version { + jsonError(w, fmt.Sprintf("%s@%s is bound to %s; unbind first", name, version, b.Repo), http.StatusConflict) + return + } + } + } + if err := st.Delete(name, version); err != nil { + workflowStoreError(w, err) + return + } + s.auditFromRequest(r, "workflow_delete", name+"@"+version, "") + okResponse(w, nil) +} + +func (s *Server) workflowBindings() []config.WorkflowBinding { + if s.deps == nil || s.deps.Config == nil || s.deps.Config.Project.WorkflowBindings == nil { + return []config.WorkflowBinding{} + } + return s.deps.Config.Project.WorkflowBindings +} + +func (s *Server) handleWorkflowBindingsGet(w http.ResponseWriter, r *http.Request) { + if s.deps == nil || s.deps.Config == nil { + jsonError(w, "config unavailable", http.StatusServiceUnavailable) + return + } + jsonResponse(w, map[string]interface{}{ + "bindings": s.workflowBindings(), + "enabled": s.deps.Config.Workflow.Enabled, + }) +} + +// handleWorkflowBindingPut binds (name+version set) or unbinds (name empty) a +// repository. A bound model must exist and compile so a stale binding can +// never leave the tracker without columns. +func (s *Server) handleWorkflowBindingPut(w http.ResponseWriter, r *http.Request) { + if !requireOwnerRole(w, r) { + return + } + if s.deps == nil || s.deps.Config == nil { + jsonError(w, "config unavailable", http.StatusServiceUnavailable) + return + } + var body config.WorkflowBinding + if err := decodeBody(r, &body); err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + action := "workflow_unbind" + if body.Name != "" { + st := s.workflowStore(w) + if st == nil { + return + } + if _, err := st.Profile(body.Name, body.Version); err != nil { + workflowStoreError(w, err) + return + } + action = "workflow_bind" + } + changed, err := s.deps.Config.SetWorkflowBinding(body.Repo, body.Name, body.Version) + if err != nil { + jsonError(w, err.Error(), http.StatusBadRequest) + return + } + if changed { + if err := s.saveConfig(); err != nil { + jsonError(w, "save config: "+err.Error(), http.StatusInternalServerError) + return + } + s.auditFromRequest(r, action, fmt.Sprintf("%s -> %s@%s", body.Repo, body.Name, body.Version), "") + } + jsonResponse(w, map[string]interface{}{"changed": changed, "bindings": s.workflowBindings()}) +} diff --git a/src/pkg/dashboard/api_workflows_test.go b/src/pkg/dashboard/api_workflows_test.go new file mode 100644 index 0000000000..bb9e077957 --- /dev/null +++ b/src/pkg/dashboard/api_workflows_test.go @@ -0,0 +1,278 @@ +package dashboard + +import ( + "bytes" + "encoding/json" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/hivedefault" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" +) + +func workflowTestServer(t *testing.T, withStore bool) *Server { + t.Helper() + logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelError})) + s := NewServer(0, logger) + deps := testDeps(t) + if withStore { + deps.WorkflowStore = wfstore.Open(t.TempDir()) + } + s.RegisterAPI(deps) + return s +} + +func techdocsModel(t *testing.T) *workflowmodel.Model { + t.Helper() + b, err := os.ReadFile("../workflowmodel/testdata/models/techdocs.json") + if err != nil { + t.Fatal(err) + } + var m workflowmodel.Model + if err := json.Unmarshal(b, &m); err != nil { + t.Fatal(err) + } + return &m +} + +func wfReq(s *Server, method, path string, body any, owner bool) *httptest.ResponseRecorder { + var buf bytes.Buffer + if body != nil { + if raw, ok := body.(string); ok { + buf.WriteString(raw) + } else { + _ = json.NewEncoder(&buf).Encode(body) + } + } + req := httptest.NewRequest(method, path, &buf) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Hive-User", "andy") + if owner { + req.Header.Set("X-Hive-Role", "owner") + req.Header.Set(ownerRoleVerifiedHeader, "true") + } + rec := httptest.NewRecorder() + s.mux.ServeHTTP(rec, req) + return rec +} + +func TestWorkflowAPIDisabledWithoutStore(t *testing.T) { + s := workflowTestServer(t, false) + for _, c := range []struct{ method, path string }{ + {http.MethodGet, "/api/workflows"}, + {http.MethodGet, "/api/workflows/x/1"}, + {http.MethodPost, "/api/workflows"}, + {http.MethodDelete, "/api/workflows/x/1"}, + } { + if rec := wfReq(s, c.method, c.path, nil, true); rec.Code != http.StatusServiceUnavailable { + t.Fatalf("%s %s: %d %s", c.method, c.path, rec.Code, rec.Body) + } + } + // compile needs no store + rec := wfReq(s, http.MethodPost, "/api/workflows/compile", techdocsModel(t), false) + var out struct{ Compiled bool } + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &out) != nil || !out.Compiled { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + // binding a model requires the store + rec = wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "repo1", "name": "x", "version": "1"}, true) + if rec.Code != http.StatusServiceUnavailable { + t.Fatalf("%d %s", rec.Code, rec.Body) + } +} + +func TestWorkflowAPIOwnerGate(t *testing.T) { + s := workflowTestServer(t, true) + for _, c := range []struct{ method, path string }{ + {http.MethodPost, "/api/workflows"}, + {http.MethodPut, "/api/workflows/x/1"}, + {http.MethodDelete, "/api/workflows/x/1"}, + {http.MethodPut, "/api/workflows/bindings"}, + } { + if rec := wfReq(s, c.method, c.path, techdocsModel(t), false); rec.Code != http.StatusForbidden { + t.Fatalf("%s %s: %d", c.method, c.path, rec.Code) + } + } +} + +func TestWorkflowAPILifecycle(t *testing.T) { + s := workflowTestServer(t, true) + m := techdocsModel(t) + + rec := wfReq(s, http.MethodGet, "/api/workflows", nil, false) + var list struct { + Workflows []wfstore.Entry + Bindings []json.RawMessage + Enabled bool + } + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &list) != nil || len(list.Workflows) != 1 || list.Bindings == nil || list.Enabled { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + + if rec := wfReq(s, http.MethodPost, "/api/workflows", "{", true); rec.Code != http.StatusBadRequest { + t.Fatalf("bad json: %d", rec.Code) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows", m, true); rec.Code != http.StatusCreated { + t.Fatalf("save: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPut, "/api/workflows/cncf-techdocs/9", m, true); rec.Code != http.StatusBadRequest { + t.Fatalf("path mismatch: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPut, "/api/workflows/cncf-techdocs/1", m, true); rec.Code != http.StatusCreated { + t.Fatalf("put: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows", hivedefault.Generate(), true); rec.Code != http.StatusForbidden { + t.Fatalf("builtin save: %d", rec.Code) + } + + rec = wfReq(s, http.MethodGet, "/api/workflows/cncf-techdocs/1", nil, false) + var got struct { + Entry wfstore.Entry + Profile *workflowmodel.Profile + } + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &got) != nil || got.Profile == nil || got.Entry.SavedBy != "andy" { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodGet, "/api/workflows/cncf-techdocs/2", nil, false); rec.Code != http.StatusNotFound { + t.Fatalf("missing: %d", rec.Code) + } + if rec := wfReq(s, http.MethodGet, "/api/workflows/Bad%20Name/2", nil, false); rec.Code != http.StatusBadRequest { + t.Fatalf("bad name: %d", rec.Code) + } + + // drafts save but cannot be bound + draft := techdocsModel(t) + draft.Spec.Meta.Version = "draft" + draft.Spec.Gates[0].ReleasedBy = "" + if rec := wfReq(s, http.MethodPost, "/api/workflows", draft, true); rec.Code != http.StatusCreated { + t.Fatalf("draft save: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "repo1", "name": "cncf-techdocs", "version": "draft"}, true); rec.Code != http.StatusBadRequest { + t.Fatalf("bind draft: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPut, "/api/workflows/bindings", "{", true); rec.Code != http.StatusBadRequest { + t.Fatalf("bind bad json: %d", rec.Code) + } + if rec := wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "", "name": "cncf-techdocs", "version": "1"}, true); rec.Code != http.StatusBadRequest { + t.Fatalf("bind empty repo: %d", rec.Code) + } + + rec = wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "repo1", "name": "cncf-techdocs", "version": "1"}, true) + var bind struct { + Changed bool + Bindings []struct{ Repo, Name, Version string } + } + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &bind) != nil || !bind.Changed || len(bind.Bindings) != 1 { + t.Fatalf("bind: %d %s", rec.Code, rec.Body) + } + if b, ok := s.deps.Config.WorkflowBindingFor("myorg/repo1"); !ok || b.Version != "1" { + t.Fatalf("%+v %v", b, ok) + } + rec = wfReq(s, http.MethodGet, "/api/workflows/bindings", nil, false) + if rec.Code != 200 || !bytes.Contains(rec.Body.Bytes(), []byte(`"cncf-techdocs"`)) { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + + // bound models cannot be deleted + if rec := wfReq(s, http.MethodDelete, "/api/workflows/cncf-techdocs/1", nil, true); rec.Code != http.StatusConflict { + t.Fatalf("delete bound: %d %s", rec.Code, rec.Body) + } + rec = wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "repo1"}, true) + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &bind) != nil || !bind.Changed || len(bind.Bindings) != 0 { + t.Fatalf("unbind: %d %s", rec.Code, rec.Body) + } + rec = wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "repo1"}, true) + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &bind) != nil || bind.Changed { + t.Fatalf("unbind again: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodDelete, "/api/workflows/cncf-techdocs/1", nil, true); rec.Code != 200 { + t.Fatalf("delete: %d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodDelete, "/api/workflows/cncf-techdocs/1", nil, true); rec.Code != http.StatusNotFound { + t.Fatalf("delete twice: %d", rec.Code) + } + if rec := wfReq(s, http.MethodDelete, "/api/workflows/hive-default/1", nil, true); rec.Code != http.StatusForbidden { + t.Fatalf("delete builtin: %d", rec.Code) + } + + // compile reports diagnostics for drafts and invalid shapes + rec = wfReq(s, http.MethodPost, "/api/workflows/compile", draft, false) + var comp struct { + Compiled bool + Diagnostics []workflowmodel.Diagnostic + } + if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &comp) != nil || comp.Compiled || len(comp.Diagnostics) == 0 { + t.Fatalf("%d %s", rec.Code, rec.Body) + } + if rec := wfReq(s, http.MethodPost, "/api/workflows/compile", "{", false); rec.Code != http.StatusBadRequest { + t.Fatalf("compile bad json: %d", rec.Code) + } +} + +func TestWorkflowBindingsWithoutConfig(t *testing.T) { + s := workflowTestServer(t, true) + s.deps.Config = nil + if rec := wfReq(s, http.MethodGet, "/api/workflows/bindings", nil, false); rec.Code != http.StatusServiceUnavailable { + t.Fatalf("%d", rec.Code) + } + if rec := wfReq(s, http.MethodPut, "/api/workflows/bindings", map[string]string{"repo": "r"}, true); rec.Code != http.StatusServiceUnavailable { + t.Fatalf("%d", rec.Code) + } + rec := wfReq(s, http.MethodGet, "/api/workflows", nil, false) + if rec.Code != 200 || !bytes.Contains(rec.Body.Bytes(), []byte(`"bindings":[]`)) { + t.Fatalf("%d %s", rec.Code, rec.Body) + } +} + +func TestStageTrackerWorkflowsBlock(t *testing.T) { + qualify := func(r string) string { return "myorg/" + r } + if stageTrackerWorkflows(nil, nil, qualify) != nil { + t.Fatal("nil config produced a block") + } + s := workflowTestServer(t, true) + if stageTrackerWorkflows(s.deps.Config, s.workflowProfileLookup(), qualify) != nil { + t.Fatal("unbound hive produced a block") + } + if rec := wfReq(s, http.MethodPost, "/api/workflows", techdocsModel(t), true); rec.Code != http.StatusCreated { + t.Fatal(rec.Body.String()) + } + if _, err := s.deps.Config.SetWorkflowBinding("repo1", "cncf-techdocs", "1"); err != nil { + t.Fatal(err) + } + if _, err := s.deps.Config.SetWorkflowBinding("repo2", "ghost", "1"); err != nil { + t.Fatal(err) + } + blk := stageTrackerWorkflows(s.deps.Config, s.workflowProfileLookup(), qualify) + if blk == nil || blk.Default.Name != hivedefault.Name || len(blk.Default.Columns) != 7 { + t.Fatalf("%+v", blk) + } + td := blk.Repos["myorg/repo1"] + if td.Name != "cncf-techdocs" || td.Error != "" || len(td.Columns) != 5 { + t.Fatalf("%+v", td) + } + var nums int + for _, c := range td.Columns { + nums += len(c.Numbers) + } + if nums == 0 { + t.Fatalf("no stage numbers anchored: %+v", td.Columns) + } + if g := blk.Repos["myorg/repo2"]; g.Error == "" || g.Name != "ghost" { + t.Fatalf("%+v", g) + } + noStore := stageTrackerWorkflows(s.deps.Config, nil, qualify) + if noStore.Repos["myorg/repo1"].Error == "" { + t.Fatalf("%+v", noStore.Repos) + } + // the tracker response carries it + resp := s.buildStageTracker(&StatusPayload{HiveID: "h"}, nil, time.Now()) + if resp.Workflows == nil || len(resp.Workflows.Repos) != 2 { + t.Fatalf("%+v", resp.Workflows) + } +} diff --git a/src/pkg/dashboard/deps.go b/src/pkg/dashboard/deps.go index 565544d6ff..a43aa6f9dc 100644 --- a/src/pkg/dashboard/deps.go +++ b/src/pkg/dashboard/deps.go @@ -2,6 +2,7 @@ package dashboard import ( "context" + wfstore "github.com/hivecommons/hive/pkg/workflowmodel/store" "log/slog" "net/http" "os" @@ -57,6 +58,9 @@ type ChatPoll struct { type Dependencies struct { Config *config.Config + // WorkflowStore serves the workflow-model library (hivecommons/hive#17620). + // Nil disables the /api/workflows endpoints. + WorkflowStore *wfstore.Store // AgentMgr is the agent runtime (hivecommons/hive#13536). The dashboard // programs against the crux.Runtime surface, not *agent.Manager; tests that // need manager-only seams unwrap it with agentManager. diff --git a/src/pkg/dashboard/import_count_test.go b/src/pkg/dashboard/import_count_test.go index 5a67cebfda..5bda3be660 100644 --- a/src/pkg/dashboard/import_count_test.go +++ b/src/pkg/dashboard/import_count_test.go @@ -72,8 +72,11 @@ func TestDashboardInternalImportCountRatchet(t *testing.T) { // surfaces render one markup (hivecommons/hive#16236), plus // pkg/maintainernotes, the append-only store behind the Activity // Monitor's private per-item maintainer notes and the /api/notes routes, - // shared with the scheduler's kick injection (hivecommons/hive#17244). - const maxDashboardInternalImports = 67 + // shared with the scheduler's kick injection (hivecommons/hive#17244), + // plus pkg/workflowmodel/store, the spoke-local library of workflow + // models behind the /api/workflows routes and the tracker's per-repo + // column block (RFC hivecommons/hive#17620, phase 2). + const maxDashboardInternalImports = 68 entries, err := os.ReadDir(".") if err != nil { diff --git a/src/pkg/dashboard/settings_parity_allowlist.txt b/src/pkg/dashboard/settings_parity_allowlist.txt index c5fe0c8734..60aa9f75cc 100644 --- a/src/pkg/dashboard/settings_parity_allowlist.txt +++ b/src/pkg/dashboard/settings_parity_allowlist.txt @@ -709,3 +709,7 @@ governor.hotspot_min_lines # merge-hotspot advisor; yaml-only governor.hotspot_refile_days # merge-hotspot advisor; yaml-only governor.hotspot_max_open # merge-hotspot advisor; yaml-only governor.hotspot_max_per_pass # merge-hotspot advisor; yaml-only +data.workflows_dir # workflow-model store root; deployment knob, no Settings control planned (RFC #17620) +project.workflow_bindings[].name # set through the per-repo workflow select on Settings → Repos via PUT /api/workflows/bindings, not a leaf-path control (RFC #17620) +project.workflow_bindings[].repo # see project.workflow_bindings[].name +project.workflow_bindings[].version # see project.workflow_bindings[].name diff --git a/src/pkg/dashboard/stage_tracker.go b/src/pkg/dashboard/stage_tracker.go index 0bf70f091e..24b18d87c9 100644 --- a/src/pkg/dashboard/stage_tracker.go +++ b/src/pkg/dashboard/stage_tracker.go @@ -77,6 +77,10 @@ type StageTrackerResponse struct { // Engine is the ADR-0025 central stage engine's mode and counters for // the tracker's status strip; absent when the hive has no engine. Engine *stageengine.Status `json:"engine,omitempty"` + // Workflows names the workflow model each bound repository follows and + // its column layout (hivecommons/hive#17620); absent when no repository + // is bound. + Workflows *StageTrackerWorkflows `json:"workflows,omitempty"` // RecentlyMerged lists the pull requests GitHub reports merged within // stageTrackerMergedWindow, whoever merged them: the audit log's // pr_merged entries (the hive's own sweep, queue, relay and forge diff --git a/src/pkg/dashboard/stage_tracker_build.go b/src/pkg/dashboard/stage_tracker_build.go index caa5694ef0..2f7fdef85d 100644 --- a/src/pkg/dashboard/stage_tracker_build.go +++ b/src/pkg/dashboard/stage_tracker_build.go @@ -57,6 +57,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto cfg = s.deps.Config } resp.NeedsHumanLabels = needsHumanPresetLabels(cfg, status.HiveID) + resp.Workflows = stageTrackerWorkflows(cfg, s.workflowProfileLookup(), qualifyRepo) expectedPerspectives := stageTrackerExpectedPerspectives(cfg) // A Review track only waits on "the hive reviewer" when this hive's // planner dispatches one; with the swarm off it says a person reviews. diff --git a/src/pkg/dashboard/stage_tracker_workflows.go b/src/pkg/dashboard/stage_tracker_workflows.go new file mode 100644 index 0000000000..3e0d593361 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_workflows.go @@ -0,0 +1,89 @@ +package dashboard + +import ( + "sort" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/hivedefault" +) + +// StageTrackerWorkflows is the tracker's `workflows` block +// (hivecommons/hive#17620, RFC phase 2): which model each bound repository +// follows and the column/state layout the SPA renders for it. Absent when no +// repository is bound, so the v5/v6 payload is unchanged for every hive that +// has not opted in. +type StageTrackerWorkflows struct { + // Default is the hive-default model every unbound repository follows. + Default StageTrackerWorkflow `json:"default"` + // Repos maps an org-qualified repository to its bound model. Repos + // bound to a model that is missing or no longer compiles are listed + // with Error set and fall back to Default in the SPA. + Repos map[string]StageTrackerWorkflow `json:"repos"` +} + +// StageTrackerWorkflow is one model's identity and column layout. +type StageTrackerWorkflow struct { + Name string `json:"name"` + Version string `json:"version"` + Hash string `json:"hash,omitempty"` + Columns []StageTrackerWorkflowColumn `json:"columns,omitempty"` + Error string `json:"error,omitempty"` +} + +// StageTrackerWorkflowColumn is a column with the stage numbers it anchors, +// so the SPA can place an item by its stage/NNN number without knowing the +// model's state ids. +type StageTrackerWorkflowColumn struct { + ID string `json:"id"` + Label string `json:"label"` + Hint string `json:"hint,omitempty"` + Terminal bool `json:"terminal,omitempty"` + States []string `json:"states"` + Numbers []int `json:"numbers"` +} + +// workflowProfileLookup resolves a bound name@version to its compiled +// profile; the store's Profile method satisfies it. +type workflowProfileLookup func(name, version string) (*workflowmodel.Profile, error) + +func stageTrackerWorkflowBlock(p *workflowmodel.Profile) StageTrackerWorkflow { + out := StageTrackerWorkflow{Name: p.Name, Version: p.Version, Hash: p.Hash} + for _, c := range p.Columns { + col := StageTrackerWorkflowColumn{ID: c.ID, Label: c.Label, Hint: c.Hint, Terminal: c.Terminal, States: append([]string{}, c.States...), Numbers: []int{}} + for _, id := range c.States { + if st := p.States[id]; st != nil && st.Lifecycle.Number > 0 { + col.Numbers = append(col.Numbers, st.Lifecycle.Number) + } + } + sort.Ints(col.Numbers) + out.Columns = append(out.Columns, col) + } + return out +} + +// stageTrackerWorkflows builds the block from the config's bindings; nil when +// there are none. +func stageTrackerWorkflows(cfg *config.Config, lookup workflowProfileLookup, qualify func(string) string) *StageTrackerWorkflows { + if cfg == nil || len(cfg.Project.WorkflowBindings) == 0 { + return nil + } + out := &StageTrackerWorkflows{ + Default: stageTrackerWorkflowBlock(hivedefault.Profile()), + Repos: map[string]StageTrackerWorkflow{}, + } + for _, b := range cfg.Project.WorkflowBindings { + repo := qualify(b.Repo) + if lookup == nil { + out.Repos[repo] = StageTrackerWorkflow{Name: b.Name, Version: b.Version, Error: "workflow store not enabled"} + continue + } + p, err := lookup(b.Name, b.Version) + if err != nil { + out.Repos[repo] = StageTrackerWorkflow{Name: b.Name, Version: b.Version, Error: err.Error()} + continue + } + out.Repos[repo] = stageTrackerWorkflowBlock(p) + } + return out +} diff --git a/src/pkg/dashboard/stage_tracker_workflows_ui_test.go b/src/pkg/dashboard/stage_tracker_workflows_ui_test.go new file mode 100644 index 0000000000..1e2d2a1ce9 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_workflows_ui_test.go @@ -0,0 +1,101 @@ +package dashboard + +import ( + "os/exec" + "strings" + "testing" +) + +// TestStageTrackerWorkflowColumnsWiring pins the RFC phase-2 surface +// (hivecommons/hive#17620): the tracker reads `workflows` from the payload, +// a bound repository's group header carries the model badge and its own +// column header, and the row strip switches to the model's columns only +// when stageTrackerWorkflowOf resolves one. +func TestStageTrackerWorkflowColumnsWiring(t *testing.T) { + html := indexHTML(t) + for _, want := range []string{ + `ctx.workflows = d.workflows || null;`, + `function stageTrackerWorkflowOf(ctx, repo)`, + `function stageTrackerWorkflowColumnIndex(wf, stage)`, + `function stageTrackerWorkflowStripItems(item, wf, stages)`, + `function stageTrackerWorkflowHeaderHtml(wf)`, + `const rowWorkflow = typeof stageTrackerWorkflowOf === 'function' ? stageTrackerWorkflowOf(ctx, item.repo) : null;`, + `${rowWorkflow ? stageTrackerWorkflowStripItems(shown, rowWorkflow, stages) : stageTrackerStripItems(`, + `const groupWorkflow = typeof stageTrackerWorkflowOf === 'function' ? stageTrackerWorkflowOf(ctx, g.repo) : null;`, + `${groupWorkflow && !g.collapsed ? stageTrackerWorkflowHeaderHtml(groupWorkflow) : ''}`, + `.ham-wf-badge {`, + `.ham-seg.ham-wf-uncovered {`, + // Settings → Repos binding select. + `function renderRepoWorkflowRow(repo)`, + `async function setRepoWorkflow(repo, value)`, + `fetch('/api/workflows/bindings', {`, + `+ ldSub + renderRepoWorkflowRow(r);`, + } { + if !strings.Contains(html, want) { + t.Errorf("index.html missing workflow-columns wiring %q", want) + } + } +} + +// The pure helpers under node: an unbound hive resolves no model (so the +// built-in strip is untouched), hive-default itself resolves none, a bound +// repository places rows by stage number into the model's columns, a +// terminal stage lights the terminal column, and an uncovered number is +// drawn as "Not covered" rather than dropped. +func TestStageTrackerWorkflowColumnsNode(t *testing.T) { + node, err := exec.LookPath("node") + if err != nil { + t.Skip("node not installed") + } + html := indexHTML(t) + script := `const assert = require('assert'); +const STAGE_TRACKER_TERMINAL = 110; +function esc(s) { return String(s).replace(/&/g, '&').replace(/ html.match(/
  • m.slice(11, -1)); +const words = html => [...html.matchAll(/([^<]*)<\/span>/g)].map(m => m[1]); +let s = stageTrackerWorkflowStripItems({ stage: 45 }, td, stages); +assert.deepStrictEqual(li(s), ['ham-seg ham-wf-seg done', 'ham-seg ham-wf-seg active', 'ham-seg ham-wf-seg todo', 'ham-seg ham-wf-seg todo cap']); +assert.deepStrictEqual(words(s), ['Requested', 'Decision', 'Writing', 'Published'], 'active column wears the stage word'); +s = stageTrackerWorkflowStripItems({ stage: 110 }, td, stages); +assert.deepStrictEqual(li(s), ['ham-seg ham-wf-seg done', 'ham-seg ham-wf-seg done', 'ham-seg ham-wf-seg done', 'ham-seg ham-wf-seg active cap']); +s = stageTrackerWorkflowStripItems({ stage: 85 }, td, stages); +assert.ok(li(s)[0].includes('ham-wf-uncovered') && words(s)[0] === 'Not covered', s); +assert.ok(s.includes('stage/085 is not a state of cncf-techdocs@1')); +s = stageTrackerWorkflowStripItems({ stage: 0 }, td, stages); +assert.deepStrictEqual(li(s)[0], 'ham-seg ham-wf-seg active'); + +const head = stageTrackerWorkflowHeaderHtml(td); +assert.ok(head.includes('data-ham-workflow="cncf-techdocs@1"') && head.includes('title="Asked for"') && li(head).length === 4, head); +assert.ok(stageTrackerWorkflowBadgeHtml(td).includes('>cncf-techdocs@1<')); +assert.strictEqual(stageTrackerWorkflowBadgeHtml(null), ''); +` + if out, err := runNodeStdin(node, script); err != nil { + t.Fatalf("node workflow columns fixture failed: %v\n%s", err, out) + } +} diff --git a/src/pkg/dashboard/static-src/css/tracker.css b/src/pkg/dashboard/static-src/css/tracker.css index b7c729d01a..6bff2639ba 100644 --- a/src/pkg/dashboard/static-src/css/tracker.css +++ b/src/pkg/dashboard/static-src/css/tracker.css @@ -267,6 +267,13 @@ .ham-group-n, .ham-group-attn { flex: 0 0 auto; font-size: var(--fs-xs); color: var(--text-faint); white-space: nowrap; } .ham-group-attn { color: var(--amber); } .ham-group-collapsed .ham-group-name { color: var(--muted); } + /* Workflow-model columns (RFC 17620): the model badge on a + bound repository's group header, its own column header, and the + "Not covered" chevron for a stage the model has no state for. */ + .ham-wf-badge { flex: 0 0 auto; font-family: var(--font-mono); font-size: var(--fs-2xs); font-weight: 700; letter-spacing: 0.02em; padding: 1px 7px; border-radius: var(--r-pill); white-space: nowrap; color: var(--indigo); background: color-mix(in srgb, var(--indigo) 12%, transparent); border: 1px solid color-mix(in srgb, var(--indigo) 40%, transparent); } + .ham-wf-cols { position: static; } + .ham-wf-cols .ham-wf-col { color: var(--indigo); } + .ham-seg.ham-wf-uncovered { background: color-mix(in srgb, var(--amber) 18%, transparent); color: var(--amber); outline: 1px dashed var(--amber); } .ham-group-dist { flex: 0 0 auto; margin-left: auto; margin-right: calc(4em + var(--sp-3)); display: inline-flex; align-items: flex-end; gap: var(--sp-1); height: 0.9em; } .ham-gd { display: inline-block; width: 0.45em; height: 15%; border-radius: var(--sp-1); background: color-mix(in srgb, var(--text-faint) 35%, transparent); } .ham-gd-1 { height: 35%; background: color-mix(in srgb, var(--amber) 55%, transparent); } diff --git a/src/pkg/dashboard/static-src/js/config-governor.js b/src/pkg/dashboard/static-src/js/config-governor.js index 49d70fd669..e9ed9d8375 100644 --- a/src/pkg/dashboard/static-src/js/config-governor.js +++ b/src/pkg/dashboard/static-src/js/config-governor.js @@ -1104,7 +1104,7 @@ `; // Fully forge-qualified so the row is unambiguous: github.ibm.com/org/repo. const displayName = ghHost + '/' + r; - return `
    ${isDefault ? '★' : '☆'}${esc(displayName)}${inheritButton}${rtRow}${cbRow}${pillLabel}
    ` + ldSub; + return `
    ${isDefault ? '★' : '☆'}${esc(displayName)}${inheritButton}${rtRow}${cbRow}${pillLabel}
    ` + ldSub + renderRepoWorkflowRow(r); }).join(''); const hostNote = isGHE ? ` on ${esc(ghHost)} (GitHub Enterprise)` diff --git a/src/pkg/dashboard/static-src/js/config-workflows.js b/src/pkg/dashboard/static-src/js/config-workflows.js new file mode 100644 index 0000000000..407bed42df --- /dev/null +++ b/src/pkg/dashboard/static-src/js/config-workflows.js @@ -0,0 +1,96 @@ + // Workflow-model bindings (hivecommons/hive#17620, RFC phase 2). + // + // Each repo row on Settings → Repos carries a "Workflow" select: hive-default + // (the built-in lifecycle every unbound repo follows) or any compiled model + // from the library. Binding persists immediately through + // PUT /api/workflows/bindings (owner-only, audited) — it is not part of the + // tab's Save button, because a binding is validated server-side against the + // store (the model must exist and compile) and the tracker picks the new + // columns up on its next poll. + const WORKFLOW_DEFAULT_KEY = 'hive-default@1'; + const _workflowState = { loaded: false, loading: false, enabled: false, workflows: [], bindings: [] }; + + function workflowKey(name, version) { return (name || '') + '@' + (version || ''); } + + async function loadWorkflowLibrary(onDone) { + if (_workflowState.loading) return; + _workflowState.loading = true; + try { + const res = await fetch('/api/workflows', { headers: _inceptionAuthHeaders() }); + if (res.status === 503) { _workflowState.loaded = true; _workflowState.workflows = []; return; } + if (!res.ok) throw new Error(await saveErrorMessage(res)); + const data = await res.json(); + _workflowState.workflows = Array.isArray(data.workflows) ? data.workflows : []; + _workflowState.bindings = Array.isArray(data.bindings) ? data.bindings : []; + _workflowState.enabled = !!data.enabled; + _workflowState.loaded = true; + } catch (err) { + _workflowState.loaded = true; + showToast('Could not load the workflow library: ' + err.message, 'error'); + } finally { + _workflowState.loading = false; + if (typeof onDone === 'function') onDone(); + } + } + + function workflowBindingFor(repo) { + const want = String(repo || '').toLowerCase(); + const org = String((_configState.data && _configState.data.org) || '').toLowerCase(); + return _workflowState.bindings.find(b => { + const r = String(b.repo || '').toLowerCase(); + return r === want || (org && (r === org + '/' + want || want === org + '/' + r)); + }) || null; + } + + // renderRepoWorkflowRow returns the per-repo select, or '' when the hive + // has no workflow store (the row simply does not appear; nothing else on + // the tab changes). + function renderRepoWorkflowRow(repo) { + if (!_workflowState.loaded) { + loadWorkflowLibrary(() => { if (_configState.tab === 'Repos') renderConfigTab('Repos'); }); + return ''; + } + if (!_workflowState.workflows.length) return ''; + const bound = workflowBindingFor(repo); + const current = bound ? workflowKey(bound.name, bound.version) : WORKFLOW_DEFAULT_KEY; + const locked = !dashboardRoleAtLeast(window._hiveRole || 'read', 'owner'); + const options = _workflowState.workflows.map(w => { + const key = workflowKey(w.name, w.version); + const compiled = w.compiled !== false; + const label = key + (w.description ? ' — ' + w.description : '') + (compiled ? '' : ' (draft, does not compile)'); + return ``; + }).join(''); + const flag = _workflowState.enabled + ? '' + : 'derivation off'; + return `
    ${flag}${bound ? `bound` : 'default'}
    `; + } + + async function setRepoWorkflow(repo, value) { + if (!dashboardRoleAtLeast(window._hiveRole || 'read', 'owner')) { + showToast('Owner role is required to bind a workflow.', 'error'); + renderConfigTab('Repos'); + return; + } + const key = String(value || ''); + const body = { repo: repo }; + if (key && key !== WORKFLOW_DEFAULT_KEY) { + const at = key.lastIndexOf('@'); + body.name = key.slice(0, at); + body.version = key.slice(at + 1); + } + try { + const res = await fetch('/api/workflows/bindings', { + method: 'PUT', + headers: _inceptionAuthHeaders(), + body: JSON.stringify(body), + }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + const data = await res.json(); + _workflowState.bindings = Array.isArray(data.bindings) ? data.bindings : []; + showToast(body.name ? `${repo} now follows ${key}` : `${repo} back on hive-default`); + } catch (err) { + showToast('Failed to bind workflow: ' + err.message, 'error'); + } + renderConfigTab('Repos'); + } diff --git a/src/pkg/dashboard/static-src/js/tracker-filter-render.js b/src/pkg/dashboard/static-src/js/tracker-filter-render.js index c406c9ff17..740f74b828 100644 --- a/src/pkg/dashboard/static-src/js/tracker-filter-render.js +++ b/src/pkg/dashboard/static-src/js/tracker-filter-render.js @@ -87,6 +87,8 @@ } stageTrackerSyncToolbar(f); const ctx = { nowMs, stages, moves: _stageTrackerMoves }; + // Workflow-model columns for bound repositories (hivecommons/hive#17620). + ctx.workflows = d.workflows || null; ctx.workers = stageTrackerWorkers(window._lastStatus); ctx.events = _stageTrackerEvents; ctx.noteTuck = stageTrackerNoteTuckSetting(); diff --git a/src/pkg/dashboard/static-src/js/tracker-row-render.js b/src/pkg/dashboard/static-src/js/tracker-row-render.js index a4f87184a9..ee7a393061 100644 --- a/src/pkg/dashboard/static-src/js/tracker-row-render.js +++ b/src/pkg/dashboard/static-src/js/tracker-row-render.js @@ -63,6 +63,7 @@ // needs-attention count and a mini per-column distribution. Keyed // `group:owner/repo` by the render so the reconcile keeps it stable. function stageTrackerGroupHeaderHtml(g, stages, ctx) { + const groupWorkflow = typeof stageTrackerWorkflowOf === 'function' ? stageTrackerWorkflowOf(ctx, g.repo) : null; const max = Math.max(1, ...g.cols); const dist = g.cols.map((c, i) => ``).join(''); const attn = g.attention ? `${g.attention} need${g.attention === 1 ? 's' : ''} attention` : ''; @@ -70,10 +71,10 @@ const chipCtx = { repoChips: Object.assign({}, (ctx && ctx.repoChips) || {}) }; return `
    - ${stageTrackerRepoChipHtml(g.repo, chipCtx)}${esc(g.repo)} + ${stageTrackerRepoChipHtml(g.repo, chipCtx)}${esc(g.repo)}${groupWorkflow ? stageTrackerWorkflowBadgeHtml(groupWorkflow) : ''} ${g.count} ${g.count === 1 ? 'row' : 'rows'}${attn} - ${dist} -
    `; + ${groupWorkflow ? '' : `${dist}`} + ${groupWorkflow && !g.collapsed ? stageTrackerWorkflowHeaderHtml(groupWorkflow) : ''}`; } // Row = title line (number, title, chips, since) + thin strip. Non-active @@ -196,6 +197,9 @@ // One note line per row: the human line takes the stage note (folded // or dropped), so the event slot below the caption renders nothing. const rowKey = stageTrackerItemKey(item); + // A repository bound to a workflow model draws the model's columns + // (tracker-workflows.js); null keeps the built-in strip byte for byte. + const rowWorkflow = typeof stageTrackerWorkflowOf === 'function' ? stageTrackerWorkflowOf(ctx, item.repo) : null; const humanNote = typeof stageTrackerHumanContextHtml === 'function' ? stageTrackerHumanContextHtml(item, stages, nowMs, subRows, ctx, rowKey) : ''; const noteCtx = humanNote && typeof stageTrackerNoteTakenCtx === 'function' ? stageTrackerNoteTakenCtx(ctx, rowKey) : ctx; // One human-gate pill per gated row (hivecommons/hive#17470): it @@ -210,7 +214,7 @@
    ${typeof stageTrackerPreparationNodeHtml === 'function' ? stageTrackerPreparationNodeHtml(item, stages, ctx) : ''} -
      ${stageTrackerStripItems(shown, stages, range, nowMs, sharedUnder ? Object.assign({}, ctx, { gateElsewhere: true }) : ctx)}
    +
      ${rowWorkflow ? stageTrackerWorkflowStripItems(shown, rowWorkflow, stages) : stageTrackerStripItems(shown, stages, range, nowMs, sharedUnder ? Object.assign({}, ctx, { gateElsewhere: true }) : ctx)}
    ${stageTrackerSinceHtml(shown, nowMs)}${typeof stageTrackerPillHtml === 'function' ? stageTrackerPillHtml(shown, stages, range, nowMs, ctx) : ''}
    ${humanNote}${stageTrackerCurrentStageHtml(shown, stages, range, nowMs)}${typeof stageTrackerPreparationCommentHtml === 'function' ? stageTrackerPreparationCommentHtml(shown, noteCtx, rowKey) : typeof stageTrackerEventHtml === 'function' ? stageTrackerEventHtml(rowKey, noteCtx) : ''}${typeof stageTrackerExecutionHtml === 'function' ? stageTrackerExecutionHtml(shown) : ''}${typeof stageTrackerReviewNotesHtml === 'function' ? stageTrackerReviewNotesHtml(item, ctx) : ''}${typeof stageTrackerNotesPanelHtml === 'function' ? stageTrackerNotesPanelHtml(item, hamNotesCtx(item)) : ''}${subs} `; diff --git a/src/pkg/dashboard/static-src/js/tracker-workflows.js b/src/pkg/dashboard/static-src/js/tracker-workflows.js new file mode 100644 index 0000000000..df16e47b27 --- /dev/null +++ b/src/pkg/dashboard/static-src/js/tracker-workflows.js @@ -0,0 +1,77 @@ + /* ---- Workflow-model columns (hivecommons/hive#17620, RFC phase 2) ----- + GET /api/stage-tracker carries `workflows` only when at least one + repository is bound to a model other than hive-default. For such a + repository the group header names the model and draws the model's own + column header, and each row's strip is one chevron per model column, + placed by the stage/NNN number the row wears. Everything else on the + row (lead cell, chips, human gate, notes) is unchanged. Hives with no + binding never reach this code: stageTrackerWorkflowOf returns null and + the built-in strip renders exactly as before. Pure helpers so the node + harness (stage_tracker_workflows_ui_test.go) can run them. */ + const STAGE_TRACKER_WORKFLOW_UNCOVERED_WORD = 'Not covered'; + + // The model block for repo, or null when the repo follows hive-default + // (unbound, bound to hive-default itself, or bound to a model the + // server reported as unusable — the row then falls back to the + // built-in strip, which is what the engine does too). + function stageTrackerWorkflowOf(ctx, repo) { + const w = ctx && ctx.workflows; + if (!w || !w.repos) return null; + const wf = w.repos[String(repo || '')]; + if (!wf || wf.error || !Array.isArray(wf.columns) || !wf.columns.length) return null; + if (w.default && wf.name === w.default.name && wf.version === w.default.version) return null; + return wf; + } + + // Index of the column holding stage number `stage`; 0 for an unstaged + // item (the first column is the model's entry), the terminal column for + // a terminal stage, -1 when no column anchors the number (the model + // does not cover it: the engine abstained with "workflow: not covered"). + function stageTrackerWorkflowColumnIndex(wf, stage) { + const n = Number(stage) || 0; + const cols = wf.columns; + if (n === 0) return 0; + if (n >= (typeof STAGE_TRACKER_TERMINAL === 'number' ? STAGE_TRACKER_TERMINAL : 110)) { + const t = cols.findIndex(c => c.terminal); + return t >= 0 ? t : cols.length - 1; + } + return cols.findIndex(c => Array.isArray(c.numbers) && c.numbers.indexOf(n) >= 0); + } + + function stageTrackerWorkflowBadgeHtml(wf) { + if (!wf) return ''; + const key = `${wf.name}@${wf.version}`; + return `${esc(key)}`; + } + + // The model's column header, drawn under a bound repository's group + // header in place of the shared hive-default header. + function stageTrackerWorkflowHeaderHtml(wf) { + const cols = wf.columns.map(c => `
  • ${esc(c.label)}
  • `).join(''); + return ``; + } + + // One row's strip under its model: done before the anchored column, + // active at it (the stage's own word from `stages`, or the column + // label), todo after. An uncovered stage lights the first column amber + // with the "Not covered" word so the gap is visible, not silent. + function stageTrackerWorkflowStripItems(item, wf, stages) { + const cur = Number(item.stage) || 0; + const terminal = cur >= (typeof STAGE_TRACKER_TERMINAL === 'number' ? STAGE_TRACKER_TERMINAL : 110); + let at = stageTrackerWorkflowColumnIndex(wf, cur); + const uncovered = at < 0; + if (uncovered) at = 0; + const st = (stages || []).find(s => Number(s.number) === cur) || {}; + return wf.columns.map((c, i) => { + let state = 'todo'; + if (terminal || i < at) state = 'done'; + else if (i === at) state = 'active'; + if (terminal && c.terminal) state = 'active'; + const word = state !== 'active' ? c.label : (uncovered ? STAGE_TRACKER_WORKFLOW_UNCOVERED_WORD : (st.short || st.label || c.label)); + const tip = state === 'active' && uncovered + ? `stage/${String(cur).padStart(3, '0')} is not a state of ${wf.name}@${wf.version}; the engine leaves this item where it is.` + : (c.hint || c.label) + (state === 'active' && cur ? ` — stage/${String(cur).padStart(3, '0')}` : ''); + const cls = ['ham-seg', 'ham-wf-seg', state, c.terminal ? 'cap' : '', uncovered && state === 'active' ? 'ham-wf-uncovered' : ''].filter(Boolean).join(' '); + return `
  • ${esc(word)}
  • `; + }).join(''); + } diff --git a/src/pkg/dashboard/static-src/manifest.txt b/src/pkg/dashboard/static-src/manifest.txt index 2ba70649aa..2d894fd79b 100644 --- a/src/pkg/dashboard/static-src/manifest.txt +++ b/src/pkg/dashboard/static-src/manifest.txt @@ -75,6 +75,7 @@ js/tracker-words.js js/tracker-row-anchor.js js/tracker-row.js js/tracker-row-strip.js +js/tracker-workflows.js js/tracker-row-acted.js js/tracker-human-attention.js js/tracker-row-layout.js @@ -133,6 +134,7 @@ js/config-tabs.js js/config-hub.js js/config-agent.js js/config-governor.js +js/config-workflows.js js/config-watchdog-security.js js/config-features.js js/integration-execution-settings.js diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 40fe22596d..4b6561263f 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -1612,6 +1612,13 @@ .ham-group-n, .ham-group-attn { flex: 0 0 auto; font-size: var(--fs-xs); color: var(--text-faint); white-space: nowrap; } .ham-group-attn { color: var(--amber); } .ham-group-collapsed .ham-group-name { color: var(--muted); } + /* Workflow-model columns (RFC 17620): the model badge on a + bound repository's group header, its own column header, and the + "Not covered" chevron for a stage the model has no state for. */ + .ham-wf-badge { flex: 0 0 auto; font-family: var(--font-mono); font-size: var(--fs-2xs); font-weight: 700; letter-spacing: 0.02em; padding: 1px 7px; border-radius: var(--r-pill); white-space: nowrap; color: var(--indigo); background: color-mix(in srgb, var(--indigo) 12%, transparent); border: 1px solid color-mix(in srgb, var(--indigo) 40%, transparent); } + .ham-wf-cols { position: static; } + .ham-wf-cols .ham-wf-col { color: var(--indigo); } + .ham-seg.ham-wf-uncovered { background: color-mix(in srgb, var(--amber) 18%, transparent); color: var(--amber); outline: 1px dashed var(--amber); } .ham-group-dist { flex: 0 0 auto; margin-left: auto; margin-right: calc(4em + var(--sp-3)); display: inline-flex; align-items: flex-end; gap: var(--sp-1); height: 0.9em; } .ham-gd { display: inline-block; width: 0.45em; height: 15%; border-radius: var(--sp-1); background: color-mix(in srgb, var(--text-faint) 35%, transparent); } .ham-gd-1 { height: 35%; background: color-mix(in srgb, var(--amber) 55%, transparent); } @@ -20019,6 +20026,83 @@

    Where to get help

    const rm = item && item.rebase_merge; return !!(rm && (rm.state === 'approved' || rm.state === 'rebasing' || rm.state === 'ci-pending')); } + /* ---- Workflow-model columns (hivecommons/hive#17620, RFC phase 2) ----- + GET /api/stage-tracker carries `workflows` only when at least one + repository is bound to a model other than hive-default. For such a + repository the group header names the model and draws the model's own + column header, and each row's strip is one chevron per model column, + placed by the stage/NNN number the row wears. Everything else on the + row (lead cell, chips, human gate, notes) is unchanged. Hives with no + binding never reach this code: stageTrackerWorkflowOf returns null and + the built-in strip renders exactly as before. Pure helpers so the node + harness (stage_tracker_workflows_ui_test.go) can run them. */ + const STAGE_TRACKER_WORKFLOW_UNCOVERED_WORD = 'Not covered'; + + // The model block for repo, or null when the repo follows hive-default + // (unbound, bound to hive-default itself, or bound to a model the + // server reported as unusable — the row then falls back to the + // built-in strip, which is what the engine does too). + function stageTrackerWorkflowOf(ctx, repo) { + const w = ctx && ctx.workflows; + if (!w || !w.repos) return null; + const wf = w.repos[String(repo || '')]; + if (!wf || wf.error || !Array.isArray(wf.columns) || !wf.columns.length) return null; + if (w.default && wf.name === w.default.name && wf.version === w.default.version) return null; + return wf; + } + + // Index of the column holding stage number `stage`; 0 for an unstaged + // item (the first column is the model's entry), the terminal column for + // a terminal stage, -1 when no column anchors the number (the model + // does not cover it: the engine abstained with "workflow: not covered"). + function stageTrackerWorkflowColumnIndex(wf, stage) { + const n = Number(stage) || 0; + const cols = wf.columns; + if (n === 0) return 0; + if (n >= (typeof STAGE_TRACKER_TERMINAL === 'number' ? STAGE_TRACKER_TERMINAL : 110)) { + const t = cols.findIndex(c => c.terminal); + return t >= 0 ? t : cols.length - 1; + } + return cols.findIndex(c => Array.isArray(c.numbers) && c.numbers.indexOf(n) >= 0); + } + + function stageTrackerWorkflowBadgeHtml(wf) { + if (!wf) return ''; + const key = `${wf.name}@${wf.version}`; + return `${esc(key)}`; + } + + // The model's column header, drawn under a bound repository's group + // header in place of the shared hive-default header. + function stageTrackerWorkflowHeaderHtml(wf) { + const cols = wf.columns.map(c => `
  • ${esc(c.label)}
  • `).join(''); + return ``; + } + + // One row's strip under its model: done before the anchored column, + // active at it (the stage's own word from `stages`, or the column + // label), todo after. An uncovered stage lights the first column amber + // with the "Not covered" word so the gap is visible, not silent. + function stageTrackerWorkflowStripItems(item, wf, stages) { + const cur = Number(item.stage) || 0; + const terminal = cur >= (typeof STAGE_TRACKER_TERMINAL === 'number' ? STAGE_TRACKER_TERMINAL : 110); + let at = stageTrackerWorkflowColumnIndex(wf, cur); + const uncovered = at < 0; + if (uncovered) at = 0; + const st = (stages || []).find(s => Number(s.number) === cur) || {}; + return wf.columns.map((c, i) => { + let state = 'todo'; + if (terminal || i < at) state = 'done'; + else if (i === at) state = 'active'; + if (terminal && c.terminal) state = 'active'; + const word = state !== 'active' ? c.label : (uncovered ? STAGE_TRACKER_WORKFLOW_UNCOVERED_WORD : (st.short || st.label || c.label)); + const tip = state === 'active' && uncovered + ? `stage/${String(cur).padStart(3, '0')} is not a state of ${wf.name}@${wf.version}; the engine leaves this item where it is.` + : (c.hint || c.label) + (state === 'active' && cur ? ` — stage/${String(cur).padStart(3, '0')}` : ''); + const cls = ['ham-seg', 'ham-wf-seg', state, c.terminal ? 'cap' : '', uncovered && state === 'active' ? 'ham-wf-uncovered' : ''].filter(Boolean).join(' '); + return `
  • ${esc(word)}
  • `; + }).join(''); + } const STAGE_TRACKER_ACTED_STATES = ['waiting', 'addressed', 'stuck', 'pending', 'failed', 'executing', 'blocked']; const STAGE_TRACKER_ACTED_FILTERS = ['any', 'awaiting', 'acted', 'stuck']; const STAGE_TRACKER_ACTED_STUCK_DEFAULT_S = 30 * 60; @@ -20965,6 +21049,7 @@

    Where to get help

    // needs-attention count and a mini per-column distribution. Keyed // `group:owner/repo` by the render so the reconcile keeps it stable. function stageTrackerGroupHeaderHtml(g, stages, ctx) { + const groupWorkflow = typeof stageTrackerWorkflowOf === 'function' ? stageTrackerWorkflowOf(ctx, g.repo) : null; const max = Math.max(1, ...g.cols); const dist = g.cols.map((c, i) => ``).join(''); const attn = g.attention ? `${g.attention} need${g.attention === 1 ? 's' : ''} attention` : ''; @@ -20972,10 +21057,10 @@

    Where to get help

    const chipCtx = { repoChips: Object.assign({}, (ctx && ctx.repoChips) || {}) }; return `
    - ${stageTrackerRepoChipHtml(g.repo, chipCtx)}${esc(g.repo)} + ${stageTrackerRepoChipHtml(g.repo, chipCtx)}${esc(g.repo)}${groupWorkflow ? stageTrackerWorkflowBadgeHtml(groupWorkflow) : ''} ${g.count} ${g.count === 1 ? 'row' : 'rows'}${attn} - ${dist} -
    `; + ${groupWorkflow ? '' : `${dist}`} + ${groupWorkflow && !g.collapsed ? stageTrackerWorkflowHeaderHtml(groupWorkflow) : ''}`; } // Row = title line (number, title, chips, since) + thin strip. Non-active @@ -21098,6 +21183,9 @@

    Where to get help

    // One note line per row: the human line takes the stage note (folded // or dropped), so the event slot below the caption renders nothing. const rowKey = stageTrackerItemKey(item); + // A repository bound to a workflow model draws the model's columns + // (tracker-workflows.js); null keeps the built-in strip byte for byte. + const rowWorkflow = typeof stageTrackerWorkflowOf === 'function' ? stageTrackerWorkflowOf(ctx, item.repo) : null; const humanNote = typeof stageTrackerHumanContextHtml === 'function' ? stageTrackerHumanContextHtml(item, stages, nowMs, subRows, ctx, rowKey) : ''; const noteCtx = humanNote && typeof stageTrackerNoteTakenCtx === 'function' ? stageTrackerNoteTakenCtx(ctx, rowKey) : ctx; // One human-gate pill per gated row (hivecommons/hive#17470): it @@ -21112,7 +21200,7 @@

    Where to get help

    ${typeof stageTrackerPreparationNodeHtml === 'function' ? stageTrackerPreparationNodeHtml(item, stages, ctx) : ''} -
      ${stageTrackerStripItems(shown, stages, range, nowMs, sharedUnder ? Object.assign({}, ctx, { gateElsewhere: true }) : ctx)}
    +
      ${rowWorkflow ? stageTrackerWorkflowStripItems(shown, rowWorkflow, stages) : stageTrackerStripItems(shown, stages, range, nowMs, sharedUnder ? Object.assign({}, ctx, { gateElsewhere: true }) : ctx)}
    ${stageTrackerSinceHtml(shown, nowMs)}${typeof stageTrackerPillHtml === 'function' ? stageTrackerPillHtml(shown, stages, range, nowMs, ctx) : ''}
    ${humanNote}${stageTrackerCurrentStageHtml(shown, stages, range, nowMs)}${typeof stageTrackerPreparationCommentHtml === 'function' ? stageTrackerPreparationCommentHtml(shown, noteCtx, rowKey) : typeof stageTrackerEventHtml === 'function' ? stageTrackerEventHtml(rowKey, noteCtx) : ''}${typeof stageTrackerExecutionHtml === 'function' ? stageTrackerExecutionHtml(shown) : ''}${typeof stageTrackerReviewNotesHtml === 'function' ? stageTrackerReviewNotesHtml(item, ctx) : ''}${typeof stageTrackerNotesPanelHtml === 'function' ? stageTrackerNotesPanelHtml(item, hamNotesCtx(item)) : ''}${subs} `; @@ -23618,6 +23706,8 @@

    All labels

    } stageTrackerSyncToolbar(f); const ctx = { nowMs, stages, moves: _stageTrackerMoves }; + // Workflow-model columns for bound repositories (hivecommons/hive#17620). + ctx.workflows = d.workflows || null; ctx.workers = stageTrackerWorkers(window._lastStatus); ctx.events = _stageTrackerEvents; ctx.noteTuck = stageTrackerNoteTuckSetting(); @@ -64896,7 +64986,7 @@

    Clanker-requested PRs

    `; // Fully forge-qualified so the row is unambiguous: github.ibm.com/org/repo. const displayName = ghHost + '/' + r; - return `
    ${isDefault ? '★' : '☆'}${esc(displayName)}${inheritButton}${rtRow}${cbRow}${pillLabel}
    ` + ldSub; + return `
    ${isDefault ? '★' : '☆'}${esc(displayName)}${inheritButton}${rtRow}${cbRow}${pillLabel}
    ` + ldSub + renderRepoWorkflowRow(r); }).join(''); const hostNote = isGHE ? ` on ${esc(ghHost)} (GitHub Enterprise)` @@ -65940,6 +66030,102 @@

    Agent Watchdog

    — fixed progression; the last step is the cap. `; } + // Workflow-model bindings (hivecommons/hive#17620, RFC phase 2). + // + // Each repo row on Settings → Repos carries a "Workflow" select: hive-default + // (the built-in lifecycle every unbound repo follows) or any compiled model + // from the library. Binding persists immediately through + // PUT /api/workflows/bindings (owner-only, audited) — it is not part of the + // tab's Save button, because a binding is validated server-side against the + // store (the model must exist and compile) and the tracker picks the new + // columns up on its next poll. + const WORKFLOW_DEFAULT_KEY = 'hive-default@1'; + const _workflowState = { loaded: false, loading: false, enabled: false, workflows: [], bindings: [] }; + + function workflowKey(name, version) { return (name || '') + '@' + (version || ''); } + + async function loadWorkflowLibrary(onDone) { + if (_workflowState.loading) return; + _workflowState.loading = true; + try { + const res = await fetch('/api/workflows', { headers: _inceptionAuthHeaders() }); + if (res.status === 503) { _workflowState.loaded = true; _workflowState.workflows = []; return; } + if (!res.ok) throw new Error(await saveErrorMessage(res)); + const data = await res.json(); + _workflowState.workflows = Array.isArray(data.workflows) ? data.workflows : []; + _workflowState.bindings = Array.isArray(data.bindings) ? data.bindings : []; + _workflowState.enabled = !!data.enabled; + _workflowState.loaded = true; + } catch (err) { + _workflowState.loaded = true; + showToast('Could not load the workflow library: ' + err.message, 'error'); + } finally { + _workflowState.loading = false; + if (typeof onDone === 'function') onDone(); + } + } + + function workflowBindingFor(repo) { + const want = String(repo || '').toLowerCase(); + const org = String((_configState.data && _configState.data.org) || '').toLowerCase(); + return _workflowState.bindings.find(b => { + const r = String(b.repo || '').toLowerCase(); + return r === want || (org && (r === org + '/' + want || want === org + '/' + r)); + }) || null; + } + + // renderRepoWorkflowRow returns the per-repo select, or '' when the hive + // has no workflow store (the row simply does not appear; nothing else on + // the tab changes). + function renderRepoWorkflowRow(repo) { + if (!_workflowState.loaded) { + loadWorkflowLibrary(() => { if (_configState.tab === 'Repos') renderConfigTab('Repos'); }); + return ''; + } + if (!_workflowState.workflows.length) return ''; + const bound = workflowBindingFor(repo); + const current = bound ? workflowKey(bound.name, bound.version) : WORKFLOW_DEFAULT_KEY; + const locked = !dashboardRoleAtLeast(window._hiveRole || 'read', 'owner'); + const options = _workflowState.workflows.map(w => { + const key = workflowKey(w.name, w.version); + const compiled = w.compiled !== false; + const label = key + (w.description ? ' — ' + w.description : '') + (compiled ? '' : ' (draft, does not compile)'); + return ``; + }).join(''); + const flag = _workflowState.enabled + ? '' + : 'derivation off'; + return `
    ${flag}${bound ? `bound` : 'default'}
    `; + } + + async function setRepoWorkflow(repo, value) { + if (!dashboardRoleAtLeast(window._hiveRole || 'read', 'owner')) { + showToast('Owner role is required to bind a workflow.', 'error'); + renderConfigTab('Repos'); + return; + } + const key = String(value || ''); + const body = { repo: repo }; + if (key && key !== WORKFLOW_DEFAULT_KEY) { + const at = key.lastIndexOf('@'); + body.name = key.slice(0, at); + body.version = key.slice(at + 1); + } + try { + const res = await fetch('/api/workflows/bindings', { + method: 'PUT', + headers: _inceptionAuthHeaders(), + body: JSON.stringify(body), + }); + if (!res.ok) throw new Error(await saveErrorMessage(res)); + const data = await res.json(); + _workflowState.bindings = Array.isArray(data.bindings) ? data.bindings : []; + showToast(body.name ? `${repo} now follows ${key}` : `${repo} back on hive-default`); + } catch (err) { + showToast('Failed to bind workflow: ' + err.message, 'error'); + } + renderConfigTab('Repos'); + } // ── Watchdog activity strip (#7254) ────────────────────────────────────── // "Review the watchdog-*-observed entries in the Audit Log, then switch to // Heal" is only a data-driven decision if the data is on the page. The diff --git a/src/pkg/stageengine/reconciler.go b/src/pkg/stageengine/reconciler.go index 769dce85f0..9089dea26a 100644 --- a/src/pkg/stageengine/reconciler.go +++ b/src/pkg/stageengine/reconciler.go @@ -47,7 +47,13 @@ type Options struct { // Workflow, when set, derives through the compiled workflow model // (DeriveFromModel) and overrides Issue and PR. hivecommons/hive#17620. Workflow *workflowmodel.Profile - Now func() time.Time + // WorkflowFor, when set, resolves the compiled model bound to a + // repository (owner/repo) at derivation time; nil means the repository + // is unbound and the Issue/PR derivers run unchanged. It composes with + // Workflow: Workflow is the engine-wide model, WorkflowFor the + // per-repository override. hivecommons/hive#17620 (RFC phase 2). + WorkflowFor func(owner, repo string) *workflowmodel.Profile + Now func() time.Time Logger *slog.Logger Metrics Metrics } @@ -87,6 +93,10 @@ func New(o Options) Reconciler { if o.PR == nil { o.PR = PRDeriver() } + if o.WorkflowFor != nil { + o.Issue = DeriveForRepo(o.WorkflowFor, o.Issue) + o.PR = DeriveForRepo(o.WorkflowFor, o.PR) + } if o.Now == nil { o.Now = time.Now } diff --git a/src/pkg/stageengine/workflow.go b/src/pkg/stageengine/workflow.go index 55f337aff2..c01674898d 100644 --- a/src/pkg/stageengine/workflow.go +++ b/src/pkg/stageengine/workflow.go @@ -3,12 +3,40 @@ package stageengine import ( "fmt" "strings" + "sync" "github.com/hivecommons/hive/pkg/authorkind" "github.com/hivecommons/hive/pkg/stagelabels" "github.com/hivecommons/hive/pkg/workflowmodel" ) +// DeriveForRepo returns a Deriver of fallback's kind that derives through +// the model lookup returns for the item's repository, or through fallback +// when lookup returns nil. Profiles are compiled per lookup result and +// cached by hash, so a rebinding takes effect on the next derivation. +func DeriveForRepo(lookup func(owner, repo string) *workflowmodel.Profile, fallback Deriver) Deriver { + if lookup == nil || fallback == nil { + return fallback + } + kind := fallback.Kind() + var mu sync.Mutex + cache := map[string]Deriver{} + return DeriverFunc(kind, func(f Facts) Decision { + p := lookup(f.Ref.Owner, f.Ref.Repo) + if p == nil { + return fallback.Derive(f) + } + mu.Lock() + d, ok := cache[p.Hash] + if !ok { + d = DeriveFromModel(p, kind) + cache[p.Hash] = d + } + mu.Unlock() + return d.Derive(f) + }) +} + // DeriveFromModel is the third Deriver (hivecommons/hive#17620 "One // engine, one writer"): it derives an item's stage under a compiled // workflow model and produces the same Decision the built-in derivers do, diff --git a/src/pkg/stageengine/workflow_parity_test.go b/src/pkg/stageengine/workflow_parity_test.go index a33cfc3825..665e6f16c0 100644 --- a/src/pkg/stageengine/workflow_parity_test.go +++ b/src/pkg/stageengine/workflow_parity_test.go @@ -104,3 +104,44 @@ func TestDeriveFromModelCoverage(t *testing.T) { t.Fatalf("missing state %s: got %+v", drop, got) } } + +// DeriveForRepo routes bound repositories through their model and leaves +// unbound repositories on the built-in deriver, decision for decision. +func TestDeriveForRepo(t *testing.T) { + p := hivedefault.Profile() + calls := 0 + lookup := func(owner, repo string) *workflowmodel.Profile { + calls++ + if repo == "bound" { + return p + } + return nil + } + if stageengine.DeriveForRepo(nil, stageengine.PRDeriver()) == nil || stageengine.DeriveForRepo(lookup, nil) != nil { + t.Fatal("nil handling") + } + derivers := map[stagelabels.Item][2]stageengine.Deriver{ + stagelabels.IssueItem: {stageengine.IssueDeriver(), stageengine.DeriveForRepo(lookup, stageengine.IssueDeriver())}, + stagelabels.PRItem: {stageengine.PRDeriver(), stageengine.DeriveForRepo(lookup, stageengine.PRDeriver())}, + } + if derivers[stagelabels.PRItem][1].Kind() != stagelabels.PRItem { + t.Fatal("kind") + } + for i, f := range stageengine.ParityFacts() { + for _, repo := range []string{"bound", "free"} { + f.Ref.Repo = repo + d := derivers[f.Ref.Kind] + if want, got := d[0].Derive(f), d[1].Derive(f); !reflect.DeepEqual(want, got) { + t.Errorf("facts[%d] %s/%s: built-in %+v, routed %+v", i, repo, f.Ref.Key(), want, got) + } + } + } + if calls == 0 { + t.Fatal("lookup never consulted") + } + // the engine composes it when Options.WorkflowFor is set + r := stageengine.New(stageengine.Options{WorkflowFor: lookup, Store: stagelabels.NewStore(t.TempDir()+"/stages.json", nil)}) + if r == nil { + t.Fatal("engine") + } +} diff --git a/src/pkg/workflowmodel/lifecycle.go b/src/pkg/workflowmodel/lifecycle.go index d98e937376..8fd8949bae 100644 --- a/src/pkg/workflowmodel/lifecycle.go +++ b/src/pkg/workflowmodel/lifecycle.go @@ -39,6 +39,9 @@ const ( ProvenanceBuiltIn Provenance = "built-in" ProvenanceCurated Provenance = "curated" ProvenanceIngestedUnreviewed Provenance = "ingested-unreviewed" + // ProvenanceLocal is a model authored or edited on this hive and not + // (yet) published anywhere. + ProvenanceLocal Provenance = "local" ) // Confidence marks an element a human has not confirmed yet. diff --git a/src/pkg/workflowmodel/store/store.go b/src/pkg/workflowmodel/store/store.go new file mode 100644 index 0000000000..b81d46dba7 --- /dev/null +++ b/src/pkg/workflowmodel/store/store.go @@ -0,0 +1,415 @@ +// Package store persists workflow models on the hive's data volume +// (hivecommons/hive#17620, phase 2). +// +// Layout under Root: +// +// workflows//.json the model, as saved +// workflows/index.json catalogue: one entry per saved version +// +// hive-default is virtual: it is generated, listed read-only, and never +// written. Every save recompiles; a model that does not compile is still +// saved (so an editor can keep a draft) but its entry carries the +// diagnostics and Compiled=false, and Profile refuses it. +package store + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "regexp" + "sort" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/hivedefault" +) + +// Layout constants. +const ( + // DefaultRoot is the spoke data volume. + DefaultRoot = "/data" + // EnvRoot overrides the root (test seam and operator relocation). + EnvRoot = "HIVE_WORKFLOWS_DATA_DIR" + // Subdir is the directory under the root. + Subdir = "workflows" + // IndexFile is the catalogue inside Subdir. + IndexFile = "index.json" +) + +// Errors. +var ( + ErrNotFound = errors.New("workflow model not found") + ErrReadOnly = errors.New("workflow model is read-only") + ErrBadName = errors.New("workflow model name must be lower-case letters, digits and hyphens") + ErrBadVersion = errors.New("workflow model version must be letters, digits, dots and hyphens") + ErrNotCompiled = errors.New("workflow model does not compile") +) + +var ( + nameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,63}$`) + versionRe = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9.-]{0,31}$`) +) + +// Entry is one catalogue line. +type Entry struct { + Name string `json:"name"` + Version string `json:"version"` + Hash string `json:"hash"` + Description string `json:"description,omitempty"` + Author string `json:"author,omitempty"` + Source workflowmodel.MetaSource `json:"source,omitempty"` + Provenance workflowmodel.Provenance `json:"provenance,omitempty"` + Compiled bool `json:"compiled"` + Diagnostics []workflowmodel.Diagnostic `json:"diagnostics,omitempty"` + States int `json:"states"` + Columns int `json:"columns"` + SavedAt time.Time `json:"savedAt,omitzero"` + SavedBy string `json:"savedBy,omitempty"` + ReadOnly bool `json:"readOnly,omitempty"` + // Reserved for the marketplace and the benchmark / rating work (P7+): + // never written by the spoke store, carried verbatim across saves. + Adoption json.RawMessage `json:"adoption,omitempty"` + Ratings json.RawMessage `json:"ratings,omitempty"` + Benchmarks json.RawMessage `json:"benchmarks,omitempty"` +} + +// Key is name@version. +func (e Entry) Key() string { return e.Name + "@" + e.Version } + +// Store is the on-disk model library. +type Store struct { + root string + mu sync.Mutex +} + +// Root resolves the data root: EnvRoot, else configured, else DefaultRoot. +func Root(configured string) string { + if v := strings.TrimSpace(os.Getenv(EnvRoot)); v != "" { + return v + } + if strings.TrimSpace(configured) != "" { + return configured + } + return DefaultRoot +} + +// Open returns a Store rooted at root (the data dir, not the workflows +// subdirectory). Nothing is created until the first save. +func Open(root string) *Store { return &Store{root: root} } + +func (s *Store) dir() string { return filepath.Join(s.root, Subdir) } + +func (s *Store) modelPath(name, version string) string { + return filepath.Join(s.dir(), name, version+".json") +} + +func checkKey(name, version string) error { + if !nameRe.MatchString(name) { + return fmt.Errorf("%w: %q", ErrBadName, name) + } + if !versionRe.MatchString(version) { + return fmt.Errorf("%w: %q", ErrBadVersion, version) + } + return nil +} + +// IsBuiltin reports whether name is the generated, read-only model. +func IsBuiltin(name string) bool { return name == hivedefault.Name } + +func builtinEntry() Entry { + m := hivedefault.Generate() + p := hivedefault.Profile() + return Entry{ + Name: p.Name, Version: p.Version, Hash: p.Hash, + Description: m.Spec.Meta.Description, Author: m.Spec.Meta.Author, + Source: m.Spec.Meta.Source, Provenance: workflowmodel.ProvenanceBuiltIn, + Compiled: true, States: len(p.States), Columns: len(p.Columns), ReadOnly: true, + } +} + +func entryFor(m *workflowmodel.Model, by string, now time.Time) Entry { + e := Entry{ + Name: m.Metadata.Name, Version: m.Spec.Meta.Version, Hash: m.Hash(), + Description: m.Spec.Meta.Description, Author: m.Spec.Meta.Author, + Source: m.Spec.Meta.Source, Provenance: m.Spec.Meta.Provenance, + States: len(m.Spec.States), Columns: len(m.Spec.Columns), + SavedBy: by, + } + if !now.IsZero() { + e.SavedAt = now.UTC().Truncate(time.Second) + } + if e.Provenance == "" { + e.Provenance = workflowmodel.ProvenanceLocal + } + p, diags, err := workflowmodel.Compile(m) + e.Diagnostics = diags + e.Compiled = err == nil && p != nil + return e +} + +// List returns the catalogue: hive-default first, then saved entries by +// name then version. +func (s *Store) List() ([]Entry, error) { + s.mu.Lock() + defer s.mu.Unlock() + idx, err := s.readIndex() + if err != nil { + return nil, err + } + return append([]Entry{builtinEntry()}, idx...), nil +} + +// Get returns a saved model (or the generated hive-default) with its entry. +func (s *Store) Get(name, version string) (*workflowmodel.Model, Entry, error) { + if IsBuiltin(name) { + if version != "" && version != hivedefault.Version { + return nil, Entry{}, fmt.Errorf("%w: %s@%s", ErrNotFound, name, version) + } + return hivedefault.Generate(), builtinEntry(), nil + } + if err := checkKey(name, version); err != nil { + return nil, Entry{}, err + } + s.mu.Lock() + defer s.mu.Unlock() + b, err := os.ReadFile(s.modelPath(name, version)) + if errors.Is(err, os.ErrNotExist) { + return nil, Entry{}, fmt.Errorf("%w: %s@%s", ErrNotFound, name, version) + } + if err != nil { + return nil, Entry{}, err + } + var m workflowmodel.Model + if err := json.Unmarshal(b, &m); err != nil { + return nil, Entry{}, fmt.Errorf("%s@%s: %w", name, version, err) + } + idx, err := s.readIndex() + if err != nil { + return nil, Entry{}, err + } + for _, e := range idx { + if e.Name == name && e.Version == version { + return &m, e, nil + } + } + return &m, entryFor(&m, "", time.Time{}), nil +} + +// Profile returns the compiled profile for name@version; a saved draft +// that does not compile is ErrNotCompiled. +func (s *Store) Profile(name, version string) (*workflowmodel.Profile, error) { + if IsBuiltin(name) && (version == "" || version == hivedefault.Version) { + return hivedefault.Profile(), nil + } + m, _, err := s.Get(name, version) + if err != nil { + return nil, err + } + p, diags, err := workflowmodel.Compile(m) + if err != nil { + return nil, fmt.Errorf("%w: %s@%s: %s", ErrNotCompiled, name, version, summarize(diags)) + } + return p, nil +} + +func summarize(diags []workflowmodel.Diagnostic) string { + var parts []string + for _, d := range diags { + if d.Severity == workflowmodel.DiagError { + parts = append(parts, d.Code) + } + } + return strings.Join(parts, ", ") +} + +// Save writes m as metadata.name@spec.meta.version and refreshes the +// index. The model must validate (shape); a compile failure is recorded +// on the entry, not refused: drafts live in the library, never in a +// binding. +func (s *Store) Save(m *workflowmodel.Model, by string, now time.Time) (Entry, error) { + if m == nil { + return Entry{}, errors.New("nil model") + } + name, version := m.Metadata.Name, m.Spec.Meta.Version + if IsBuiltin(name) { + return Entry{}, fmt.Errorf("%w: %s", ErrReadOnly, name) + } + if err := checkKey(name, version); err != nil { + return Entry{}, err + } + if err := m.Validate(); err != nil { + return Entry{}, err + } + b, err := json.MarshalIndent(m, "", " ") + if err != nil { + return Entry{}, err + } + s.mu.Lock() + defer s.mu.Unlock() + if err := os.MkdirAll(filepath.Dir(s.modelPath(name, version)), 0o755); err != nil { + return Entry{}, err + } + if err := writeAtomic(s.modelPath(name, version), b); err != nil { + return Entry{}, err + } + idx, err := s.readIndex() + if err != nil { + return Entry{}, err + } + e := entryFor(m, by, now) + replaced := false + for i := range idx { + if idx[i].Name == name && idx[i].Version == version { + e.Adoption, e.Ratings, e.Benchmarks = idx[i].Adoption, idx[i].Ratings, idx[i].Benchmarks + idx[i] = e + replaced = true + } + } + if !replaced { + idx = append(idx, e) + } + return e, s.writeIndex(idx) +} + +// Delete removes one version. Whether a repository is bound to it is the +// caller's check: the config owns bindings. +func (s *Store) Delete(name, version string) error { + if IsBuiltin(name) { + return fmt.Errorf("%w: %s", ErrReadOnly, name) + } + if err := checkKey(name, version); err != nil { + return err + } + s.mu.Lock() + defer s.mu.Unlock() + if err := os.Remove(s.modelPath(name, version)); err != nil { + if errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("%w: %s@%s", ErrNotFound, name, version) + } + return err + } + _ = os.Remove(filepath.Join(s.dir(), name)) // succeeds only when empty + idx, err := s.readIndex() + if err != nil { + return err + } + out := idx[:0] + for _, e := range idx { + if e.Name != name || e.Version != version { + out = append(out, e) + } + } + return s.writeIndex(out) +} + +// Versions lists the entries of name, newest save first. +func (s *Store) Versions(name string) ([]Entry, error) { + all, err := s.List() + if err != nil { + return nil, err + } + var out []Entry + for _, e := range all { + if e.Name == name { + out = append(out, e) + } + } + if len(out) == 0 { + return nil, fmt.Errorf("%w: %s", ErrNotFound, name) + } + sort.SliceStable(out, func(i, j int) bool { return out[i].SavedAt.After(out[j].SavedAt) }) + return out, nil +} + +// Rebuild regenerates the index from the model files on disk (recovery +// after a partial write or a hand-copied directory). +func (s *Store) Rebuild(now time.Time) ([]Entry, error) { + s.mu.Lock() + defer s.mu.Unlock() + names, err := os.ReadDir(s.dir()) + if errors.Is(err, os.ErrNotExist) { + return nil, s.writeIndex(nil) + } + if err != nil { + return nil, err + } + var idx []Entry + for _, d := range names { + if !d.IsDir() { + continue + } + files, err := os.ReadDir(filepath.Join(s.dir(), d.Name())) + if err != nil { + return nil, err + } + for _, f := range files { + if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") { + continue + } + b, err := os.ReadFile(filepath.Join(s.dir(), d.Name(), f.Name())) + if err != nil { + return nil, err + } + var m workflowmodel.Model + if err := json.Unmarshal(b, &m); err != nil { + continue // not a model; leave it alone + } + if m.Metadata.Name != d.Name() || m.Spec.Meta.Version+".json" != f.Name() { + continue // misfiled + } + idx = append(idx, entryFor(&m, "", now)) + } + } + return idx, s.writeIndex(idx) +} + +func sortIndex(idx []Entry) { + sort.SliceStable(idx, func(i, j int) bool { + if idx[i].Name != idx[j].Name { + return idx[i].Name < idx[j].Name + } + return idx[i].Version < idx[j].Version + }) +} + +func (s *Store) readIndex() ([]Entry, error) { + b, err := os.ReadFile(filepath.Join(s.dir(), IndexFile)) + if errors.Is(err, os.ErrNotExist) { + return nil, nil + } + if err != nil { + return nil, err + } + var idx []Entry + if err := json.Unmarshal(b, &idx); err != nil { + return nil, fmt.Errorf("%s: %w", IndexFile, err) + } + return idx, nil +} + +func (s *Store) writeIndex(idx []Entry) error { + sortIndex(idx) + if idx == nil { + idx = []Entry{} + } + b, err := json.MarshalIndent(idx, "", " ") + if err != nil { + return err + } + if err := os.MkdirAll(s.dir(), 0o755); err != nil { + return err + } + return writeAtomic(filepath.Join(s.dir(), IndexFile), b) +} + +func writeAtomic(path string, b []byte) error { + tmp := path + ".tmp" + if err := os.WriteFile(tmp, b, 0o644); err != nil { + return err + } + return os.Rename(tmp, path) +} diff --git a/src/pkg/workflowmodel/store/store_test.go b/src/pkg/workflowmodel/store/store_test.go new file mode 100644 index 0000000000..01d178341d --- /dev/null +++ b/src/pkg/workflowmodel/store/store_test.go @@ -0,0 +1,259 @@ +package store + +import ( + "encoding/json" + "errors" + "os" + "path/filepath" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/workflowmodel" + "github.com/hivecommons/hive/pkg/workflowmodel/hivedefault" +) + +func techdocs(t *testing.T) *workflowmodel.Model { + t.Helper() + b, err := os.ReadFile("../testdata/models/techdocs.json") + if err != nil { + t.Fatal(err) + } + var m workflowmodel.Model + if err := json.Unmarshal(b, &m); err != nil { + t.Fatal(err) + } + return &m +} + +var t0 = time.Date(2026, 11, 10, 12, 0, 0, 0, time.UTC) + +func TestEmptyStoreListsBuiltin(t *testing.T) { + s := Open(t.TempDir()) + got, err := s.List() + if err != nil || len(got) != 1 || got[0].Name != hivedefault.Name || !got[0].ReadOnly || !got[0].Compiled || got[0].Provenance != workflowmodel.ProvenanceBuiltIn { + t.Fatalf("%v %+v", err, got) + } + m, e, err := s.Get(hivedefault.Name, "") + if err != nil || m == nil || e.Hash != hivedefault.Profile().Hash { + t.Fatalf("%v %+v", err, e) + } + if _, _, err := s.Get(hivedefault.Name, "9"); !errors.Is(err, ErrNotFound) { + t.Fatal(err) + } + p, err := s.Profile(hivedefault.Name, hivedefault.Version) + if err != nil || p.Name != hivedefault.Name { + t.Fatal(err) + } + if _, err := s.Save(hivedefault.Generate(), "andy", t0); !errors.Is(err, ErrReadOnly) { + t.Fatal(err) + } + if err := s.Delete(hivedefault.Name, "1"); !errors.Is(err, ErrReadOnly) { + t.Fatal(err) + } +} + +func TestSaveGetListDelete(t *testing.T) { + root := t.TempDir() + s := Open(root) + m := techdocs(t) + e, err := s.Save(m, "andy", t0) + if err != nil { + t.Fatal(err) + } + if !e.Compiled || e.Provenance != workflowmodel.ProvenanceLocal || e.SavedBy != "andy" || !e.SavedAt.Equal(t0) || e.States != 9 || e.Columns != 5 || e.Key() != "cncf-techdocs@1" { + t.Fatalf("%+v", e) + } + if _, err := os.Stat(filepath.Join(root, Subdir, "cncf-techdocs", "1.json")); err != nil { + t.Fatal(err) + } + list, err := s.List() + if err != nil || len(list) != 2 || list[1].Key() != "cncf-techdocs@1" { + t.Fatalf("%v %+v", err, list) + } + got, ge, err := s.Get("cncf-techdocs", "1") + if err != nil || got.Hash() != m.Hash() || ge.SavedBy != "andy" { + t.Fatalf("%v %+v", err, ge) + } + if p, err := s.Profile("cncf-techdocs", "1"); err != nil || p.Mode != workflowmodel.DerivationRules { + t.Fatal(err) + } + + // second version, then Versions is newest first + m2 := techdocs(t) + m2.Spec.Meta.Version = "2" + if _, err := s.Save(m2, "nate", t0.Add(time.Hour)); err != nil { + t.Fatal(err) + } + vs, err := s.Versions("cncf-techdocs") + if err != nil || len(vs) != 2 || vs[0].Version != "2" { + t.Fatalf("%v %+v", err, vs) + } + if _, err := s.Versions("nobody"); !errors.Is(err, ErrNotFound) { + t.Fatal(err) + } + + if err := s.Delete("cncf-techdocs", "1"); err != nil { + t.Fatal(err) + } + if err := s.Delete("cncf-techdocs", "1"); !errors.Is(err, ErrNotFound) { + t.Fatal(err) + } + if _, _, err := s.Get("cncf-techdocs", "1"); !errors.Is(err, ErrNotFound) { + t.Fatal(err) + } + list, _ = s.List() + if len(list) != 2 || list[1].Version != "2" { + t.Fatalf("%+v", list) + } + if err := s.Delete("cncf-techdocs", "2"); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(root, Subdir, "cncf-techdocs")); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("empty model dir should be removed: %v", err) + } +} + +func TestDraftsSaveButDoNotProfile(t *testing.T) { + s := Open(t.TempDir()) + m := techdocs(t) + m.Spec.Gates[0].ReleasedBy = "" + e, err := s.Save(m, "", t0) + if err != nil { + t.Fatal(err) + } + if e.Compiled || len(e.Diagnostics) == 0 { + t.Fatalf("%+v", e) + } + if _, err := s.Profile("cncf-techdocs", "1"); !errors.Is(err, ErrNotCompiled) { + t.Fatal(err) + } + // an invalid shape is refused outright + m.Spec.Initial = "ghost" + if _, err := s.Save(m, "", t0); err == nil { + t.Fatal("invalid model saved") + } +} + +func TestKeysAreChecked(t *testing.T) { + s := Open(t.TempDir()) + m := techdocs(t) + m.Metadata.Name = "../etc" + if _, err := s.Save(m, "", t0); !errors.Is(err, ErrBadName) { + t.Fatal(err) + } + m = techdocs(t) + m.Spec.Meta.Version = "1/../x" + if _, err := s.Save(m, "", t0); !errors.Is(err, ErrBadVersion) { + t.Fatal(err) + } + if _, _, err := s.Get("Bad Name", "1"); !errors.Is(err, ErrBadName) { + t.Fatal(err) + } + if err := s.Delete("ok", "bad version"); !errors.Is(err, ErrBadVersion) { + t.Fatal(err) + } + if _, err := s.Save(nil, "", t0); err == nil { + t.Fatal("nil saved") + } +} + +func TestResaveKeepsReservedFields(t *testing.T) { + root := t.TempDir() + s := Open(root) + m := techdocs(t) + if _, err := s.Save(m, "", t0); err != nil { + t.Fatal(err) + } + idx, _ := s.readIndex() + idx[0].Ratings = json.RawMessage(`{"stars":5}`) + if err := s.writeIndex(idx); err != nil { + t.Fatal(err) + } + m.Spec.Meta.Description = "edited" + e, err := s.Save(m, "", t0.Add(time.Minute)) + if err != nil || e.Description != "edited" { + t.Fatalf("%v %+v", err, e) + } + var r struct{ Stars int } + if err := json.Unmarshal(e.Ratings, &r); err != nil || r.Stars != 5 { + t.Fatalf("ratings %s: %v", e.Ratings, err) + } +} + +func TestRebuild(t *testing.T) { + root := t.TempDir() + s := Open(root) + if _, err := s.Save(techdocs(t), "", t0); err != nil { + t.Fatal(err) + } + // corrupt the index, drop a stray file and a misfiled model + if err := os.WriteFile(filepath.Join(root, Subdir, IndexFile), []byte("nope"), 0o644); err != nil { + t.Fatal(err) + } + if _, err := s.List(); err == nil { + t.Fatal("corrupt index read") + } + _ = os.WriteFile(filepath.Join(root, Subdir, "cncf-techdocs", "notes.txt"), []byte("x"), 0o644) + _ = os.WriteFile(filepath.Join(root, Subdir, "cncf-techdocs", "7.json"), []byte(`{"metadata":{"name":"other"}}`), 0o644) + _ = os.WriteFile(filepath.Join(root, Subdir, "cncf-techdocs", "8.json"), []byte(`not json`), 0o644) + idx, err := s.Rebuild(t0) + if err != nil || len(idx) != 1 || idx[0].Key() != "cncf-techdocs@1" { + t.Fatalf("%v %+v", err, idx) + } + // an index entry missing for a model on disk still resolves on Get + if err := os.WriteFile(filepath.Join(root, Subdir, IndexFile), []byte("[]"), 0o644); err != nil { + t.Fatal(err) + } + _, e, err := s.Get("cncf-techdocs", "1") + if err != nil || !e.Compiled || e.SavedBy != "" { + t.Fatalf("%v %+v", err, e) + } + + empty := Open(t.TempDir()) + if idx, err := empty.Rebuild(t0); err != nil || len(idx) != 0 { + t.Fatalf("%v %+v", err, idx) + } +} + +func TestUnreadableModelAndIndex(t *testing.T) { + root := t.TempDir() + s := Open(root) + if _, err := s.Save(techdocs(t), "", t0); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(s.modelPath("cncf-techdocs", "1"), []byte("{"), 0o644); err != nil { + t.Fatal(err) + } + if _, _, err := s.Get("cncf-techdocs", "1"); err == nil { + t.Fatal("truncated model read") + } + if _, err := s.Profile("cncf-techdocs", "1"); err == nil { + t.Fatal("truncated model profiled") + } + if err := os.WriteFile(filepath.Join(root, Subdir, IndexFile), []byte("["), 0o644); err != nil { + t.Fatal(err) + } + if _, err := s.Versions("cncf-techdocs"); err == nil { + t.Fatal("corrupt index listed") + } + if _, err := s.Save(techdocs(t), "", t0); err == nil { + t.Fatal("saved over a corrupt index") + } + if err := s.Delete("cncf-techdocs", "1"); err == nil { + t.Fatal("deleted over a corrupt index") + } + if vs, err := Open(t.TempDir()).Versions(hivedefault.Name); err != nil || len(vs) != 1 { + t.Fatalf("%v %+v", err, vs) + } +} + +func TestRoot(t *testing.T) { + t.Setenv(EnvRoot, "") + if Root("") != DefaultRoot || Root("/x") != "/x" { + t.Fatal("root resolution") + } + t.Setenv(EnvRoot, "/env") + if Root("/x") != "/env" { + t.Fatal("env must win") + } +}