fix(api): 执行明细补齐 action 与目标知识库字段

逐项明细 DTO 漏了 action(前端 parseReleaseItemExecution 必需),点
「查看结果」整个明细区报 invalid release item execution。同时把遷移 000044
的 target_knowledge_base_id 读出来透出,页面逐项结果显示「目标库」,
让'目标知识库可选'在界面上可核对。测试:Go 契约用例 + 前端解析回归用例。
This commit is contained in:
姚章浩
2026-10-08 17:26:26 +08:00
parent 280a81c855
commit 86732ca987
8 changed files with 75 additions and 7 deletions
+2
View File
@@ -107,6 +107,8 @@ type ReleaseItemExecutionView struct {
ErrorCode string `json:"error_code"`
CreatedAt time.Time `json:"created_at"`
CompletedAt *time.Time `json:"completed_at"`
// TargetKnowledgeBaseID 是该项冻结的目标知识库(迁移 000044 起)。旧行可能为空。
TargetKnowledgeBaseID string `json:"target_knowledge_base_id"`
}
type ReleaseExecutionDetail struct {
@@ -3,6 +3,7 @@ package httpapi
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
@@ -130,3 +131,58 @@ func TestProductionActionPagePathStillRespectsGates(t *testing.T) {
t.Fatalf("body=%s", response.Body.String())
}
}
// 前端 parseReleaseItemExecution 要求每项明细都带 action,列表还要求批次进度字段;
// DTO 一旦漏字段,一条不合法就让整块明细/列表报 invalid release ...(线上就是这样)。
func TestReleaseExecutionDetailExposesItemFieldsThePageRequires(t *testing.T) {
now := time.Date(2026, time.October, 8, 9, 0, 0, 0, time.UTC)
store := &fakeJobStore{releaseExecutionDetailResult: governance.ReleaseExecutionDetail{
ReleaseExecution: governance.ReleaseExecution{
ID: "rx-1", ReleaseBatchID: "rb-1", Operation: governance.ReleaseOperationPublish,
State: governance.ReleaseExecutionCompleted, TargetEnvironment: "production",
AuthorizedBy: "admin", JobID: "job-1", BatchSize: 50, CurrentBatch: 1, TotalBatches: 1,
ItemCount: 1, CompletedItemCount: 1, CreatedAt: now,
},
Items: []governance.ReleaseItemExecutionView{{
PublicationID: "tp-1", Title: "手册.pdf", Action: "upsert", State: "verified",
ProductionRevision: "rv-1", TargetKnowledgeBaseID: "kb-target-1", CreatedAt: now,
}},
}}
handler, err := NewHandler(Config{Store: store, Now: func() time.Time { return now }})
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/release-executions/rx-1", nil))
if response.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
}
var payload struct {
Data struct {
BatchSize float64 `json:"batch_size"`
TotalBatch float64 `json:"total_batches"`
Items []struct {
Action string `json:"action"`
TargetKnowledgeBase string `json:"target_knowledge_base_id"`
ProductionRevision string `json:"production_revision"`
PreviousProductionRV string `json:"previous_production_revision"`
} `json:"items"`
} `json:"data"`
}
if err := json.Unmarshal(response.Body.Bytes(), &payload); err != nil {
t.Fatal(err)
}
if payload.Data.BatchSize != 50 || payload.Data.TotalBatch != 1 {
t.Fatalf("batch progress missing: %+v", payload.Data)
}
if len(payload.Data.Items) != 1 {
t.Fatalf("items=%+v", payload.Data.Items)
}
item := payload.Data.Items[0]
if item.Action != "upsert" {
t.Fatalf("action=%q, want upsert (前端解析必需)", item.Action)
}
if item.TargetKnowledgeBase != "kb-target-1" {
t.Fatalf("target_knowledge_base_id=%q", item.TargetKnowledgeBase)
}
}
+2 -1
View File
@@ -1857,10 +1857,11 @@ func getReleaseExecution(w http.ResponseWriter, r *http.Request, store Store) {
items := make([]map[string]any, 0, len(detail.Items))
for _, item := range detail.Items {
items = append(items, map[string]any{
"publication_id": item.PublicationID, "title": item.Title, "state": item.State,
"publication_id": item.PublicationID, "title": item.Title, "action": item.Action, "state": item.State,
"previous_production_revision": item.PreviousProductionRevision,
"production_revision": item.ProductionRevision, "error_code": item.ErrorCode,
"created_at": item.CreatedAt, "completed_at": item.CompletedAt,
"target_knowledge_base_id": item.TargetKnowledgeBaseID,
})
}
result["items"] = items
@@ -336,7 +336,8 @@ WHERE id = $1
const listReleaseExecutionItemsSQL = `
SELECT item.publication_id, source.title, item.action, item.state,
item.previous_production_revision, item.production_revision,
item.error_code, item.created_at, item.completed_at
item.error_code, item.created_at, item.completed_at,
COALESCE(item.target_knowledge_base_id, '')
FROM release_item_executions AS item
JOIN test_publications AS publication ON publication.id = item.publication_id
JOIN source_item_versions AS version ON version.id = publication.source_item_version_id
@@ -483,7 +484,7 @@ func (s *Store) GetReleaseExecution(ctx context.Context, executionID string) (go
var item governance.ReleaseItemExecutionView
if err := rows.Scan(&item.PublicationID, &item.Title, &item.Action, &item.State,
&item.PreviousProductionRevision, &item.ProductionRevision, &item.ErrorCode,
&item.CreatedAt, &item.CompletedAt); err != nil {
&item.CreatedAt, &item.CompletedAt, &item.TargetKnowledgeBaseID); err != nil {
return governance.ReleaseExecutionDetail{}, fmt.Errorf("scan release execution item: %w", err)
}
if item.Action != "upsert" && item.Action != "delete" {
+1 -1
View File
@@ -343,7 +343,7 @@ describe('ReleaseManagement', () => {
items: [{ id: 'rx-auto', release_batch_id: 'rb-safe', operation: 'rollback', prior_execution_id: 'rx-safe', state: 'running', target_environment: 'production', authorized_by: 'system:auto-rollback', job_id: 'job-auto', item_count: 1, batch_size: 50, current_batch: 1, total_batches: 1, completed_item_count: 0, failed_item_count: 0, error_code: '', created_at: now, started_at: now, completed_at: null }, { id: 'rx-safe', release_batch_id: 'rb-safe', operation: 'publish', prior_execution_id: '', state: 'failed', target_environment: 'production', authorized_by: 'owner', job_id: 'job-safe', item_count: 2, batch_size: 50, current_batch: 1, total_batches: 1, completed_item_count: 1, failed_item_count: 1, error_code: 'release_item_failed', created_at: priorNow, started_at: priorNow, completed_at: priorNow }],
total: 2, limit: 20, offset: 0,
}
const executionDetail: ReleaseExecutionDetail = { ...executionPage.items[0], items: [{ publication_id: 'tp-safe', title: 'Synthetic', action: 'delete', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: now, completed_at: now }, { publication_id: 'tp-restored', title: 'Restored', action: 'delete', state: 'rolled_back', previous_production_revision: 'prod-v0', production_revision: 'prod-v0', error_code: '', created_at: now, completed_at: now }] }
const executionDetail: ReleaseExecutionDetail = { ...executionPage.items[0], items: [{ publication_id: 'tp-safe', title: 'Synthetic', action: 'delete', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: now, completed_at: now, target_knowledge_base_id: '0b217e8a-2a65-40a3-ab8f-34ac5c7a6f05' }, { publication_id: 'tp-restored', title: 'Restored', action: 'delete', state: 'rolled_back', previous_production_revision: 'prod-v0', production_revision: 'prod-v0', error_code: '', created_at: now, completed_at: now, target_knowledge_base_id: '0b217e8a-2a65-40a3-ab8f-34ac5c7a6f05' }] }
const releasePlan: ReleasePlan = { items: [{ publication_id: 'tp-safe', title: 'B.pdf', target_scope: 'external', action: 'add', test_state: 'ready', production_state: 'absent', can_release: true, blocker: '' }, { publication_id: 'tp-delete', title: 'C.pdf', target_scope: 'internal', action: 'delete', test_state: 'not_required', production_state: 'present', can_release: true, blocker: '' }, { publication_id: '', title: 'D.pdf', target_scope: 'internal', action: 'blocked', test_state: 'ready', production_state: 'present', can_release: false, blocker: 'source_unavailable' }], summary: { add: 1, update: 0, delete: 1, blocked: 1, unchanged: 11284 } }
const html = renderToStaticMarkup(<ReleaseManagement
reviewPage={reviewPage} reviewLoading={false} reviewError={null} reviewingId={null}
+1 -1
View File
@@ -1060,7 +1060,7 @@ export function ReleaseManagement(props: ReleaseManagementProps) {
{!props.executionLoading && !props.executionError && props.executionPage.items.length === 0 ? <div className="empty-state">尚无生产发布或回滚执行记录。</div> : null}
{props.executionPage.items.length > 0 ? <div className="batch-grid execution-grid"><div className="table-wrap"><table><thead><tr><th>开始时间</th><th>操作</th><th>状态</th><th>批次进度</th><th>完成进度</th><th>授权人</th><th>操作</th></tr></thead><tbody>
{props.executionPage.items.map((execution) => <tr key={execution.id}><td>{formatTime(execution.created_at)}</td><td>{execution.operation === 'publish' ? '发布' : '回滚'}</td><td><span className={`status status-${execution.state}`}>{executionLabels[execution.state]}</span>{executionStateDetail(execution, props.executionPage.items) ? <small>{executionStateDetail(execution, props.executionPage.items)}</small> : null}{execution.error_code ? <small>{releaseFailureMessage(execution.error_code)}</small> : null}</td><td>{execution.total_batches>0?`批次 ${execution.current_batch} / ${execution.total_batches}`:'等待开始'}<small>每批最多 {execution.batch_size} 项</small></td><td>{execution.completed_item_count}/{execution.item_count}{execution.failed_item_count ? <small>失败 {execution.failed_item_count}</small> : null}</td><td>{execution.authorized_by}</td><td><div className="row-actions"><button type="button" className="secondary-action" onClick={() => props.onSelectExecution(execution.id)}>查看结果</button>{isAdmin && execution.state === 'failed' ? <button type="button" onClick={() => props.onStartReleaseAction?.('retry', execution.id, `执行 ${execution.id.slice(0, 8)}…`)}>重排</button> : null}{isAdmin && execution.operation === 'publish' && execution.state === 'completed' ? <button type="button" onClick={() => props.onStartReleaseAction?.('rollback', execution.id, `执行 ${execution.id.slice(0, 8)}…`)}>回滚</button> : null}</div></td></tr>)}
</tbody></table></div><div className="batch-detail execution-detail"><h3>每项知识的结果</h3>{props.executionDetail ? <>{props.executionDetail.items.map((item) => { const result = releaseItemResult(item); return <div className="batch-detail-row execution-detail-row" key={item.publication_id}><span><strong>{item.title}</strong><small>{result.detail}{item.error_code ? ` · ${releaseFailureMessage(item.error_code)}` : ''}</small></span><span><b>{result.outcome}</b></span></div> })}{props.executionDetail.items.length === 0 ? <div className="empty-state">发布尚无逐项结果。</div> : null}</> : <div className="empty-state">点击“查看结果”核对每项知识。</div>}</div></div> : null}
</tbody></table></div><div className="batch-detail execution-detail"><h3>每项知识的结果</h3>{props.executionDetail ? <>{props.executionDetail.items.map((item) => { const result = releaseItemResult(item); return <div className="batch-detail-row execution-detail-row" key={item.publication_id}><span><strong>{item.title}</strong><small>{result.detail}{item.target_knowledge_base_id ? ` · 目标库 ${item.target_knowledge_base_id.slice(0, 8)}…` : ''}{item.error_code ? ` · ${releaseFailureMessage(item.error_code)}` : ''}</small></span><span><b>{result.outcome}</b></span></div> })}{props.executionDetail.items.length === 0 ? <div className="empty-state">发布尚无逐项结果。</div> : null}</> : <div className="empty-state">点击“查看结果”核对每项知识。</div>}</div></div> : null}
{props.executionChain ? <div className="release-audit-chain">
<h3>发布审计链</h3>
<div className="audit-chain-line"><span>批次</span><b>{props.executionChain.release_batch_name || '未绑定批次'}</b><small>{`创建 ${props.executionChain.created_by || '—'} · 锁定 ${props.executionChain.sealed_by || '—'} · 授权 ${props.executionChain.authorized_by || '—'}`}</small></div>
+8 -2
View File
@@ -114,11 +114,17 @@ describe('release execution audit parsing', () => {
expect(page.items[0]).toMatchObject({ batch_size: 50, current_batch: 2, total_batches: 3 })
expect(page.items[0]).not.toHaveProperty('authorization_code')
expect(page.items[0]).not.toHaveProperty('manifest_sha256')
const detail = parseReleaseExecutionDetailResponse({ data: { ...execution, items: [{ publication_id: 'tp-safe', title: 'Synthetic', action: 'delete', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: '2026-09-06T10:00:00Z', completed_at: '2026-09-06T10:02:00Z', knowledge_id: 'must-drop' }] } })
expect(detail.items[0]).toEqual({ publication_id: 'tp-safe', title: 'Synthetic', action: 'delete', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: '2026-09-06T10:00:00Z', completed_at: '2026-09-06T10:02:00Z' })
const detail = parseReleaseExecutionDetailResponse({ data: { ...execution, items: [{ publication_id: 'tp-safe', title: 'Synthetic', action: 'delete', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: '2026-09-06T10:00:00Z', completed_at: '2026-09-06T10:02:00Z', target_knowledge_base_id: 'kb-target-1', knowledge_id: 'must-drop' }] } })
expect(detail.items[0]).toEqual({ publication_id: 'tp-safe', title: 'Synthetic', action: 'delete', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: '2026-09-06T10:00:00Z', completed_at: '2026-09-06T10:02:00Z', target_knowledge_base_id: 'kb-target-1' })
expect(parseReleaseCapabilitiesResponse({ data: { production_release_requests_enabled: true, production_backup_restore_verified: false, secret: 'drop' } })).toEqual({ production_release_requests_enabled: true, production_backup_restore_verified: false })
})
it('rejects a release item execution the API forgot to describe', () => {
// 线上 DTO 曾漏 action:一条明细不合法就让整个明细区报 invalid release item execution。
const missingAction = { publication_id: 'tp-safe', title: 'Synthetic', state: 'verified', previous_production_revision: 'prod-v0', production_revision: 'absent', error_code: '', created_at: '2026-09-06T10:00:00Z', completed_at: null }
expect(() => parseReleaseExecutionDetailResponse({ data: { ...execution, items: [missingAction] } })).toThrow('invalid release item execution')
})
it('fetches only read-only execution audit endpoints', async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ data: [execution], total: 1, limit: 20, offset: 0 }), { status: 200 }))
+2
View File
@@ -113,6 +113,7 @@ export interface ReleaseItemExecution {
error_code: string
created_at: string
completed_at: string | null
target_knowledge_base_id: string
}
export interface ReleaseExecutionDetail extends ReleaseExecution {
@@ -450,6 +451,7 @@ function parseReleaseItemExecution(raw: unknown): ReleaseItemExecution {
return {
publication_id: raw.publication_id, title: raw.title, action: raw.action, state: raw.state as ReleaseItemExecution['state'],
previous_production_revision: raw.previous_production_revision, production_revision: raw.production_revision,
target_knowledge_base_id: typeof raw.target_knowledge_base_id === 'string' ? raw.target_knowledge_base_id : '',
error_code: raw.error_code, created_at: raw.created_at, completed_at: raw.completed_at as string | null,
}
}