feat(release): 发布到生产一键完成(建批+锁定+授权合成一个动作)

用户的反馈是对的:上一轮只砍掉了"冻结/锁定"两个按钮,但一条文档从"已批准"到"发出"实际仍要
~9 次交互(找条目→勾选→填名→建批→滚到清单→找批次→授权→输语句→提交),因为"批次仪式"本身
还留在路径上。

- 新端点 POST /api/v1/release-batches/publish-now:在同一操作里先建批(SealImmediately,冻结
  即锁定)再授权生产发布,返回 {batch, execution}。
- 不放松任何约束:请求门禁(403)、备份恢复验证门禁(412)、管理员身份(401)、固定确认语句(400)、
  发布前快照硬门禁(409)、建批门禁(422) 全部沿用既有实现,无旁路;名称可省(服务端固定回退名,
  保证重放指纹一致)。
- 界面:审核行上直接「发布到生产」(管理员 + 门禁开启时显示),工具栏支持选中多条一次发布;
  复用既有确认弹窗(固定语句)。批次名不再是必经步骤。
- 实测(面板):点行级按钮 → 输 `publish-to-production` → 提交 → 批次 rb-bee80550…(sealed)
  + 执行 rx-3d831129…(completed, 项级 verified) → 页面回滚 rx-43835fac…(rolled_back, absent)。

