feat(webui): inline editing for per-repo allow_concurrent/max_retries/review_enabled
Adds PATCH /api/config/repos/{owner}/{name}, a dedicated seam distinct
from the generic dotted-key PATCH /api/config path: a repository is
one element of []config.Repo, not a flat key. Traced the mechanism
before adding any new plumbing -- overlay.Nest/ApplyOverlayValues
already handle "repos" as a single whole-slice key with no dot, so
the fix is a webui handler plus a bootstrap seam that reads the
daemon's live, untrimmed Repos (never webui.RepoView, which omits
Preflight/TestGlob), mutates the one matched field via the pure
applyRepoFieldUpdate helper, and republishes through the existing
UpdateConfig(ctx, {"repos": repos}) path unchanged.
config-row.js's row() gains an opts.save override so an edit can be
redirected to a different persistence call. The repositories table
renders an autosaving checkbox for the two booleans and a
save-on-blur/Enter input for max_retries, via api.configRepoUpdate.
Closes archie-core-b6ew.6, and with it archie-core-b6ew.
This commit is contained in:
1 parent
a444253209
commit
a4fbd8d6ef
12 files changed
+427
-20
No files matched your search
@@ -1281,6 +1281,31 @@ func (b *boot) installUpdateConfigHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
// installUpdateRepoFieldHandler installs the dashboard's PATCH
|
||||
// /api/config/repos/{owner}/{name} handler (archie-core-b6ew.6). A
|
||||
// repository is one element of a []config.Repo, not a flat dotted key, so
|
||||
// this cannot go through UpdateConfig's dotted-key path directly (overlay's
|
||||
// Nest/ApplyOverlayValues have no notion of "element N of this slice").
|
||||
// Instead it reads the daemon's own live, untrimmed Repos (never
|
||||
// webui.RepoView, which omits fields like Preflight/TestGlob and would
|
||||
// silently drop them on a naive round-trip), changes the one field on the
|
||||
// matching repo, and republishes the *entire* repository list through
|
||||
// UpdateConfig -- reusing its validate-persist-publish path unchanged, so
|
||||
// this handler owns only "which repo, which field," never how an update is
|
||||
// applied. An edit here therefore overrides the whole repos list in the
|
||||
// runtime overlay, same granularity every other overlay key already has;
|
||||
// [[repos]] file edits stay visible for every field this handler never
|
||||
// touches until an operator resets the "repos" override.
|
||||
func (b *boot) installUpdateRepoFieldHandler() {
|
||||
b.web.UpdateRepoField = func(ctx context.Context, owner, name, field string, value any) error {
|
||||
repos, err := applyRepoFieldUpdate(b.d.Cfg.Get().Clone().Repos, owner, name, field, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return b.web.UpdateConfig(ctx, map[string]any{"repos": repos})
|
||||
}
|
||||
}
|
||||
|
||||
// installConfigHandlers installs the dashboard's config override
|
||||
// listing and per-row reset handlers, both going through the same
|
||||
// publish path as reload.
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// applyDottedOverlay decodes dotted-path updates into cfg. yaml cannot
|
||||
@@ -22,3 +26,43 @@ func applyDottedOverlay(cfg *config.Config, updates map[string]any) error {
|
||||
}
|
||||
return configuration.ApplyOverlayValues(cfg, nested)
|
||||
}
|
||||
|
||||
// applyRepoFieldUpdate returns a copy of repos with one editable field
|
||||
// changed on the repository matched by owner/name (archie-core-b6ew.6).
|
||||
// repos must already be a caller-owned copy (config.Config.Clone's
|
||||
// Repos) -- this mutates the matched element in place and returns the
|
||||
// same slice, matching applyDottedOverlay's "caller owns the copy"
|
||||
// contract. Kept as a pure function, separate from
|
||||
// installUpdateRepoFieldHandler's closure, so the field-matching and
|
||||
// type-coercion logic is directly unit-testable without constructing a
|
||||
// full boot.
|
||||
func applyRepoFieldUpdate(repos []config.Repo, owner, name, field string, value any) ([]config.Repo, error) {
|
||||
if !webui.RepoEditableFields[field] {
|
||||
return nil, fmt.Errorf("%w: repo field %s is not editable from the dashboard", webui.ErrConfigUpdateInvalid, field)
|
||||
}
|
||||
index := slices.IndexFunc(repos, func(r config.Repo) bool { return r.Owner == owner && r.Name == name })
|
||||
if index < 0 {
|
||||
return nil, fmt.Errorf("%w: repository %s/%s is not configured", webui.ErrConfigUpdateInvalid, owner, name)
|
||||
}
|
||||
switch field {
|
||||
case "allow_concurrent":
|
||||
v, ok := value.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: allow_concurrent must be a boolean", webui.ErrConfigUpdateInvalid)
|
||||
}
|
||||
repos[index].AllowConcurrent = v
|
||||
case "review_enabled":
|
||||
v, ok := value.(bool)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: review_enabled must be a boolean", webui.ErrConfigUpdateInvalid)
|
||||
}
|
||||
repos[index].ReviewEnabled = v
|
||||
case "max_retries":
|
||||
v, ok := value.(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%w: max_retries must be a number", webui.ErrConfigUpdateInvalid)
|
||||
}
|
||||
repos[index].MaxRetries = int(v)
|
||||
}
|
||||
return repos, nil
|
||||
}
|
||||
@@ -28,3 +28,83 @@ func TestApplyDottedOverlayRejectsEmptyKey(t *testing.T) {
|
||||
t.Fatal("empty dotted key accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func twoRepoFixture() []config.Repo {
|
||||
return []config.Repo{
|
||||
{Owner: "acme", Name: "widget", Base: "main", Preflight: [][]string{{"go", "version"}}},
|
||||
{Owner: "acme", Name: "gadget", Base: "main"},
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyRepoFieldUpdateChangesOnlyTheMatchedRepo proves the update
|
||||
// targets exactly one element by owner/name and leaves every other repo,
|
||||
// and every other field on the matched repo, untouched -- including
|
||||
// Preflight, which webui.RepoView never carries on the wire (the whole
|
||||
// point of reading b.d.Cfg's live Repos instead of the trimmed view).
|
||||
func TestApplyRepoFieldUpdateChangesOnlyTheMatchedRepo(t *testing.T) {
|
||||
repos, err := applyRepoFieldUpdate(twoRepoFixture(), "acme", "widget", "allow_concurrent", true)
|
||||
if err != nil {
|
||||
t.Fatalf("applyRepoFieldUpdate: %v", err)
|
||||
}
|
||||
if !repos[0].AllowConcurrent {
|
||||
t.Error("widget.AllowConcurrent = false, want true")
|
||||
}
|
||||
if len(repos[0].Preflight) != 1 || repos[0].Preflight[0][0] != "go" {
|
||||
t.Errorf("widget.Preflight = %v, want it untouched", repos[0].Preflight)
|
||||
}
|
||||
if repos[1].AllowConcurrent {
|
||||
t.Error("gadget.AllowConcurrent changed, want the non-matched repo left alone")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRepoFieldUpdateSetsEachEditableField(t *testing.T) {
|
||||
repos, err := applyRepoFieldUpdate(twoRepoFixture(), "acme", "widget", "max_retries", float64(7))
|
||||
if err != nil {
|
||||
t.Fatalf("applyRepoFieldUpdate: %v", err)
|
||||
}
|
||||
if repos[0].MaxRetries != 7 {
|
||||
t.Errorf("MaxRetries = %d, want 7 (JSON numbers decode as float64)", repos[0].MaxRetries)
|
||||
}
|
||||
|
||||
repos, err = applyRepoFieldUpdate(twoRepoFixture(), "acme", "widget", "review_enabled", true)
|
||||
if err != nil {
|
||||
t.Fatalf("applyRepoFieldUpdate: %v", err)
|
||||
}
|
||||
if !repos[0].ReviewEnabled {
|
||||
t.Error("ReviewEnabled = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRepoFieldUpdateRejectsUnknownRepo(t *testing.T) {
|
||||
_, err := applyRepoFieldUpdate(twoRepoFixture(), "acme", "does-not-exist", "allow_concurrent", true)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for a repo that is not configured")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyRepoFieldUpdateRejectsUneditableField(t *testing.T) {
|
||||
_, err := applyRepoFieldUpdate(twoRepoFixture(), "acme", "widget", "gate", []any{})
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for a field outside RepoEditableFields")
|
||||
}
|
||||
}
|
||||
|
||||
// TestApplyRepoFieldUpdateRejectsWrongValueType proves a bool field given a
|
||||
// number (or vice versa) fails with a clear error instead of a silent
|
||||
// zero-value write -- the JSON body is caller-controlled (the dashboard),
|
||||
// not type-checked before it reaches this function.
|
||||
func TestApplyRepoFieldUpdateRejectsWrongValueType(t *testing.T) {
|
||||
cases := []struct {
|
||||
field string
|
||||
value any
|
||||
}{
|
||||
{"allow_concurrent", "yes"},
|
||||
{"review_enabled", 1.0},
|
||||
{"max_retries", true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if _, err := applyRepoFieldUpdate(twoRepoFixture(), "acme", "widget", tc.field, tc.value); err == nil {
|
||||
t.Errorf("field %q accepted wrong-typed value %#v", tc.field, tc.value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -382,6 +382,7 @@ func Run() int {
|
||||
b.buildDaemon()
|
||||
b.wireConfigPublishing(ctx, *cfgPath, *overlayPath)
|
||||
b.installUpdateConfigHandler()
|
||||
b.installUpdateRepoFieldHandler()
|
||||
b.installConfigHandlers(*cfgPath, *overlayPath)
|
||||
|
||||
if err := b.startServices(ctx); err != nil {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// RepoEditableFields are the per-repository fields the dashboard may change
|
||||
// through handleConfigRepoUpdate. Deliberately narrow: these three are
|
||||
// booleans/small ints with no cross-field validation, unlike Gate or
|
||||
// Protect, which stay file-edited (archie-core-b6ew.4/.6).
|
||||
var RepoEditableFields = map[string]bool{
|
||||
"allow_concurrent": true,
|
||||
"max_retries": true,
|
||||
"review_enabled": true,
|
||||
}
|
||||
|
||||
// handleConfigRepoUpdate applies one field change to one repository,
|
||||
// identified by owner/name in the path. It exists because the repository
|
||||
// list is a []config.Repo, not a flat dotted key -- PATCH /api/config's
|
||||
// generic path (overlay.Nest + configuration.ApplyOverlayValues) has no way
|
||||
// to address "element N of this slice." UpdateRepoField's implementation
|
||||
// (wired by the composition root) reads the daemon's own live config.Repo
|
||||
// values (never the trimmed, wire-safe RepoView), changes just the one
|
||||
// field, and republishes the full repository list through the same
|
||||
// validate-persist-publish path PATCH /api/config already uses -- see
|
||||
// bootstrap.go's installUpdateConfigHandler for why that matters: the wire
|
||||
// view omits fields (Preflight, TestGlob) that a naive round-trip through
|
||||
// ConfigView would silently drop.
|
||||
func (s *Server) handleConfigRepoUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
if s.UpdateRepoField == nil {
|
||||
http.Error(w, ErrConfigUpdateUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
owner := r.PathValue("owner")
|
||||
name := r.PathValue("name")
|
||||
var body struct {
|
||||
Field string `json:"field"`
|
||||
Value any `json:"value"`
|
||||
}
|
||||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if !RepoEditableFields[body.Field] {
|
||||
http.Error(w, "field "+body.Field+" is not editable from the dashboard", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.UpdateRepoField(r.Context(), owner, name, body.Field, body.Value); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, ErrConfigUpdateUnavailable):
|
||||
http.Error(w, err.Error(), http.StatusServiceUnavailable)
|
||||
case errors.Is(err, ErrConfigUpdateInvalid):
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
http.Error(w, "repo update failed: "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"ok": true})
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHandleConfigRepoUpdateAppliesViaSeam(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
type call struct {
|
||||
owner, name, field string
|
||||
value any
|
||||
}
|
||||
var got call
|
||||
srv.UpdateRepoField = func(_ context.Context, owner, name, field string, value any) error {
|
||||
got = call{owner, name, field, value}
|
||||
return nil
|
||||
}
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
|
||||
strings.NewReader(`{"field": "allow_concurrent", "value": true}`))
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
|
||||
}
|
||||
if got != (call{"acme", "widget", "allow_concurrent", true}) {
|
||||
t.Errorf("seam called with %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleConfigRepoUpdateRejectsUneditableFields proves the handler
|
||||
// itself refuses a field outside RepoEditableFields before ever calling the
|
||||
// seam -- Gate, Protect, and every other repo field stay file-edited.
|
||||
func TestHandleConfigRepoUpdateRejectsUneditableFields(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
called := false
|
||||
srv.UpdateRepoField = func(context.Context, string, string, string, any) error {
|
||||
called = true
|
||||
return nil
|
||||
}
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
|
||||
strings.NewReader(`{"field": "gate", "value": [["rm", "-rf", "/"]]}`))
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", w.Code)
|
||||
}
|
||||
if called {
|
||||
t.Error("UpdateRepoField was called for a field outside RepoEditableFields")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleConfigRepoUpdateUnavailableMapsTo503(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
srv.UpdateRepoField = nil
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
|
||||
strings.NewReader(`{"field": "allow_concurrent", "value": true}`))
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want 503", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleConfigRepoUpdateSeamErrorMapsTo400(t *testing.T) {
|
||||
srv := newTestServer(t)
|
||||
srv.UpdateRepoField = func(context.Context, string, string, string, any) error {
|
||||
return ErrConfigUpdateInvalid
|
||||
}
|
||||
|
||||
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config/repos/acme/widget",
|
||||
strings.NewReader(`{"field": "max_retries", "value": 5}`))
|
||||
w := httptest.NewRecorder()
|
||||
srv.Handler().ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,14 @@ type Server struct {
|
||||
// directly.
|
||||
ResetConfig func(context.Context, string) error
|
||||
|
||||
// UpdateRepoField changes one editable field (RepoEditableFields) on
|
||||
// the repository identified by owner/name, republishing the full
|
||||
// repository list through the same path as UpdateConfig. Optional:
|
||||
// when nil, PATCH /api/config/repos/{owner}/{name} answers 503. See
|
||||
// handleConfigRepoUpdate for why this needs its own seam rather than
|
||||
// going through UpdateConfig's dotted-key path directly.
|
||||
UpdateRepoField func(ctx context.Context, owner, name, field string, value any) error
|
||||
|
||||
// Channels reports actual adapter lifecycle, independently of configuration
|
||||
// presence. Nil preserves the configuration-only fallback for tests and
|
||||
// minimal embedding.
|
||||
@@ -276,6 +284,7 @@ func (s *Server) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
mux.HandleFunc("PATCH /api/config", s.handleConfigUpdate)
|
||||
mux.HandleFunc("POST /api/config/reset", s.handleConfigReset)
|
||||
mux.HandleFunc("PATCH /api/config/repos/{owner}/{name}", s.handleConfigRepoUpdate)
|
||||
mux.HandleFunc("GET /api/logs", s.handleLogs)
|
||||
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
|
||||
mux.HandleFunc("GET /api/logs/stream", s.handleLogStream)
|
||||
|
||||
Reference in new issue
Block a user