Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(httpapi): merge duplicated rules/skills project filter into filterOwnedByProject (#369)

fx-kurumi4328a5901ffd
diff --git a/internal/httpapi/catalog.go b/internal/httpapi/catalog.go
index f8a9a485..5bc5641c 100644
--- a/internal/httpapi/catalog.go
+++ b/internal/httpapi/catalog.go
@@ -597,13 +597,7 @@ func (h *catalogHandlers) rules(c *gin.Context) {
if c.Request.Method == http.MethodGet {
items := h.repo.Rules()
if projectID := strings.TrimSpace(c.Query("projectId")); projectID != "" {
- filtered := make([]domain.RuleDefinition, 0, len(items))
- for _, item := range items {
- if item.ProjectID == projectID {
- filtered = append(filtered, item)
- }
- }
- items = filtered
+ items = filterOwnedByProject(items, projectID, func(item domain.RuleDefinition) string { return item.ProjectID })
}
c.JSON(200, items)
return
@@ -646,13 +640,7 @@ func (h *catalogHandlers) skills(c *gin.Context) {
if c.Request.Method == http.MethodGet {
items := h.repo.Skills()
if projectID := strings.TrimSpace(c.Query("projectId")); projectID != "" {
- filtered := make([]domain.SkillDefinition, 0, len(items))
- for _, item := range items {
- if item.ProjectID == projectID {
- filtered = append(filtered, item)
- }
- }
- items = filtered
+ items = filterOwnedByProject(items, projectID, func(item domain.SkillDefinition) string { return item.ProjectID })
}
c.JSON(200, items)
return
@@ -916,6 +904,19 @@ func filterEnvironmentAssets[T any](items []T, projectID string, scope func(T) (
return out
}
+// filterOwnedByProject keeps only items owned by projectID. Unlike
+// filterEnvironmentAssets it does not admit shared items from other projects.
+// The result is never nil so list endpoints keep returning [] instead of null.
+func filterOwnedByProject[T any](items []T, projectID string, owner func(T) string) []T {
+ out := make([]T, 0, len(items))
+ for _, item := range items {
+ if owner(item) == projectID {
+ out = append(out, item)
+ }
+ }
+ return out
+}
+
func (h *catalogHandlers) validateEnvironmentAsset(id, name, projectID, kind string) error {
if id == "" || strings.TrimSpace(name) == "" || projectID == "" {
return fmt.Errorf("%s 需要 id、name 和 projectId", kind)
diff --git a/internal/httpapi/catalog_project_filter_test.go b/internal/httpapi/catalog_project_filter_test.go
new file mode 100644
index 00000000..9279ed0c
--- /dev/null
+++ b/internal/httpapi/catalog_project_filter_test.go
@@ -0,0 +1,74 @@
+package httpapi
+
+import (
+ "encoding/json"
+ "net/http"
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestFilterOwnedByProject(t *testing.T) {
+ items := []domain.RuleDefinition{
+ {ID: "a", ProjectID: "p1"},
+ {ID: "b", ProjectID: "p2", Shared: true},
+ {ID: "c", ProjectID: ""},
+ {ID: "d", ProjectID: "p1", Shared: true},
+ }
+ owner := func(item domain.RuleDefinition) string { return item.ProjectID }
+
+ got := filterOwnedByProject(items, "p1", owner)
+ if len(got) != 2 || got[0].ID != "a" || got[1].ID != "d" {
+ t.Fatalf("filter p1 = %+v, want [a d] in order", got)
+ }
+ // Shared items from other projects are not admitted.
+ if got := filterOwnedByProject(items, "p3", owner); got == nil || len(got) != 0 {
+ t.Fatalf("filter p3 = %#v, want non-nil empty slice", got)
+ }
+ if got := filterOwnedByProject[domain.RuleDefinition](nil, "p1", owner); got == nil || len(got) != 0 {
+ t.Fatalf("filter nil = %#v, want non-nil empty slice", got)
+ }
+}
+
+func TestCatalogRulesAndSkillsListFilterByProject(t *testing.T) {
+ f := cvBNewHTTPFixture(t)
+ for _, p := range []domain.Project{
+ {ID: "pf-one", Key: "PFONE", Name: "PF One", OwnerID: f.user.ID},
+ {ID: "pf-two", Key: "PFTWO", Name: "PF Two", OwnerID: f.user.ID},
+ {ID: "pf-empty", Key: "PFEMPTY", Name: "PF Empty", OwnerID: f.user.ID},
+ } {
+ if err := f.s.PutProject(p); err != nil {
+ t.Fatal(err)
+ }
+ }
+ for _, kind := range []string{"rules", "skills"} {
+ for _, body := range []map[string]any{
+ {"id": "pf-" + kind + "-one", "name": "One", "projectId": "pf-one"},
+ {"id": "pf-" + kind + "-two", "name": "Two", "projectId": "pf-two", "shared": true},
+ } {
+ if rec := cvBRequest(t, f, http.MethodPut, "/api/"+kind, body, true); rec.Code != http.StatusOK {
+ t.Fatalf("put %s %v = %d: %s", kind, body["id"], rec.Code, rec.Body.String())
+ }
+ }
+
+ rec := cvBRequest(t, f, http.MethodGet, "/api/"+kind+"?projectId=pf-one", nil, true)
+ if rec.Code != http.StatusOK {
+ t.Fatalf("list %s = %d: %s", kind, rec.Code, rec.Body.String())
+ }
+ var items []struct {
+ ID string `json:"id"`
+ ProjectID string `json:"projectId"`
+ }
+ if err := json.Unmarshal(rec.Body.Bytes(), &items); err != nil {
+ t.Fatal(err)
+ }
+ if len(items) != 1 || items[0].ID != "pf-"+kind+"-one" || items[0].ProjectID != "pf-one" {
+ t.Fatalf("list %s?projectId=pf-one = %+v, want only pf-%s-one", kind, items, kind)
+ }
+
+ rec = cvBRequest(t, f, http.MethodGet, "/api/"+kind+"?projectId=pf-empty", nil, true)
+ if rec.Code != http.StatusOK || rec.Body.String() != "[]" {
+ t.Fatalf("list %s?projectId=pf-empty = %d %q, want 200 []", kind, rec.Code, rec.Body.String())
+ }
+ }
+}