测试:publish_now_test.go(四道关卡 + 组合调用 + 名称回退)与 reviewRelease.test.ts(请求体/回传/错误原因);
Go 26 包、前端 183 条全过。
This commit is contained in:
姚章浩
2026-10-10 15:48:30 +08:00
parent 70131c6161
commit f8faed0b21
8 changed files with 378 additions and 14 deletions
+101
View File
@@ -0,0 +1,101 @@
package httpapi
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"knowledge-governance/internal/governance"
)
// publish-now 把「建批(冻结+锁定)」与「授权生产发布」合成一个动作,
// 但四个关卡一个都不放松:请求门禁、备份恢复验证、管理员身份、固定确认语句。
func TestPublishNowComposesSealAndAuthorization(t *testing.T) {
request := func(body string, identity *sessionIdentity) *http.Request {
r := httptest.NewRequest(http.MethodPost, "/api/v1/release-batches/publish-now", bytes.NewBufferString(body))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Idempotency-Key", "op-publish-now")
if identity != nil {
r = r.WithContext(context.WithValue(r.Context(), sessionKey{}, *identity))
}
return r
}
admin := &sessionIdentity{Username: "yaozhanghao", Role: roleAdmin}
engineer := &sessionIdentity{Username: "engineer-1", Role: roleEngineer}
valid := `{"publication_ids":["tp-a"],"confirmation":"publish-to-production"}`
// 请求门禁关闭。
closedHandler, err := NewHandler(Config{Store: &fakeJobStore{}, Now: func() time.Time { return time.Now().UTC() },
ProductionReleaseRequestsEnabled: false, ProductionBackupRestoreVerified: true,
ProductionReleaseAuthorizerRef: "release-owner", ProductionReleaseAuthorizationToken: "safe-token"})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
closedHandler.ServeHTTP(recorder, request(valid, admin))
if recorder.Code != http.StatusForbidden {
t.Fatalf("gate off status=%d", recorder.Code)
}
// 备份恢复验证未过。
unverified, err := NewHandler(Config{Store: &fakeJobStore{}, Now: func() time.Time { return time.Now().UTC() },
ProductionReleaseRequestsEnabled: true, ProductionBackupRestoreVerified: false,
ProductionReleaseAuthorizerRef: "release-owner", ProductionReleaseAuthorizationToken: "safe-token"})
if err != nil {
t.Fatal(err)
}
recorder = httptest.NewRecorder()
unverified.ServeHTTP(recorder, request(valid, admin))
if recorder.Code != http.StatusPreconditionFailed {
t.Fatalf("backup unverified status=%d", recorder.Code)
}
// 非管理员 / 错语句 / 空选择。
handler, store := productionActionHandler(t)
store.createBatchResult = governance.ReleaseBatch{ID: "rb-now", Name: "立即发布", State: governance.ReleaseBatchSealed, ItemCount: 1, ReleaseChangeSetID: "rcs-now"}
for _, testCase := range []struct {
name string
body string
identity *sessionIdentity
status int
}{
{"engineer session", valid, engineer, http.StatusUnauthorized},
{"no session", valid, nil, http.StatusUnauthorized},
{"wrong phrase", `{"publication_ids":["tp-a"],"confirmation":"publish"}`, admin, http.StatusBadRequest},
{"empty selection", `{"publication_ids":[],"confirmation":"publish-to-production"}`, admin, http.StatusBadRequest},
} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request(testCase.body, testCase.identity))
if recorder.Code != testCase.status {
t.Fatalf("%s status=%d want=%d", testCase.name, recorder.Code, testCase.status)
}
}
// 正常路径:建批就是"立即锁定",随后完成授权,两步在同一个操作里。
store.createBatchRequest = governance.CreateReleaseBatchRequest{}
store.authorizeReleaseCalled = false
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, request(`{"publication_ids":["tp-a","tp-b"],"name":"立即发布 · 测试","confirmation":"publish-to-production"}`, admin))
if recorder.Code != http.StatusAccepted {
t.Fatalf("publish-now status=%d body=%s", recorder.Code, recorder.Body.String())
}
if !store.createBatchRequest.SealImmediately {
t.Fatal("publish-now must create the batch already sealed")
}
if store.createBatchRequest.Name != "立即发布 · 测试" || len(store.createBatchRequest.PublicationIDs) != 2 {
t.Fatalf("batch request = %#v", store.createBatchRequest)
}
if !store.authorizeReleaseCalled || store.authorizeReleaseRequest.BatchID != "rb-now" {
t.Fatalf("authorize was not composed: called=%v request=%#v", store.authorizeReleaseCalled, store.authorizeReleaseRequest)
}
if store.authorizeReleaseRequest.AuthorizationCode != "production_release_authorized" {
t.Fatalf("authorization code = %q", store.authorizeReleaseRequest.AuthorizationCode)
}
if !strings.Contains(recorder.Body.String(), "rx-safe") {
t.Fatalf("response must carry the execution: %s", recorder.Body.String())
}
}
+97
View File
@@ -428,6 +428,11 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("POST /api/v1/release-batches/{id}/seal", func(w http.ResponseWriter, r *http.Request) {
sealReleaseBatch(w, r, config)
})
// 一个动作把"建批(冻结+锁定)→ 授权生产发布"走完:门禁、固定确认语句、管理员身份、
// 发布前快照门禁全部沿用既有实现,不新增旁路。
mux.HandleFunc("POST /api/v1/release-batches/publish-now", func(w http.ResponseWriter, r *http.Request) {
publishNowPublications(w, r, config)
})
mux.HandleFunc("POST /api/v1/release-batches/{id}/authorize-production", func(w http.ResponseWriter, r *http.Request) {
authorizeProductionRelease(w, r, config)
})
@@ -1760,6 +1765,98 @@ type authorizeProductionReleaseRequest struct {
Confirmation string `json:"confirmation"`
}
type publishNowRequest struct {
PublicationIDs []string `json:"publication_ids"`
DeletePublicationIDs []string `json:"delete_publication_ids"`
Name string `json:"name"`
Confirmation string `json:"confirmation"`
}
// publishNowPublications:把「建批(冻结 + 锁定)」和「授权生产发布」合成一个动作。
//
// 它不放松任何约束——只是把操作员本来要按顺序点两次按钮、再来一次确认语句的流程,
// 变成一次带确认语句的提交:请求门禁、备份恢复验证门禁、管理员身份、固定确认语句、
// 发布前快照硬门禁、清单不可变全都照旧由既有两个入口把关。
func publishNowPublications(w http.ResponseWriter, r *http.Request, config Config) {
if !config.ProductionReleaseRequestsEnabled {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "production release requests are disabled"})
return
}
if !config.ProductionBackupRestoreVerified {
writeJSON(w, http.StatusPreconditionFailed, map[string]string{"error": "production backup restore verification is required"})
return
}
actor, releaseAuthorized := productionReleaseActor(r, config)
if !releaseAuthorized {
writeJSON(w, http.StatusUnauthorized, map[string]string{"error": "production release authorization required"})
return
}
var input publishNowRequest
if err := decodeStrictJSON(r, &input); err != nil || input.Confirmation != "publish-to-production" {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid production release authorization"})
return
}
if len(input.PublicationIDs) == 0 && len(input.DeletePublicationIDs) == 0 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "select at least one publication"})
return
}
seen := make(map[string]bool, len(input.PublicationIDs)+len(input.DeletePublicationIDs))
for _, publicationID := range append(append([]string{}, input.PublicationIDs...), input.DeletePublicationIDs...) {
if !validCatalogID(publicationID) || seen[publicationID] || len(seen) > 100 {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid publication_ids"})
return
}
seen[publicationID] = true
}
operationID, ok := requireOperationID(w, r)
if !ok {
return
}
name := strings.TrimSpace(input.Name)
if name == "" || len(name) > 200 || strings.ContainsAny(name, "\x00\r\n") {
// 固定回退名:同一请求重放时指纹一致,幂等不会被时间戳破坏。
name = "立即发布"
}
now := config.Now().UTC()
batch, _, err := config.Store.CreateReleaseBatchWithOperation(r.Context(), governance.CreateReleaseBatchRequest{
Name: name, CreatedBy: actor, PublicationIDs: input.PublicationIDs, DeletePublicationIDs: input.DeletePublicationIDs,
SealImmediately: true, Now: now,
}, operationID)
switch {
case errors.Is(err, postgresstore.ErrOperationRequestConflict):
writeJSON(w, http.StatusConflict, map[string]string{"error": "Idempotency-Key conflicts with a different request"})
return
case errors.Is(err, postgresstore.ErrInvalidReleaseBatch):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid release batch request"})
return
case errors.Is(err, postgresstore.ErrReleaseBatchNotEligible), errors.Is(err, postgresstore.ErrReleaseBatchGateConflict):
writeJSON(w, http.StatusUnprocessableEntity, map[string]string{"error": "release batch contains ineligible publications"})
return
case errors.Is(err, postgresstore.ErrReleaseChangeSetUnconfirmed), errors.Is(err, postgresstore.ErrReleaseChangeSetEmpty):
writeJSON(w, http.StatusUnprocessableEntity, map[string]string{"error": "release change set must be confirmed and cover every requested publication"})
return
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not create release batch"})
return
}
execution, _, err := config.Store.AuthorizeReleaseBatchWithOperation(r.Context(), governance.AuthorizeReleaseBatchRequest{
BatchID: batch.ID, AuthorizedBy: actor, AuthorizationCode: "production_release_authorized", Now: now,
}, operationID+":authorize")
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.ErrInvalidReleaseAuthorization):
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid production release authorization"})
case errors.Is(err, postgresstore.ErrReleaseAuthorizationGateConflict):
// 批次已锁定但不满足生产门禁(例如快照缺失):如实说明,批次留着不浪费。
writeJSON(w, http.StatusConflict, map[string]string{"error": "sealed batch no longer satisfies the production release gate"})
case err != nil:
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not authorize production release"})
default:
writeJSON(w, http.StatusAccepted, map[string]any{"data": map[string]any{"batch": batch, "execution": execution}})
}
}
func authorizeProductionRelease(w http.ResponseWriter, r *http.Request, config Config) {
if !config.ProductionReleaseRequestsEnabled {
writeJSON(w, http.StatusForbidden, map[string]string{"error": "production release requests are disabled"})