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 generic template clone lookup (#325)

fx-kurumif4897cbd73ab
diff --git a/internal/httpapi/generic_clone_lookup_test.go b/internal/httpapi/generic_clone_lookup_test.go
new file mode 100644
index 00000000..1fcd0858
--- /dev/null
+++ b/internal/httpapi/generic_clone_lookup_test.go
@@ -0,0 +1,52 @@
+package httpapi
+
+import "testing"
+
+type genericCloneStub struct {
+ ID string
+ ProjectID string
+}
+
+func TestFindGenericProjectClone(t *testing.T) {
+ items := map[string]genericCloneStub{
+ "plain": {ID: "plain", ProjectID: "p1"},
+ "shared": {ID: "shared", ProjectID: "other"},
+ "shared-p1": {ID: "shared-p1", ProjectID: "p1"},
+ "foreign": {ID: "foreign", ProjectID: "other"},
+ "foreign-p1": {ID: "foreign-p1", ProjectID: "other"},
+ }
+ var looked []string
+ lookup := func(id string) (genericCloneStub, bool) {
+ looked = append(looked, id)
+ item, ok := items[id]
+ return item, ok
+ }
+ owner := func(v genericCloneStub) string { return v.ProjectID }
+
+ cases := []struct {
+ source string
+ wantID string
+ found bool
+ looked []string
+ }{
+ {source: "plain", wantID: "plain", found: true, looked: []string{"plain"}},
+ {source: "shared", wantID: "shared-p1", found: true, looked: []string{"shared", "shared-p1"}},
+ {source: "foreign", found: false, looked: []string{"foreign", "foreign-p1"}},
+ {source: "missing", found: false, looked: []string{"missing", "missing-p1"}},
+ }
+ for _, tc := range cases {
+ looked = nil
+ got, found := findGenericProjectClone(tc.source, "p1", lookup, owner)
+ if found != tc.found || got.ID != tc.wantID {
+ t.Fatalf("%s: got %#v found=%v, want id=%q found=%v", tc.source, got, found, tc.wantID, tc.found)
+ }
+ if len(looked) != len(tc.looked) {
+ t.Fatalf("%s: looked up %v, want %v", tc.source, looked, tc.looked)
+ }
+ for i := range looked {
+ if looked[i] != tc.looked[i] {
+ t.Fatalf("%s: looked up %v, want %v", tc.source, looked, tc.looked)
+ }
+ }
+ }
+}
diff --git a/internal/httpapi/generic_workflows.go b/internal/httpapi/generic_workflows.go
index b018cba6..34594bee 100644
--- a/internal/httpapi/generic_workflows.go
+++ b/internal/httpapi/generic_workflows.go
@@ -92,15 +92,10 @@ func (h *catalogHandlers) cloneGenericTemplateAgents(workflowID, projectID strin
}
remap := make(map[string]string, len(template.Agents))
for _, source := range template.Agents {
- if existing, found := h.repo.Agent(source.ID); found && existing.ProjectID == projectID {
- h.upgradeGenericTemplateAgent(&existing, source, projectID, now)
- remap[source.ID] = existing.ID
- continue
- }
// Projects that did not get the plain template id (a sibling project
// owns it) hold a deterministic <id>-<project> clone instead; reuse it
// so re-adopting the template never duplicates the roles.
- if existing, found := h.repo.Agent(source.ID + "-" + projectID); found && existing.ProjectID == projectID {
+ if existing, found := findGenericProjectClone(source.ID, projectID, h.repo.Agent, func(a domain.AgentDefinition) string { return a.ProjectID }); found {
h.upgradeGenericTemplateAgent(&existing, source, projectID, now)
remap[source.ID] = existing.ID
continue
@@ -143,11 +138,7 @@ func (h *catalogHandlers) upgradeGenericTemplateAgent(existing *domain.AgentDefi
func (h *catalogHandlers) cloneGenericTemplateRules(sources []domain.RuleDefinition, projectID string, now time.Time) (map[string]string, error) {
remap := make(map[string]string, len(sources))
for _, source := range sources {
- if existing, found := h.repo.Rule(source.ID); found && existing.ProjectID == projectID {
- remap[source.ID] = existing.ID
- continue
- }
- if existing, found := h.repo.Rule(source.ID + "-" + projectID); found && existing.ProjectID == projectID {
+ if existing, found := findGenericProjectClone(source.ID, projectID, h.repo.Rule, func(v domain.RuleDefinition) string { return v.ProjectID }); found {
remap[source.ID] = existing.ID
continue
}
@@ -167,11 +158,7 @@ func (h *catalogHandlers) cloneGenericTemplateRules(sources []domain.RuleDefinit
func (h *catalogHandlers) cloneGenericTemplateSkills(sources []domain.SkillDefinition, projectID string, now time.Time) (map[string]string, error) {
remap := make(map[string]string, len(sources))
for _, source := range sources {
- if existing, found := h.repo.Skill(source.ID); found && existing.ProjectID == projectID {
- remap[source.ID] = existing.ID
- continue
- }
- if existing, found := h.repo.Skill(source.ID + "-" + projectID); found && existing.ProjectID == projectID {
+ if existing, found := findGenericProjectClone(source.ID, projectID, h.repo.Skill, func(v domain.SkillDefinition) string { return v.ProjectID }); found {
remap[source.ID] = existing.ID
continue
}
@@ -213,6 +200,19 @@ func genericAssetIDCandidates(sourceID, projectID string) []string {
return []string{sourceID, sourceID + "-" + projectID}
}
+// findGenericProjectClone returns the first genericAssetIDCandidates entry that
+// already exists and belongs to projectID, so re-adopting a template reuses the
+// project's clone instead of creating a duplicate.
+func findGenericProjectClone[T any](sourceID, projectID string, lookup func(string) (T, bool), ownerOf func(T) string) (T, bool) {
+ for _, id := range genericAssetIDCandidates(sourceID, projectID) {
+ if existing, found := lookup(id); found && ownerOf(existing) == projectID {
+ return existing, true
+ }
+ }
+ var zero T
+ return zero, false
+}
+
func (h *catalogHandlers) availableGenericRuleID(sourceID, projectID string) string {
return h.pickAvailableID(genericAssetIDCandidates(sourceID, projectID), func(id string) bool {
_, taken := h.repo.Rule(id)