feat(qaops): 增加版本化部门区域归属配置与预览

This commit is contained in:
姚章浩
2026-10-17 11:39:38 +08:00
parent fd04bf7886
commit dab74b706b
23 changed files with 1693 additions and 51 deletions
+2
View File
@@ -20,6 +20,7 @@ import (
"knowledge-governance/internal/monitoring"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/platform/weknora"
"knowledge-governance/internal/qaopsorg"
"knowledge-governance/internal/serviceverify"
)
@@ -133,6 +134,7 @@ func run(ctx context.Context, getenv func(string) string) error {
log.Print("QA operations source is unavailable; governance remains online")
}
handler, err := httpapi.NewHandler(httpapi.Config{
QAOrg: qaopsorg.NewStore(pool, getenv("DOBOTAI_OPS_TENANT_KEY")),
QAOps: qaReader,
QAOpsTenantKey: qaSettings.TenantKey,
QAOpsEnabled: qaSettings.Enabled,
+62
View File
@@ -0,0 +1,62 @@
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"knowledge-governance/internal/qaopsorg"
)
func main() {
if err := run(os.Args[1:], os.Getenv, os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, "directory import failed; check private input and governance connection (details withheld)")
os.Exit(1)
}
}
func run(args []string, getenv func(string) string, out io.Writer) error {
if len(args) != 1 || getenv("QAOPS_CONFIRM_DIRECTORY_IMPORT") != "import-governance-directory" || getenv("DOBOTAI_OPS_TENANT_KEY") == "" || getenv("DATABASE_URL") == "" {
return errors.New("explicit input, tenant, governance database and confirmation required")
}
info, err := os.Lstat(args[0])
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0077 != 0 || info.Size() > 16<<20 {
return errors.New("input must be a private regular JSON file")
}
file, err := os.Open(args[0])
if err != nil {
return errors.New("input unavailable")
}
defer file.Close()
decoder := json.NewDecoder(io.LimitReader(file, (16<<20)+1))
decoder.DisallowUnknownFields()
var input qaopsorg.Import
if decoder.Decode(&input) != nil || decoder.Decode(&struct{}{}) != io.EOF {
return errors.New("input invalid")
}
if !input.Complete {
return qaopsorg.ErrInvalid
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
pool, err := pgxpool.New(ctx, getenv("DATABASE_URL"))
if err != nil {
return qaopsorg.ErrUnavailable
}
defer pool.Close()
store := qaopsorg.NewStore(pool, getenv("DOBOTAI_OPS_TENANT_KEY"))
saved, err := store.ReplaceDirectory(ctx, input)
if err != nil {
return err
}
active, err := store.GetView(ctx)
if err != nil || active.DirectoryID != saved.DirectoryID {
return qaopsorg.ErrConflict
}
return json.NewEncoder(out).Encode(map[string]any{"directory_id": active.DirectoryID, "source": active.DirectorySource, "synced_at": active.DirectorySyncedAt, "departments": len(active.View.Departments), "people": active.View.TotalPeople, "readback_verified": true})
}
+31
View File
@@ -0,0 +1,31 @@
package main
import (
"bytes"
"os"
"path/filepath"
"testing"
)
func TestDirectoryImportRequiresExplicitGovernanceConfirmationAndPrivateInput(t *testing.T) {
var output bytes.Buffer
if run(nil, func(string) string { return "" }, &output) == nil || output.Len() != 0 {
t.Fatal("unconfigured importer must not connect or report success")
}
path := filepath.Join(t.TempDir(), "directory.json")
if os.WriteFile(path, []byte(`{"complete":false}`), 0644) != nil {
t.Fatal("fixture creation failed")
}
env := func(k string) string {
return map[string]string{"QAOPS_CONFIRM_DIRECTORY_IMPORT": "import-governance-directory", "DOBOTAI_OPS_TENANT_KEY": "fixture", "DATABASE_URL": "postgres://invalid.invalid/no_connection_expected"}[k]
}
if run([]string{path}, env, &output) == nil {
t.Fatal("public personnel input accepted")
}
if os.Chmod(path, 0600) != nil {
t.Fatal("fixture chmod failed")
}
if run([]string{path}, env, &output) == nil || output.Len() != 0 {
t.Fatal("partial sync accepted or importer reported fabricated success")
}
}