feat(version-history): 「版本记录」页 —— 追踪远端 main 全部提交(只读 + 管理员手动同步)
build / build (push) Successful in 57s
build / build (push) Successful in 57s
按已确认的契约实现(口径见 docs/VERSION_HISTORY.md):
- **版本号口径(只给有证据的)**:注释 tag + tag 指向该提交 + 该提交内 CHANGELOG 顶部三者一致 ⇒ 正式版本;
tag 之后的提交按 `<主>.<次>.<自该 tag 的提交数>` 显示开发号(与 scripts/version.py **同规则**);
证据不足 ⇒ 「历史提交 · 短SHA」+ 原因码,**不补造版本号**。完整 SHA 是唯一身份、版本号不是唯一键
(tag 之后两条并行分支各自的第 1 个提交都算 0.1.1 ⇒ 两条都保留,用短 SHA 区分)。
- **迁移 000046**:version_history_commits(提交记录,主键 repo_key+commit_sha)+ version_history_sync_runs
(同步状态)。快照 = 一次同步:抓完并与 X-Total-Count 对账通过才切换;失败不切换快照、不清空历史、
last_success_at 不动(页面显示"上次同步失败(原因)· 仍展示上次成功数据");「同步成功」≠「历史完整」。
- **只读客户端(匿名,不使用任何凭据)**:白名单 origin + 固定仓库、拒绝任意 URL 与跨主机重定向、
响应上限 5MB / 超时 15s、仅 5xx/429 退避重试(最多 3 次);逐页抓到短页为止;每次全量扫 tag
⇒ 旧提交后补的 tag 会更新既有记录(不新增第二行)。
- **接口**(三个都登记进 api/openapi.yaml):GET /api/v1/build-info(viewer)、
GET /api/v1/version-history(viewer;游标绑定快照,过期返回 409 cursor_expired;不返回伪造 total)、
POST /api/v1/version-history/sync(admin;并发同步复用同一作业,靠 IdempotencyKey)。
- **「当前运行」只认精确匹配**:产物注入的完整 SHA 命中某条记录才标该条;开发构建显式标注"开发构建";
没有可用标识 ⇒ "当前运行版本未知";**不拿最新 main / 最新 tag 顶替**,CI 成功不等于已部署。
- **前端**:一级菜单第 5 项(版本记录)+ 时间线页面(全部/正式/开发/历史四段筛选、正文展开、
加载更多、760px 单列无横向溢出、提交正文与变更记录按纯文本渲染);无打 tag / 发布 / 部署入口。
新增配置(留空即未接线,页面降级):VERSION_HISTORY_GITEA_BASE_URL / _REPO / _ALLOWED_ORIGINS /
_HTTP_TIMEOUT / _MAX_PAGES。
验证:`make verify` 15/15 success(tests-go 578 通过 / 0 失败;tests-web 209 通过 / 0 失败);
新增 Go 用例 30(版本口径与图遍历 12、客户端守卫 6、同步编排 5、接口 7);前端新增 18(组件 10 + 客户端 8);
真实仓库匿名快照拉取(`VERSION_HISTORY_LIVE_BASE_URL=… go test -run TestLiveSyncAgainstRealRepository`):
抓到 113 条与 X-Total-Count 一致、v0.1.0 是注释 tag 且目标与 `git rev-parse v0.1.0^{commit}` 一致、
开发号逐个等于 `git rev-list --count v0.1.0..<sha>`(正式 1 / 开发 22 / 历史 90)。
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
// Package versionhistory 把「远端 main 可达的提交」+「tag 证据」+「提交内 CHANGELOG」
|
||||
// 换算成版本记录页要的提交记录。
|
||||
//
|
||||
// 它**只读、纯计算**:不发网络请求、不写库、不执行任何历史提交里的脚本。版本号口径
|
||||
// 委托给 internal/governance.DecideVersion,提交数按提交图算(等价 `git rev-list --count <tag>..<sha>`),
|
||||
// 与 scripts/version.py 同规则 —— 两处算出的号必须一致。
|
||||
package versionhistory
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// RawCommit 是一条从 Gitea 读回的提交(只保留展示与判版本所需的字段)。
|
||||
type RawCommit struct {
|
||||
SHA string
|
||||
Parents []string
|
||||
CommittedAt time.Time
|
||||
AuthorDisplay string
|
||||
Subject string
|
||||
Body string
|
||||
}
|
||||
|
||||
// RawTag 是一个 tag 及其可核验属性;Annotated=false 表示轻量 tag(不算正式版本的证据)。
|
||||
type RawTag struct {
|
||||
Name string
|
||||
TagSHA string
|
||||
TargetSHA string
|
||||
Annotated bool
|
||||
}
|
||||
|
||||
// ChangelogSource 读「某个提交内的 CHANGELOG.md」;读不到返回错误(调用方按"无记录"处理)。
|
||||
type ChangelogSource func(sha string) (string, error)
|
||||
|
||||
// DeriveInput 是一次换算的全部输入。
|
||||
type DeriveInput struct {
|
||||
RepoKey string
|
||||
CommitURLBase string // 形如 "<origin>/<owner>/<repo>/commit/",由配置拼好,不接受外部传入
|
||||
Commits []RawCommit
|
||||
Tags []RawTag
|
||||
Changelog ChangelogSource
|
||||
SnapshotID int64
|
||||
}
|
||||
|
||||
// DeriveResult 是换算结果。
|
||||
type DeriveResult struct {
|
||||
Records []governance.VersionHistoryCommit
|
||||
GraphComplete bool
|
||||
MissingParents []string
|
||||
}
|
||||
|
||||
// DeriveVersions 逐提交判定版本号(含合并提交与侧分支提交;不展示任何未推送的本地提交,
|
||||
// 因为输入本身就是远端 main 可达集合)。
|
||||
func DeriveVersions(in DeriveInput) DeriveResult {
|
||||
bySHA := make(map[string]RawCommit, len(in.Commits))
|
||||
for _, commit := range in.Commits {
|
||||
bySHA[commit.SHA] = commit
|
||||
}
|
||||
missing := map[string]bool{}
|
||||
ancestors := make(map[string]map[string]struct{}, len(in.Commits))
|
||||
var walk func(sha string) map[string]struct{}
|
||||
walk = func(sha string) map[string]struct{} {
|
||||
if set, ok := ancestors[sha]; ok {
|
||||
return set
|
||||
}
|
||||
set := map[string]struct{}{sha: {}}
|
||||
ancestors[sha] = set // 先占位,DAG 不会有环
|
||||
commit, ok := bySHA[sha]
|
||||
if !ok {
|
||||
return set
|
||||
}
|
||||
for _, parent := range commit.Parents {
|
||||
if _, known := bySHA[parent]; !known {
|
||||
missing[parent] = true
|
||||
continue
|
||||
}
|
||||
for item := range walk(parent) {
|
||||
set[item] = struct{}{}
|
||||
}
|
||||
}
|
||||
return set
|
||||
}
|
||||
for _, commit := range in.Commits {
|
||||
walk(commit.SHA)
|
||||
}
|
||||
|
||||
tagsByTarget := map[string][]RawTag{}
|
||||
tagByName := map[string]RawTag{}
|
||||
for _, tag := range in.Tags {
|
||||
tagsByTarget[tag.TargetSHA] = append(tagsByTarget[tag.TargetSHA], tag)
|
||||
if _, exists := tagByName[tag.Name]; !exists {
|
||||
tagByName[tag.Name] = tag
|
||||
}
|
||||
}
|
||||
|
||||
changelogCache := map[string]string{}
|
||||
readChangelog := func(sha string) string {
|
||||
if text, ok := changelogCache[sha]; ok {
|
||||
return text
|
||||
}
|
||||
text := ""
|
||||
if in.Changelog != nil {
|
||||
if loaded, err := in.Changelog(sha); err == nil {
|
||||
text = loaded
|
||||
}
|
||||
}
|
||||
changelogCache[sha] = text
|
||||
return text
|
||||
}
|
||||
|
||||
graphComplete := len(missing) == 0
|
||||
result := DeriveResult{GraphComplete: graphComplete}
|
||||
for sha := range missing {
|
||||
result.MissingParents = append(result.MissingParents, sha)
|
||||
}
|
||||
sort.Strings(result.MissingParents)
|
||||
|
||||
records := make([]governance.VersionHistoryCommit, 0, len(in.Commits))
|
||||
for _, commit := range in.Commits {
|
||||
changelog := readChangelog(commit.SHA)
|
||||
top := governance.ChangelogTopVersion(changelog)
|
||||
|
||||
baseTagName := ""
|
||||
var sinceBase *int
|
||||
if governance.SemverValid(top) {
|
||||
candidate := "v" + top
|
||||
if base, exists := tagByName[candidate]; exists {
|
||||
if !graphComplete {
|
||||
// tag 存在但图不完整 ⇒ 不给号(宁可没号,也不报错号)
|
||||
baseTagName = candidate
|
||||
} else if baseSet, ok := ancestors[base.TargetSHA]; ok {
|
||||
if own, ok2 := ancestors[commit.SHA]; ok2 {
|
||||
count := 0
|
||||
for item := range own {
|
||||
if _, inBase := baseSet[item]; !inBase {
|
||||
count++
|
||||
}
|
||||
}
|
||||
baseTagName = candidate
|
||||
sinceBase = &count
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
evidenceTags := make([]governance.VersionTagEvidence, 0, len(tagsByTarget[commit.SHA]))
|
||||
for _, tag := range tagsByTarget[commit.SHA] {
|
||||
evidenceTags = append(evidenceTags, governance.VersionTagEvidence{
|
||||
Name: tag.Name, TagSHA: tag.TagSHA, Annotated: tag.Annotated})
|
||||
}
|
||||
sort.Slice(evidenceTags, func(i, j int) bool { return evidenceTags[i].Name < evidenceTags[j].Name })
|
||||
|
||||
label, state, kind, evidence := governance.DecideVersion(top, evidenceTags, baseTagName, sinceBase)
|
||||
if label == "" && !graphComplete {
|
||||
evidence.Reason = governance.ReasonIncompleteGraph
|
||||
}
|
||||
record := governance.VersionHistoryCommit{
|
||||
RepoKey: in.RepoKey,
|
||||
CommitSHA: commit.SHA,
|
||||
Parents: commit.Parents,
|
||||
CommittedAt: commit.CommittedAt,
|
||||
AuthorDisplay: commit.AuthorDisplay,
|
||||
Subject: commit.Subject,
|
||||
Body: commit.Body,
|
||||
VersionLabel: label,
|
||||
VersionState: state,
|
||||
RecordKind: kind,
|
||||
ReleaseTags: evidenceTags,
|
||||
CommitURL: in.CommitURLBase + commit.SHA,
|
||||
VersionEvidence: evidence,
|
||||
SnapshotID: in.SnapshotID,
|
||||
}
|
||||
if kind == governance.RecordKindRelease {
|
||||
record.ReleaseNotes = governance.ExtractReleaseNotes(changelog, label)
|
||||
}
|
||||
records = append(records, record)
|
||||
}
|
||||
|
||||
// 最新在上;同一提交时间用 SHA 兜底 ⇒ 排序确定,分页不重不漏。
|
||||
sort.SliceStable(records, func(i, j int) bool {
|
||||
if !records[i].CommittedAt.Equal(records[j].CommittedAt) {
|
||||
return records[i].CommittedAt.After(records[j].CommittedAt)
|
||||
}
|
||||
return records[i].CommitSHA > records[j].CommitSHA
|
||||
})
|
||||
result.Records = records
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user