diff --git a/internal/httpapi/catalog_cases_test.go b/internal/httpapi/catalog_cases_test.go index a3cdaa431..729b457a8 100644 --- a/internal/httpapi/catalog_cases_test.go +++ b/internal/httpapi/catalog_cases_test.go @@ -142,6 +142,25 @@ func TestCatalogWorkflowBranches(t *testing.T) { } } + draft := requestJSONAuthed[domain.WorkflowDefinition](t, f.h, f.user, http.MethodGet, "/api/workflows/cvb-flow", nil, http.StatusOK) + if draft.Status != "draft" || draft.PublishedRevision != 0 { + t.Fatalf("saved workflow = %+v", draft) + } + const publishPath = "/api/workflows/cvb-flow/publish" + first := requestJSONAuthed[domain.WorkflowDefinition](t, f.h, f.user, http.MethodPost, publishPath, nil, http.StatusOK) + if first.Status != "published" || first.Version != 1 || first.PublishedRevision != first.Revision { + t.Fatalf("first publication = %+v", first) + } + first.Description = "Updated after the first publication" + saved := requestJSONAuthed[domain.WorkflowDefinition](t, f.h, f.user, http.MethodPut, "/api/workflows", first, http.StatusOK) + if saved.Version != 1 || saved.PublishedRevision != first.Revision || saved.Revision <= first.Revision { + t.Fatalf("saved edit = %+v", saved) + } + second := requestJSONAuthed[domain.WorkflowDefinition](t, f.h, f.user, http.MethodPost, publishPath, nil, http.StatusOK) + if second.Version != 2 || second.PublishedRevision != second.Revision { + t.Fatalf("second publication = %+v", second) + } + // Deletion conflicts: active run, then bound issue. if err := f.s.PutRun(domain.Run{ID: "cvb-active", WorkflowID: "cvb-flow", Status: "running", Nodes: map[string]*domain.NodeRun{}}); err != nil { t.Fatal(err) diff --git a/internal/repo/catalog.go b/internal/repo/catalog.go index faaed9be6..3ce12246f 100644 --- a/internal/repo/catalog.go +++ b/internal/repo/catalog.go @@ -473,6 +473,14 @@ func workflowNeedsPublish(v domain.WorkflowDefinition) bool { return v.Status != "published" || workflowPublishedRevision(v) != v.Revision } +func nextWorkflowVersion(v domain.WorkflowDefinition) int { + // Newly saved drafts already carry version 1 for compatibility. + if v.Version < 1 || (v.Version == 1 && workflowPublishedRevision(v) == 0) { + return 1 + } + return v.Version + 1 +} + // PublishWorkflow is idempotent until the saved definition changes. func (r *CatalogRepo) PublishWorkflow(id string) (domain.WorkflowDefinition, error) { var published domain.WorkflowDefinition @@ -498,8 +506,8 @@ func (r *CatalogRepo) publishStoredWorkflow(id string) (domain.WorkflowDefinitio if !workflowNeedsPublish(v) { return v, nil } + v.Version = nextWorkflowVersion(v) v.Status = "published" - v.Version++ if err := r.putWorkflow(v, true); err != nil { return domain.WorkflowDefinition{}, err } diff --git a/internal/repo/generic_store.go b/internal/repo/generic_store.go index 568c67dea..95db00b8e 100644 --- a/internal/repo/generic_store.go +++ b/internal/repo/generic_store.go @@ -196,13 +196,10 @@ func (r *CatalogRepo) publishGenericWorkflow(id string) (domain.WorkflowDefiniti if !workflowNeedsPublish(template.Workflow) { return template.Workflow, nil } + template.Workflow.Version = nextWorkflowVersion(template.Workflow) template.Workflow.Status = "published" - template.Workflow.Version++ template.Workflow.Revision++ template.Workflow.PublishedRevision = template.Workflow.Revision - if template.Workflow.Version < 1 { - template.Workflow.Version = 1 - } template.Workflow.UpdatedAt = time.Now().UTC() if err := r.putGenericTemplate(template); err != nil { return domain.WorkflowDefinition{}, err diff --git a/internal/repo/workflow_publish_test.go b/internal/repo/workflow_publish_test.go index 7e10bdddb..b4bafd240 100644 --- a/internal/repo/workflow_publish_test.go +++ b/internal/repo/workflow_publish_test.go @@ -54,7 +54,7 @@ func TestMySQLConcurrentWorkflowPublish(t *testing.T) { defer wg.Done() <-start published, err := publish(id) - if err != nil || published.Version != 2 || published.PublishedRevision != published.Revision { + if err != nil || published.Version != 1 || published.PublishedRevision != published.Revision { t.Errorf("concurrent publish %s: version=%d revision=%d marker=%d err=%v", id, published.Version, published.Revision, published.PublishedRevision, err) } }() @@ -64,6 +64,52 @@ func TestMySQLConcurrentWorkflowPublish(t *testing.T) { } } +func TestWorkflowFirstPublishUsesVersionOne(t *testing.T) { + for _, generic := range []bool{false, true} { + t.Run(map[bool]string{false: "project", true: "generic"}[generic], func(t *testing.T) { + s := testStoreWithGenericTemplates(t) + draft := domain.WorkflowDefinition{ID: "first-publish", Name: "First publish", Status: "draft", Version: 1, Revision: 1} + save := s.PutWorkflow + publish := s.PublishWorkflow + if generic { + if err := s.PutGenericTemplate(GenericTemplate{Workflow: draft}); err != nil { + t.Fatal(err) + } + save = func(v domain.WorkflowDefinition) error { _, err := s.UpdateGenericWorkflow(v); return err } + publish = s.PublishGenericWorkflow + } else if err := save(draft); err != nil { + t.Fatal(err) + } + draft, _ = s.Workflow(draft.ID) + draft.Description = "Saved before publishing" + if err := save(draft); err != nil { + t.Fatal(err) + } + draft, _ = s.Workflow(draft.ID) + if draft.Status != "draft" || draft.Version != 1 || draft.PublishedRevision != 0 { + t.Fatalf("saved draft = %+v", draft) + } + first, err := publish(draft.ID) + if err != nil || first.Status != "published" || first.Version != 1 || first.PublishedRevision != first.Revision || first.Revision <= draft.Revision { + t.Fatalf("first publication = %+v, %v", first, err) + } + again, err := publish(draft.ID) + if err != nil || again.Version != first.Version || again.Revision != first.Revision { + t.Fatalf("repeated publication = %+v, %v", again, err) + } + first.Status = "draft" + first.Description = "Saved after publishing v1" + if err := save(first); err != nil { + t.Fatal(err) + } + second, err := publish(draft.ID) + if err != nil || second.Version != 2 || second.PublishedRevision != second.Revision { + t.Fatalf("second publication = %+v, %v", second, err) + } + }) + } +} + func TestWorkflowPublishOnlyAfterChanges(t *testing.T) { for _, generic := range []bool{false, true} { t.Run(map[bool]string{false: "project", true: "generic"}[generic], func(t *testing.T) { diff --git a/web/app/flow-canvas.css b/web/app/flow-canvas.css index 4b61ca911..fac24e06f 100644 --- a/web/app/flow-canvas.css +++ b/web/app/flow-canvas.css @@ -145,7 +145,7 @@ html[data-theme="dark"] #flowView{ #flowView .fd-body{position:relative;flex:1;display:flex;min-height:0;min-width:0} /* palette */ -#flowView .fd-palette{position:relative;z-index:12;width:256px;flex:none;display:flex;flex-direction:column;min-height:0;border-right:1px solid var(--fd-line);background:var(--fd-surface);transition:width .2s var(--fd-ease)} +#flowView .fd-palette{position:relative;z-index:12;width:min(256px,38%);flex:none;display:flex;flex-direction:column;min-height:0;border-right:1px solid var(--fd-line);background:var(--fd-surface);transition:width .2s var(--fd-ease)} #flowView .fd-pal-head{display:flex;align-items:center;gap:6px;padding:12px 12px 6px} #flowView .fd-pal-body{flex:1;overflow:auto;padding:2px 8px 12px} #flowView .fd-pal-group+.fd-pal-group{margin-top:6px} @@ -280,7 +280,8 @@ html[data-theme="dark"] #flowView{ /* floating chrome */ #flowView .fd-chrome{position:absolute;z-index:15;display:flex;align-items:center;gap:2px;padding:4px;border:1px solid var(--fd-line);border-radius:11px;background:var(--fd-glass);-webkit-backdrop-filter:blur(12px) saturate(1.4);backdrop-filter:blur(12px) saturate(1.4);box-shadow:var(--fd-shadow);white-space:nowrap;transition:right .24s var(--fd-ease),left .24s var(--fd-ease),opacity .2s} #flowView .fd-tr{top:12px;right:12px} -#flowView .fd-bl{left:12px;bottom:12px} +#flowView .fd-bl{left:12px;bottom:12px;max-width:calc(100% - 24px);flex-wrap:wrap} +#flowView #canvasArrange{height:30px;padding:0 8px} #flowView .fd-sep{width:1px;height:18px;margin:0 4px;background:var(--fd-line-2);flex:none} #flowView .fd-zoomv{min-width:46px;height:30px;padding:0 4px;border:0;border-radius:7px;background:none;color:var(--fd-ink-2);font:500 12px/1 ui-monospace,SFMono-Regular,Menlo,monospace;cursor:pointer} #flowView .fd-zoomv:hover{background:var(--fd-hover)} diff --git a/web/app/flow-palette.js b/web/app/flow-palette.js index 631a915e0..5554253f1 100644 --- a/web/app/flow-palette.js +++ b/web/app/flow-palette.js @@ -156,7 +156,7 @@ const flowPalette=(()=>{ host.innerHTML=switcherGroups().map(g=>{ const items=g.items.filter(w=>matches(q,w.name||'',w.id||'')); if(!items.length)return ''; - return `
还没有历史。保存一次修改后才会留下上一版。
'; + $('#revisionList').innerHTML=items.length?items.map(r=>{ + const workflow=kind==='workflow'&&revisionPayload(r); + const initialDraft=workflow?.status&&!workflowVersionLabel(workflow); + const summary=initialDraft?r.name:r.summary||r.name; + const version=initialDraft?lfT('草稿','Draft'):`v${r.version}`; + return ``; + }).join(''):'还没有历史。保存一次修改后才会留下上一版。
'; $('#revisionDiff').textContent='选择一个历史版本,查看和当前的差异。'; $('#revisionRestore').disabled=true; $('#revisionList').onclick=ev=>{const btn=ev.target.closest('[data-rev]');if(btn)showRevisionDiff(btn.dataset.rev)}; @@ -28,7 +38,7 @@ function showRevisionDiff(id){ let current={}; if(state.revisionKind==='workflow')current=state.workflow||{}; else current=(state.data.agents||[]).find(a=>a.id===state.revisionEntity)||{}; - let prev=rev.payload;if(typeof prev==='string'){try{prev=JSON.parse(prev)}catch{prev={}}} + const prev=revisionPayload(rev); $('#revisionDiff').textContent=lineDiff(JSON.stringify(prev,null,2),JSON.stringify(current,null,2)); $('#revisionRestore').disabled=false; } diff --git a/web/app/sops.js b/web/app/sops.js index c6502cfec..cc01e3a08 100644 --- a/web/app/sops.js +++ b/web/app/sops.js @@ -136,6 +136,9 @@ function sopBadge(label,tone,icon,title){ return `${mark}${escapeHTML(label)}`; } function sopHealthBadge(health){const {tone,icon}=sopHealthTone(health);return sopBadge(health?.label||'',tone,icon)} +function workflowVersionLabel(w){ + return w.publishedRevision||w.status==='published'||w.version>1 ? `v${w.version||1}` : ''; +} function sopStatusBadge(w){ if(catalogGeneric(w))return sopBadge('通用','accent','layers'); if(w?.status==='published')return sopBadge('已发布','success','check'); @@ -153,13 +156,13 @@ function sopRunCardBody(w,m,liveCount){ } function projectSOPCard(w,m={},liveCount=0){ const recent=formatTimestamp(m.lastRunAt); - const meta=`${w.owner||'未分配负责人'} · v${w.version||1} · ${recent?`最近 ${recent}`:'尚未运行'}`; + const meta=[w.owner||'未分配负责人',workflowVersionLabel(w),recent?`最近 ${recent}`:'尚未运行'].filter(Boolean).join(' · '); return `