feat(release): 批量审核 + 建批即锁定 —— 发布管理单文档交互降到 3~4 次

P0-3:
- 批量审核:新增 POST /api/v1/review-candidates/batch-decision,逐条复用单条审核的写入
  路径(同一套合法性校验与"最新验收轮次"绑定),逐条回报 decided/skipped/skipped_ids;
  不可审核的条目只计入 skipped,不打断整批,其他错误整批回滚(不留半成品)。
  界面新增「全选可批准」「批量批准选中」「批量驳回选中」。
- 建批即锁定:建批请求加 seal 字段,服务端在创建后立即执行既有的密封(仍逐项复核门禁,
  失败整批回滚),草稿态不再单独占一步;界面按钮改为「创建并锁定清单」,流程说明从 8 步改 7 步。
- 判据修正:「可批准」原先把已驳回的算进去(review_state !== 'approved'),改为只含 pending + 门禁通过。

语义边界(写进测试):批量批准按人工身份记录,人工可推翻自己先前的驳回;system:auto 发起的
批量批准对人工驳回过的那条必须跳过(Decided=0/Skipped=1)——机器永远不覆盖人工。

测试:review_batch_integration_test.go(批量决定/跳报/重复 id 拒绝/机器不覆盖人工、
建批即锁定)、review_batch_test.go(幂等键、400/409/500/501 映射、服务端盖操作人)、
reviewRelease.test.ts(批量请求体与逐条回报、建批带 seal)。前端 181 条通过。
This commit is contained in:
姚章浩
2026-10-10 15:29:31 +08:00
parent c83a6c8086
commit 70131c6161
15 changed files with 575 additions and 42 deletions
+118
View File
@@ -0,0 +1,118 @@
package httpapi
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"knowledge-governance/internal/governance"
"knowledge-governance/internal/platform/postgres"
)
// reviewBatchFakeStore:在既有 fakeJobStore 之上补出批量审核能力,验证批量端点的
// 幂等键要求、逐条结果回报与错误映射。
type reviewBatchFakeStore struct {
fakeJobStore
captured governance.ReviewPublicationsBatchRequest
result governance.ReviewPublicationsBatchResult
replayed bool
err error
}
func (s *reviewBatchFakeStore) ReviewPublicationsBatchWithOperation(_ context.Context, request governance.ReviewPublicationsBatchRequest, _ string) (governance.ReviewPublicationsBatchResult, bool, error) {
s.captured = request
return s.result, s.replayed, s.err
}
func TestBatchReviewCandidatesHandler(t *testing.T) {
store := &reviewBatchFakeStore{result: governance.ReviewPublicationsBatchResult{Selected: 3, Decided: 2, Skipped: 1, SkippedIDs: []string{"tp-c"}, Reviews: []governance.PublicationReview{}}}
handler, err := NewHandler(Config{Store: store})
if err != nil {
t.Fatal(err)
}
// 缺幂等键:写路由一律要求。
missing := httptest.NewRecorder()
handler.ServeHTTP(missing, httptest.NewRequest(http.MethodPost, "/api/v1/review-candidates/batch-decision", strings.NewReader(`{"publication_ids":["tp-a"],"decision":"approved","decided_visibility":"internal","decision_code":"approved_for_release"}`)))
if missing.Code != http.StatusBadRequest {
t.Fatalf("missing idempotency key status=%d", missing.Code)
}
// 空选择:直接拒绝。
empty := httptest.NewRecorder()
emptyRequest := httptest.NewRequest(http.MethodPost, "/api/v1/review-candidates/batch-decision", strings.NewReader(`{"publication_ids":[],"decision":"approved","decided_visibility":"internal","decision_code":"approved_for_release"}`))
emptyRequest.Header.Set("Idempotency-Key", "op-batch-1")
handler.ServeHTTP(empty, emptyRequest)
if empty.Code != http.StatusBadRequest {
t.Fatalf("empty selection status=%d", empty.Code)
}
// 正常:逐条结果原样回报,操作人由服务端盖章。
ok := httptest.NewRecorder()
okRequest := httptest.NewRequest(http.MethodPost, "/api/v1/review-candidates/batch-decision", strings.NewReader(`{"publication_ids":["tp-a","tp-b","tp-c"],"decision":"approved","decided_visibility":"internal","decision_code":"approved_for_release"}`))
okRequest.Header.Set("Idempotency-Key", "op-batch-2")
handler.ServeHTTP(ok, okRequest)
if ok.Code != http.StatusOK {
t.Fatalf("batch review status=%d body=%s", ok.Code, ok.Body.String())
}
var body struct {
Data struct {
Selected int `json:"selected"`
Decided int `json:"decided"`
Skipped int `json:"skipped"`
SkippedIDs []string `json:"skipped_ids"`
} `json:"data"`
}
if err := json.Unmarshal(ok.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if body.Data.Decided != 2 || body.Data.Skipped != 1 || len(body.Data.SkippedIDs) != 1 || body.Data.SkippedIDs[0] != "tp-c" {
t.Fatalf("batch result=%#v", body.Data)
}
if store.captured.ReviewerRef == "" {
t.Fatal("reviewer must be stamped by the server")
}
// 错误映射。
for _, testCase := range []struct {
name string
err error
status int
}{
{"invalid", postgres.ErrInvalidPublicationReview, http.StatusBadRequest},
{"conflict", postgres.ErrOperationRequestConflict, http.StatusConflict},
{"unexpected", errors.New("boom"), http.StatusInternalServerError},
} {
failing := &reviewBatchFakeStore{err: testCase.err}
failingHandler, err := NewHandler(Config{Store: failing})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
request := httptest.NewRequest(http.MethodPost, "/api/v1/review-candidates/batch-decision", strings.NewReader(`{"publication_ids":["tp-a"],"decision":"approved","decided_visibility":"internal","decision_code":"approved_for_release"}`))
request.Header.Set("Idempotency-Key", "op-batch-3")
failingHandler.ServeHTTP(recorder, request)
if recorder.Code != testCase.status {
t.Fatalf("%s status=%d want=%d", testCase.name, recorder.Code, testCase.status)
}
}
// 未接线:诚实 501。
unwired, err := NewHandler(Config{Store: storeWithoutReviewBatch{Store: store}})
if err != nil {
t.Fatal(err)
}
unwiredRecorder := httptest.NewRecorder()
unwiredRequest := httptest.NewRequest(http.MethodPost, "/api/v1/review-candidates/batch-decision", strings.NewReader(`{"publication_ids":["tp-a"],"decision":"approved","decided_visibility":"internal","decision_code":"approved_for_release"}`))
unwiredRequest.Header.Set("Idempotency-Key", "op-batch-4")
unwired.ServeHTTP(unwiredRecorder, unwiredRequest)
if unwiredRecorder.Code != http.StatusNotImplemented {
t.Fatalf("unwired status=%d", unwiredRecorder.Code)
}
}
type storeWithoutReviewBatch struct{ Store }
+57 -1
View File
@@ -375,6 +375,9 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("GET /api/v1/review-candidates", func(w http.ResponseWriter, r *http.Request) {
listReviewCandidates(w, r, config.Store)
})
mux.HandleFunc("POST /api/v1/review-candidates/batch-decision", func(w http.ResponseWriter, r *http.Request) {
batchReviewCandidates(w, r, config)
})
mux.HandleFunc("POST /api/v1/review-candidates/{id}/decision", func(w http.ResponseWriter, r *http.Request) {
reviewPublication(w, r, config)
})
@@ -1504,6 +1507,56 @@ type createReleaseBatchRequest struct {
PublicationIDs []string `json:"publication_ids"`
DeletePublicationIDs []string `json:"delete_publication_ids"`
ReleaseChangeSetID string `json:"release_change_set_id"`
// Seal:建批后立即锁定(P0-3,清单已不可变,草稿态只是多按一次)。
Seal bool `json:"seal"`
}
// reviewBatchStore 是本路由需要的最小能力面:真实 Store 实现它,既有 fake 不必跟着改。
type reviewBatchStore interface {
ReviewPublicationsBatchWithOperation(context.Context, governance.ReviewPublicationsBatchRequest, string) (governance.ReviewPublicationsBatchResult, bool, error)
}
// batchReviewCandidates:一次对多条候选做同一个审核决定(批量批准/驳回)。
// 服务端逐条复用单条审核写入(同一套合法性校验),并逐条回报"哪几条没批成"。
func batchReviewCandidates(w http.ResponseWriter, r *http.Request, config Config) {
operationID, ok := requireOperationID(w, r)
if !ok {
return
}
var input struct {
PublicationIDs []string `json:"publication_ids"`
Decision string `json:"decision"`
DecidedVisibility string `json:"decided_visibility"`
DecisionCode string `json:"decision_code"`
}
if err := decodeStrictJSON(r, &input); err != nil || len(input.PublicationIDs) == 0 || len(input.PublicationIDs) > 100 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid review batch request"})
return
}
batcher, ok := config.Store.(reviewBatchStore)
if !ok {
writeJSON(w, http.StatusNotImplemented, map[string]string{"error": "review batch is not wired"})
return
}
result, replayed, err := batcher.ReviewPublicationsBatchWithOperation(r.Context(), governance.ReviewPublicationsBatchRequest{
PublicationIDs: input.PublicationIDs, Decision: governance.ReviewState(input.Decision),
DecidedVisibility: governance.Visibility(input.DecidedVisibility), DecisionCode: input.DecisionCode,
ReviewerRef: actorFor(r), Now: config.Now().UTC(),
}, operationID)
switch {
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
writeJSON(w, http.StatusConflict, map[string]string{"error": "Idempotency-Key conflicts with a different request"})
case errors.Is(err, postgresstore.ErrInvalidPublicationReview):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid review batch request"})
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not record review batch"})
default:
operation := "decided"
if replayed {
operation = "replayed"
}
writeJSON(w, http.StatusOK, map[string]any{"data": result, "operation": operation})
}
}
func createReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
@@ -1517,7 +1570,7 @@ func createReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
return
}
batch, replayed, err := config.Store.CreateReleaseBatchWithOperation(r.Context(), governance.CreateReleaseBatchRequest{
Name: input.Name, CreatedBy: actorFor(r), PublicationIDs: input.PublicationIDs, DeletePublicationIDs: input.DeletePublicationIDs, ReleaseChangeSetID: input.ReleaseChangeSetID, Now: config.Now().UTC(),
Name: input.Name, CreatedBy: actorFor(r), PublicationIDs: input.PublicationIDs, DeletePublicationIDs: input.DeletePublicationIDs, ReleaseChangeSetID: input.ReleaseChangeSetID, SealImmediately: input.Seal, Now: config.Now().UTC(),
}, operationID)
switch {
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
@@ -1531,6 +1584,9 @@ func createReleaseBatch(w http.ResponseWriter, r *http.Request, config Config) {
case errors.Is(err, postgresstore.ErrReleaseChangeSetEmpty):
// 建批时冻结依赖当前实时计划;计划为空就没得冻。
writeJSON(w, http.StatusUnprocessableEntity, map[string]string{"error": "current release plan has no releasable item"})
case errors.Is(err, postgresstore.ErrReleaseBatchGateConflict):
// 建批即锁定时逐项复核门禁失败:整批已回滚,不会留下半成品。
writeJSON(w, http.StatusUnprocessableEntity, map[string]string{"error": "release batch contains ineligible publications"})
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not create release batch"})
default: