internal/workflow:統一型別宣告,修復 #12 合併後 main 編譯失敗(#21)

#12 的 workflow.go 與 #13 的 types.go 各自宣告 User/Comment/Issue/
PullRequest/Repository,文字合併無衝突但語意衝突,main 自 a8c1f3e 起
go build 失敗。

統一採 #13 的字串模型(types.go 為唯一型別來源):
- Issue/Comment 補 State/Body/ClosedAt 欄位,新增 parseMoment
  (對應 gitea.py _parse_iso8601;保留原時區偏移,輸出與 Python
  isoformat 位元組一致)
- workflow.go 移除重複宣告,stalled/xref 邏輯改用該模型與 judge.go
  既有輔助(AssigneeNames/ReviewerNames/LastComment/containsString)
- Source 介面 number 改 int64(與 types.go 對齊),cmd/xcheck 同步
- workflow_test.go 測試資料改新型別模型(字串時刻、指標欄位)

驗證:
- go build/go vet/go test ./... 全綠(#6 與 #7 案例皆未回歸)
- xcheck 對真實 API 並行掃描,stalled 輸出與 gitea.py 位元組一致(含
  bear-cli#9 父追蹤項子項摘要、+08:00 時區偏移)
- xref 邊界案例(nope#9/PR#50 略過、bear-cli#9 內嵌解析)與 Python 一致
This commit is contained in:
ceo
2026-09-10 11:32:40 +08:00
parent 0b0c3cefcb
commit 4582dfaa4e
4 changed files with 147 additions and 218 deletions
+61 -159
View File
@@ -37,85 +37,11 @@ const (
// Go 的 regexp 不支援 lookbehind,改在掃描時檢查前一字元。
var xrefPattern = regexp.MustCompile(`([A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)?)?#(\d+)`)
// User 是 Gitea user 物件的精簡投影。
type User struct {
Username string `json:"username"`
}
// Comment 是 issue/PR 留言的精簡投影。
type Comment struct {
User User `json:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
}
// Author 回傳留言作者帳號;空字串表示無法判定。
func (c Comment) Author() string { return c.User.Username }
// Issue 是 issue 的精簡投影(Gitea /repos/{owner}/{repo}/issues,不含 PR)。
type Issue struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
Body string `json:"body"`
Author User `json:"user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt *time.Time `json:"closed_at,omitempty"`
// URL 對應 API 的 html_url。
URL string `json:"html_url"`
Assignees []User `json:"assignees"`
Repository Reference `json:"repository"`
// PullRequest 非 nil 表示這其實是 PR(Gitea 的 /issues API 兩者都回)。
PullRequest any `json:"pull_request,omitempty"`
}
// Reference 是 repository 物件的精簡投影(issue 物件帶 full_name)。
type Reference struct {
FullName string `json:"full_name"`
}
// Ref 是 PR base/head 的精簡投影(PR 物件沒有 repository 欄位,改讀 base/head.repo)。
type Ref struct {
Repo Reference `json:"repo"`
}
// PullRequest 是 /repos/{owner}/{repo}/pulls 物件的精簡投影。
type PullRequest struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
Author User `json:"user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt *time.Time `json:"closed_at,omitempty"`
// URL 對應 API 的 html_url。
URL string `json:"html_url"`
Base Ref `json:"base"`
Head Ref `json:"head"`
RequestedReviewers []User `json:"requested_reviewers"`
}
// FullName 回傳 "owner/repo";PR 沒有 repository 欄位,改讀 base(再退 head)。
func (p PullRequest) FullName() string {
if p.Base.Repo.FullName != "" {
return p.Base.Repo.FullName
}
return p.Head.Repo.FullName
}
// Repository 是 /orgs/{org}/repos 物件的精簡投影。
type Repository struct {
Name string `json:"name"`
Empty bool `json:"empty"`
HasPullRequests *bool `json:"has_pull_requests"`
}
// Item 是 stalled 掃描的單筆輸出(欄位對應 gitea.py 的 find_stalled_work)。
type Item struct {
Type string
Repo string
Number int
Number int64
Title string
URL string
Assignees []string
@@ -195,7 +121,7 @@ func writeJSON(b *strings.Builder, v any) {
// Child 是父追蹤項的子項活動摘要。
type Child struct {
Repo string `json:"repo"`
Number int `json:"number"`
Number int64 `json:"number"`
Title string `json:"title"`
State string `json:"state"`
// LastActivityAt 用指標:子項無可解析時刻時 Python 輸出 null(不是 "")。
@@ -206,7 +132,7 @@ type Child struct {
type XrefTarget struct {
Owner string
Repo string
Number int
Number int64
}
// Source 是 stalled 掃描需要的資料源(CLI 層以 API 實作;測試注入假資料)。
@@ -214,15 +140,15 @@ type Source interface {
// MyOrganizations 回傳目前帳號所屬組織清單。
MyOrganizations() ([]string, error)
// OrganizationRepositories 回傳組織的倉庫清單。
OrganizationRepositories(org string) ([]Repository, error)
OrganizationRepositories(org string) ([]Repo, error)
// OpenIssues 回傳倉庫的 open issues(Gitea 會混入 PR,由掃描端過濾)。
OpenIssues(owner, repo string) ([]Issue, error)
// OpenPulls 回傳倉庫的 open pull requests。
OpenPulls(owner, repo string) ([]PullRequest, error)
// IssueComments 回傳 issue/PR 的留言。
IssueComments(owner, repo string, number int) ([]Comment, error)
IssueComments(owner, repo string, number int64) ([]Comment, error)
// TryGetIssue 寬容版單一 issue 查詢:失敗(含 404)回 (零值, false, nil)。
TryGetIssue(owner, repo string, number int) (Issue, bool, error)
TryGetIssue(owner, repo string, number int64) (Issue, bool, error)
}
// iterXrefs 產出文字中的 (prefix, number) 配對(對應 _iter_issue_xrefs)。
@@ -278,7 +204,7 @@ func ResolveXrefTargets(org, repo string, texts []string, myOrgs map[string]bool
for _, text := range texts {
for _, pair := range iterXrefs(text) {
prefix, numberStr := pair[0], pair[1]
var number int
var number int64
if _, err := fmt.Sscanf(numberStr, "%d", &number); err != nil || number <= 0 {
continue
}
@@ -334,23 +260,25 @@ func childActivity(src Source, org, repo string, parent Issue, comments []Commen
if gerr != nil || !ok {
continue
}
if t.Owner == org && t.Repo == repo && child.Number == parent.Number {
if t.Owner == org && t.Repo == repo && child.issueNumber() == parent.issueNumber() {
continue // 自我引用
}
if !parent.CreatedAt.IsZero() && !child.CreatedAt.IsZero() && !child.CreatedAt.After(parent.CreatedAt) {
parentCreated, pcOK := parseMoment(parent.CreatedAt)
childCreated, ccOK := parseMoment(child.CreatedAt)
if pcOK && ccOK && !childCreated.After(parentCreated) {
continue // 上行引用(父項回指更早或同時的 issue);時間未知則不排除
}
var ms []time.Time
if !child.UpdatedAt.IsZero() {
ms = append(ms, child.UpdatedAt)
if m, ok := parseMoment(child.UpdatedAt); ok {
ms = append(ms, m)
}
if child.ClosedAt != nil && !child.ClosedAt.IsZero() {
ms = append(ms, *child.ClosedAt)
if m, ok := parseMoment(child.ClosedAt); ok {
ms = append(ms, m)
}
if cs, cerr := src.IssueComments(t.Owner, t.Repo, t.Number); cerr == nil {
for _, c := range cs {
if !c.CreatedAt.IsZero() {
ms = append(ms, c.CreatedAt)
if m, ok := parseMoment(c.CreatedAt); ok {
ms = append(ms, m)
}
}
}
@@ -384,20 +312,6 @@ func maxTime(ts []time.Time) time.Time {
return best
}
func usernames(users []User) []string {
var out []string
seen := map[string]bool{}
for _, u := range users {
if u.Username == "" || seen[u.Username] {
continue
}
seen[u.Username] = true
out = append(out, u.Username)
}
sort.Strings(out)
return out
}
// ScanStalled 掃描跨組織的停滯 open issues/PR(對應 find_stalled_work)。
//
// 判定:最後活動(留言時間與本身 updated_at 取較晚者,父追蹤項再加計
@@ -483,15 +397,20 @@ func ScanStalled(src Source, username string, threshold float64, now time.Time)
// considerIssue 對單一 issue 套用停滯判定(對應 _consider 的 issue 分支)。
func considerIssue(src Source, org, repo string, issue Issue, comments []Comment, username string, threshold float64, now time.Time, myOrgs map[string]bool, orgRepos func(string) ([]string, error)) (Item, bool) {
childMoments, children := childActivity(src, org, repo, issue, comments, myOrgs, orgRepos)
assignees := usernames(issue.Assignees)
moments := []time.Time{issue.UpdatedAt, issue.CreatedAt}
assignees := AssigneeNames(&issue)
var moments []time.Time
if m, ok := parseMoment(issue.UpdatedAt); ok {
moments = append(moments, m)
}
if m, ok := parseMoment(issue.CreatedAt); ok {
moments = append(moments, m)
}
for _, c := range comments {
if !c.CreatedAt.IsZero() {
moments = append(moments, c.CreatedAt)
if m, ok := parseMoment(c.CreatedAt); ok {
moments = append(moments, m)
}
}
moments = append(moments, childMoments...)
moments = nonZero(moments)
if len(moments) == 0 {
return Item{}, false
}
@@ -500,16 +419,21 @@ func considerIssue(src Source, org, repo string, issue Issue, comments []Comment
if stalled < threshold {
return Item{}, false
}
lastCommentAt, lastCommentBy := lastComment(comments)
if lastCommentBy == username && !lastCommentAt.IsZero() &&
now.Sub(lastCommentAt).Hours() < NudgeCooldownHours {
return Item{}, false
last := LastComment(comments)
lastCommentBy := ""
if last != nil {
lastCommentBy = last.Author()
}
if lastCommentBy == username {
if m, ok := parseMoment(last.CreatedAt); ok && now.Sub(m).Hours() < NudgeCooldownHours {
return Item{}, false
}
}
reason := "waiting-outside"
switch {
case len(assignees) == 0:
reason = "no-assignee"
case lastCommentBy != "" && containsStr(assignees, lastCommentBy):
case lastCommentBy != "" && containsString(assignees, lastCommentBy):
reason = "assignee-idle"
case lastCommentBy == "":
reason = "no-commenter"
@@ -518,8 +442,8 @@ func considerIssue(src Source, org, repo string, issue Issue, comments []Comment
reason = "parent-tracking"
}
item := Item{
Type: "issue", Repo: issue.Repository.FullName, Number: issue.Number,
Title: issue.Title, URL: issue.URL,
Type: "issue", Repo: issue.FullName(), Number: issue.issueNumber(),
Title: issue.Title, URL: issue.WebURL(),
Assignees: assignees,
LastCommentBy: lastCommentBy, LastActivityAt: lastActivity,
StalledHours: round1(stalled), Reason: reason,
@@ -532,15 +456,20 @@ func considerIssue(src Source, org, repo string, issue Issue, comments []Comment
// considerPull 對單一 PR 套用停滯判定(對應 _consider 的 pull 分支)。
func considerPull(pr PullRequest, comments []Comment, username string, threshold float64, now time.Time) (Item, bool) {
reviewers := usernames(pr.RequestedReviewers)
author := pr.Author.Username
moments := []time.Time{pr.UpdatedAt, pr.CreatedAt}
reviewers := ReviewerNames(&pr)
author := pr.Author()
var moments []time.Time
if m, ok := parseMoment(pr.UpdatedAt); ok {
moments = append(moments, m)
}
if m, ok := parseMoment(pr.CreatedAt); ok {
moments = append(moments, m)
}
for _, c := range comments {
if !c.CreatedAt.IsZero() {
moments = append(moments, c.CreatedAt)
if m, ok := parseMoment(c.CreatedAt); ok {
moments = append(moments, m)
}
}
moments = nonZero(moments)
if len(moments) == 0 {
return Item{}, false
}
@@ -549,10 +478,15 @@ func considerPull(pr PullRequest, comments []Comment, username string, threshold
if stalled < threshold {
return Item{}, false
}
lastCommentAt, lastCommentBy := lastComment(comments)
if lastCommentBy == username && !lastCommentAt.IsZero() &&
now.Sub(lastCommentAt).Hours() < NudgeCooldownHours {
return Item{}, false
last := LastComment(comments)
lastCommentBy := ""
if last != nil {
lastCommentBy = last.Author()
}
if lastCommentBy == username {
if m, ok := parseMoment(last.CreatedAt); ok && now.Sub(m).Hours() < NudgeCooldownHours {
return Item{}, false
}
}
reason := "no-commenter"
switch {
@@ -560,12 +494,12 @@ func considerPull(pr PullRequest, comments []Comment, username string, threshold
reason = "no-reviewer"
case lastCommentBy == author:
reason = "author-idle"
case lastCommentBy != "" && containsStr(reviewers, lastCommentBy):
case lastCommentBy != "" && containsString(reviewers, lastCommentBy):
reason = "reviewer-idle"
}
item := Item{
Type: "pull", Repo: pr.FullName(), Number: pr.Number,
Title: pr.Title, URL: pr.URL,
Title: pr.Title, URL: pr.WebURL(),
Author: author, Reviewers: reviewers,
LastCommentBy: lastCommentBy, LastActivityAt: lastActivity,
StalledHours: round1(stalled), Reason: reason,
@@ -573,19 +507,6 @@ func considerPull(pr PullRequest, comments []Comment, username string, threshold
return item, true
}
// lastComment 回傳最後一則留言(created_at 最新者)的時間與作者。
func lastComment(comments []Comment) (time.Time, string) {
var best time.Time
by := ""
for _, c := range comments {
if c.CreatedAt.After(best) {
best = c.CreatedAt
by = c.Author()
}
}
return best, by
}
func anyOpen(children []Child) bool {
for _, c := range children {
if c.State == "open" {
@@ -595,25 +516,6 @@ func anyOpen(children []Child) bool {
return false
}
func containsStr(list []string, s string) bool {
for _, x := range list {
if x == s {
return true
}
}
return false
}
func nonZero(ts []time.Time) []time.Time {
out := make([]time.Time, 0, len(ts))
for _, t := range ts {
if !t.IsZero() {
out = append(out, t)
}
}
return out
}
// round1 舍入到小數一位(對應 Python round(x, 1))。
// 經 strconv 的十進位正確舍入(半偶數),與 Python 的浮點 round 行為一致,
// 避免 (x*10+0.5) 截斷在 .x5 邊界產生偏差。