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

Merged
chenyunda218 merged 1 commits from fix/issue21-workflow-types into main 2026-09-10 11:45:39 +08:00
4 changed files with 147 additions and 218 deletions
+4 -4
View File
@@ -132,8 +132,8 @@ func (s *apiSource) MyOrganizations() ([]string, error) {
return names, nil return names, nil
} }
func (s *apiSource) OrganizationRepositories(org string) ([]workflow.Repository, error) { func (s *apiSource) OrganizationRepositories(org string) ([]workflow.Repo, error) {
var repos []workflow.Repository var repos []workflow.Repo
if err := s.listAll("orgs/"+org+"/repos", nil, &repos); err != nil { if err := s.listAll("orgs/"+org+"/repos", nil, &repos); err != nil {
return nil, err return nil, err
} }
@@ -157,7 +157,7 @@ func (s *apiSource) OpenPulls(owner, repo string) ([]workflow.PullRequest, error
return pulls, nil return pulls, nil
} }
func (s *apiSource) IssueComments(owner, repo string, number int) ([]workflow.Comment, error) { func (s *apiSource) IssueComments(owner, repo string, number int64) ([]workflow.Comment, error) {
var comments []workflow.Comment var comments []workflow.Comment
if err := s.listAll(fmt.Sprintf("repos/%s/%s/issues/%d/comments", owner, repo, number), nil, &comments); err != nil { if err := s.listAll(fmt.Sprintf("repos/%s/%s/issues/%d/comments", owner, repo, number), nil, &comments); err != nil {
return nil, err return nil, err
@@ -165,7 +165,7 @@ func (s *apiSource) IssueComments(owner, repo string, number int) ([]workflow.Co
return comments, nil return comments, nil
} }
func (s *apiSource) TryGetIssue(owner, repo string, number int) (workflow.Issue, bool, error) { func (s *apiSource) TryGetIssue(owner, repo string, number int64) (workflow.Issue, bool, error) {
var issue workflow.Issue var issue workflow.Issue
body, code, err := s.get(fmt.Sprintf("repos/%s/%s/issues/%d", owner, repo, number), nil) body, code, err := s.get(fmt.Sprintf("repos/%s/%s/issues/%d", owner, repo, number), nil)
if err != nil { if err != nil {
+24
View File
@@ -3,6 +3,8 @@ package workflow
import ( import (
"encoding/json" "encoding/json"
"regexp" "regexp"
"strings"
"time"
) )
// User 是 Gitea API 的使用者物件(僅取工作流判斷會用到的欄位)。 // User 是 Gitea API 的使用者物件(僅取工作流判斷會用到的欄位)。
@@ -65,6 +67,7 @@ type PRRef struct {
// Comment 是 issue/PR 的討論留言(PR 的留言也走 issues comments 端點)。 // Comment 是 issue/PR 的討論留言(PR 的留言也走 issues comments 端點)。
type Comment struct { type Comment struct {
Body string `json:"body"`
CreatedAt string `json:"created_at"` CreatedAt string `json:"created_at"`
User *User `json:"user"` User *User `json:"user"`
} }
@@ -82,6 +85,9 @@ type Issue struct {
Number int64 `json:"number"` Number int64 `json:"number"`
Index int64 `json:"index"` Index int64 `json:"index"`
Title string `json:"title"` Title string `json:"title"`
State string `json:"state"`
Body string `json:"body"`
ClosedAt string `json:"closed_at"`
HTMLURL string `json:"html_url"` HTMLURL string `json:"html_url"`
URL string `json:"url"` URL string `json:"url"`
UpdatedAt string `json:"updated_at"` UpdatedAt string `json:"updated_at"`
@@ -216,3 +222,21 @@ func (p *PullRequest) FullName() string {
} }
return repoFullNameFromURL(p.WebURL()) return repoFullNameFromURL(p.WebURL())
} }
// parseMoment 對應 gitea.py 的 _parse_iso8601:剖析 ISO8601 字串(Z 後綴、
// 小數秒、時區偏移皆可;無時區視為 UTC),失敗回 (零值, false)。
// 保留原時區偏移、不轉 UTC——Python datetime.isoformat() 會原樣輸出
// 「+08:00」這類偏移,轉了 UTC 輸出就變 +00:00,與基準位元組不相容。
// stalled 掃描的時刻判斷(last_activity、催促冷卻)都以這裡的語義為準。
func parseMoment(value string) (time.Time, bool) {
text := strings.TrimSpace(value)
if text == "" {
return time.Time{}, false
}
for _, layout := range []string{time.RFC3339Nano, time.RFC3339, "2006-01-02T15:04:05", "2006-01-02"} {
if t, err := time.Parse(layout, text); err == nil {
return t, true
}
}
return time.Time{}, false
}
+61 -159
View File
@@ -37,85 +37,11 @@ const (
// Go 的 regexp 不支援 lookbehind,改在掃描時檢查前一字元。 // Go 的 regexp 不支援 lookbehind,改在掃描時檢查前一字元。
var xrefPattern = regexp.MustCompile(`([A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)?)?#(\d+)`) 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)。 // Item 是 stalled 掃描的單筆輸出(欄位對應 gitea.py 的 find_stalled_work)。
type Item struct { type Item struct {
Type string Type string
Repo string Repo string
Number int Number int64
Title string Title string
URL string URL string
Assignees []string Assignees []string
@@ -195,7 +121,7 @@ func writeJSON(b *strings.Builder, v any) {
// Child 是父追蹤項的子項活動摘要。 // Child 是父追蹤項的子項活動摘要。
type Child struct { type Child struct {
Repo string `json:"repo"` Repo string `json:"repo"`
Number int `json:"number"` Number int64 `json:"number"`
Title string `json:"title"` Title string `json:"title"`
State string `json:"state"` State string `json:"state"`
// LastActivityAt 用指標:子項無可解析時刻時 Python 輸出 null(不是 "")。 // LastActivityAt 用指標:子項無可解析時刻時 Python 輸出 null(不是 "")。
@@ -206,7 +132,7 @@ type Child struct {
type XrefTarget struct { type XrefTarget struct {
Owner string Owner string
Repo string Repo string
Number int Number int64
} }
// Source 是 stalled 掃描需要的資料源(CLI 層以 API 實作;測試注入假資料)。 // Source 是 stalled 掃描需要的資料源(CLI 層以 API 實作;測試注入假資料)。
@@ -214,15 +140,15 @@ type Source interface {
// MyOrganizations 回傳目前帳號所屬組織清單。 // MyOrganizations 回傳目前帳號所屬組織清單。
MyOrganizations() ([]string, error) MyOrganizations() ([]string, error)
// OrganizationRepositories 回傳組織的倉庫清單。 // OrganizationRepositories 回傳組織的倉庫清單。
OrganizationRepositories(org string) ([]Repository, error) OrganizationRepositories(org string) ([]Repo, error)
// OpenIssues 回傳倉庫的 open issues(Gitea 會混入 PR,由掃描端過濾)。 // OpenIssues 回傳倉庫的 open issues(Gitea 會混入 PR,由掃描端過濾)。
OpenIssues(owner, repo string) ([]Issue, error) OpenIssues(owner, repo string) ([]Issue, error)
// OpenPulls 回傳倉庫的 open pull requests。 // OpenPulls 回傳倉庫的 open pull requests。
OpenPulls(owner, repo string) ([]PullRequest, error) OpenPulls(owner, repo string) ([]PullRequest, error)
// IssueComments 回傳 issue/PR 的留言。 // 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 寬容版單一 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)。 // 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 _, text := range texts {
for _, pair := range iterXrefs(text) { for _, pair := range iterXrefs(text) {
prefix, numberStr := pair[0], pair[1] prefix, numberStr := pair[0], pair[1]
var number int var number int64
if _, err := fmt.Sscanf(numberStr, "%d", &number); err != nil || number <= 0 { if _, err := fmt.Sscanf(numberStr, "%d", &number); err != nil || number <= 0 {
continue continue
} }
@@ -334,23 +260,25 @@ func childActivity(src Source, org, repo string, parent Issue, comments []Commen
if gerr != nil || !ok { if gerr != nil || !ok {
continue continue
} }
if t.Owner == org && t.Repo == repo && child.Number == parent.Number { if t.Owner == org && t.Repo == repo && child.issueNumber() == parent.issueNumber() {
continue // 自我引用 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);時間未知則不排除 continue // 上行引用(父項回指更早或同時的 issue);時間未知則不排除
} }
var ms []time.Time var ms []time.Time
if !child.UpdatedAt.IsZero() { if m, ok := parseMoment(child.UpdatedAt); ok {
ms = append(ms, child.UpdatedAt) ms = append(ms, m)
} }
if child.ClosedAt != nil && !child.ClosedAt.IsZero() { if m, ok := parseMoment(child.ClosedAt); ok {
ms = append(ms, *child.ClosedAt) ms = append(ms, m)
} }
if cs, cerr := src.IssueComments(t.Owner, t.Repo, t.Number); cerr == nil { if cs, cerr := src.IssueComments(t.Owner, t.Repo, t.Number); cerr == nil {
for _, c := range cs { for _, c := range cs {
if !c.CreatedAt.IsZero() { if m, ok := parseMoment(c.CreatedAt); ok {
ms = append(ms, c.CreatedAt) ms = append(ms, m)
} }
} }
} }
@@ -384,20 +312,6 @@ func maxTime(ts []time.Time) time.Time {
return best 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)。 // ScanStalled 掃描跨組織的停滯 open issues/PR(對應 find_stalled_work)。
// //
// 判定:最後活動(留言時間與本身 updated_at 取較晚者,父追蹤項再加計 // 判定:最後活動(留言時間與本身 updated_at 取較晚者,父追蹤項再加計
@@ -483,15 +397,20 @@ func ScanStalled(src Source, username string, threshold float64, now time.Time)
// considerIssue 對單一 issue 套用停滯判定(對應 _consider 的 issue 分支)。 // 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) { 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) childMoments, children := childActivity(src, org, repo, issue, comments, myOrgs, orgRepos)
assignees := usernames(issue.Assignees) assignees := AssigneeNames(&issue)
moments := []time.Time{issue.UpdatedAt, issue.CreatedAt} 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 { for _, c := range comments {
if !c.CreatedAt.IsZero() { if m, ok := parseMoment(c.CreatedAt); ok {
moments = append(moments, c.CreatedAt) moments = append(moments, m)
} }
} }
moments = append(moments, childMoments...) moments = append(moments, childMoments...)
moments = nonZero(moments)
if len(moments) == 0 { if len(moments) == 0 {
return Item{}, false return Item{}, false
} }
@@ -500,16 +419,21 @@ func considerIssue(src Source, org, repo string, issue Issue, comments []Comment
if stalled < threshold { if stalled < threshold {
return Item{}, false return Item{}, false
} }
lastCommentAt, lastCommentBy := lastComment(comments) last := LastComment(comments)
if lastCommentBy == username && !lastCommentAt.IsZero() && lastCommentBy := ""
now.Sub(lastCommentAt).Hours() < NudgeCooldownHours { if last != nil {
return Item{}, false lastCommentBy = last.Author()
}
if lastCommentBy == username {
if m, ok := parseMoment(last.CreatedAt); ok && now.Sub(m).Hours() < NudgeCooldownHours {
return Item{}, false
}
} }
reason := "waiting-outside" reason := "waiting-outside"
switch { switch {
case len(assignees) == 0: case len(assignees) == 0:
reason = "no-assignee" reason = "no-assignee"
case lastCommentBy != "" && containsStr(assignees, lastCommentBy): case lastCommentBy != "" && containsString(assignees, lastCommentBy):
reason = "assignee-idle" reason = "assignee-idle"
case lastCommentBy == "": case lastCommentBy == "":
reason = "no-commenter" reason = "no-commenter"
@@ -518,8 +442,8 @@ func considerIssue(src Source, org, repo string, issue Issue, comments []Comment
reason = "parent-tracking" reason = "parent-tracking"
} }
item := Item{ item := Item{
Type: "issue", Repo: issue.Repository.FullName, Number: issue.Number, Type: "issue", Repo: issue.FullName(), Number: issue.issueNumber(),
Title: issue.Title, URL: issue.URL, Title: issue.Title, URL: issue.WebURL(),
Assignees: assignees, Assignees: assignees,
LastCommentBy: lastCommentBy, LastActivityAt: lastActivity, LastCommentBy: lastCommentBy, LastActivityAt: lastActivity,
StalledHours: round1(stalled), Reason: reason, StalledHours: round1(stalled), Reason: reason,
@@ -532,15 +456,20 @@ func considerIssue(src Source, org, repo string, issue Issue, comments []Comment
// considerPull 對單一 PR 套用停滯判定(對應 _consider 的 pull 分支)。 // considerPull 對單一 PR 套用停滯判定(對應 _consider 的 pull 分支)。
func considerPull(pr PullRequest, comments []Comment, username string, threshold float64, now time.Time) (Item, bool) { func considerPull(pr PullRequest, comments []Comment, username string, threshold float64, now time.Time) (Item, bool) {
reviewers := usernames(pr.RequestedReviewers) reviewers := ReviewerNames(&pr)
author := pr.Author.Username author := pr.Author()
moments := []time.Time{pr.UpdatedAt, pr.CreatedAt} 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 { for _, c := range comments {
if !c.CreatedAt.IsZero() { if m, ok := parseMoment(c.CreatedAt); ok {
moments = append(moments, c.CreatedAt) moments = append(moments, m)
} }
} }
moments = nonZero(moments)
if len(moments) == 0 { if len(moments) == 0 {
return Item{}, false return Item{}, false
} }
@@ -549,10 +478,15 @@ func considerPull(pr PullRequest, comments []Comment, username string, threshold
if stalled < threshold { if stalled < threshold {
return Item{}, false return Item{}, false
} }
lastCommentAt, lastCommentBy := lastComment(comments) last := LastComment(comments)
if lastCommentBy == username && !lastCommentAt.IsZero() && lastCommentBy := ""
now.Sub(lastCommentAt).Hours() < NudgeCooldownHours { if last != nil {
return Item{}, false lastCommentBy = last.Author()
}
if lastCommentBy == username {
if m, ok := parseMoment(last.CreatedAt); ok && now.Sub(m).Hours() < NudgeCooldownHours {
return Item{}, false
}
} }
reason := "no-commenter" reason := "no-commenter"
switch { switch {
@@ -560,12 +494,12 @@ func considerPull(pr PullRequest, comments []Comment, username string, threshold
reason = "no-reviewer" reason = "no-reviewer"
case lastCommentBy == author: case lastCommentBy == author:
reason = "author-idle" reason = "author-idle"
case lastCommentBy != "" && containsStr(reviewers, lastCommentBy): case lastCommentBy != "" && containsString(reviewers, lastCommentBy):
reason = "reviewer-idle" reason = "reviewer-idle"
} }
item := Item{ item := Item{
Type: "pull", Repo: pr.FullName(), Number: pr.Number, Type: "pull", Repo: pr.FullName(), Number: pr.Number,
Title: pr.Title, URL: pr.URL, Title: pr.Title, URL: pr.WebURL(),
Author: author, Reviewers: reviewers, Author: author, Reviewers: reviewers,
LastCommentBy: lastCommentBy, LastActivityAt: lastActivity, LastCommentBy: lastCommentBy, LastActivityAt: lastActivity,
StalledHours: round1(stalled), Reason: reason, StalledHours: round1(stalled), Reason: reason,
@@ -573,19 +507,6 @@ func considerPull(pr PullRequest, comments []Comment, username string, threshold
return item, true 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 { func anyOpen(children []Child) bool {
for _, c := range children { for _, c := range children {
if c.State == "open" { if c.State == "open" {
@@ -595,25 +516,6 @@ func anyOpen(children []Child) bool {
return false 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))。 // round1 舍入到小數一位(對應 Python round(x, 1))。
// 經 strconv 的十進位正確舍入(半偶數),與 Python 的浮點 round 行為一致, // 經 strconv 的十進位正確舍入(半偶數),與 Python 的浮點 round 行為一致,
// 避免 (x*10+0.5) 截斷在 .x5 邊界產生偏差。 // 避免 (x*10+0.5) 截斷在 .x5 邊界產生偏差。
+58 -55
View File
@@ -21,9 +21,9 @@ func at(minutes int) time.Time {
// fakeSource 是注入假資料的 Source。 // fakeSource 是注入假資料的 Source。
type fakeSource struct { type fakeSource struct {
orgs []string orgs []string
repos map[string][]Repository repos map[string][]Repo
// issues[owner/repo][number] // issues[owner/repo][number]
issues map[string]map[int]Issue issues map[string]map[int64]Issue
// pulls[owner/repo] // pulls[owner/repo]
pulls map[string][]PullRequest pulls map[string][]PullRequest
// comments[owner/repo/number] // comments[owner/repo/number]
@@ -32,7 +32,7 @@ type fakeSource struct {
func (f *fakeSource) MyOrganizations() ([]string, error) { return f.orgs, nil } func (f *fakeSource) MyOrganizations() ([]string, error) { return f.orgs, nil }
func (f *fakeSource) OrganizationRepositories(org string) ([]Repository, error) { func (f *fakeSource) OrganizationRepositories(org string) ([]Repo, error) {
return f.repos[org], nil return f.repos[org], nil
} }
@@ -48,11 +48,11 @@ func (f *fakeSource) OpenPulls(owner, repo string) ([]PullRequest, error) {
return f.pulls[owner+"/"+repo], nil return f.pulls[owner+"/"+repo], nil
} }
func (f *fakeSource) IssueComments(owner, repo string, number int) ([]Comment, error) { func (f *fakeSource) IssueComments(owner, repo string, number int64) ([]Comment, error) {
return f.comments[fmt.Sprintf("%s/%s/%d", owner, repo, number)], nil return f.comments[fmt.Sprintf("%s/%s/%d", owner, repo, number)], nil
} }
func (f *fakeSource) TryGetIssue(owner, repo string, number int) (Issue, bool, error) { func (f *fakeSource) TryGetIssue(owner, repo string, number int64) (Issue, bool, error) {
i, ok := f.issues[owner+"/"+repo][number] i, ok := f.issues[owner+"/"+repo][number]
if !ok { if !ok {
return Issue{}, false, nil return Issue{}, false, nil
@@ -60,11 +60,14 @@ func (f *fakeSource) TryGetIssue(owner, repo string, number int) (Issue, bool, e
return i, true, nil return i, true, nil
} }
func repoWith(name string, hasPR bool) []Repository { func repoWith(name string, hasPR bool) []Repo {
h := hasPR h := hasPR
return []Repository{{Name: name, HasPullRequests: &h}} return []Repo{{Name: name, HasPullRequests: &h}}
} }
// iso 把相對分鐘轉成 parseMoment 可剖析的 ISO8601 字串(假資料用)。
func iso(minutes int) string { return FormatMoment(at(minutes)) }
func mustResolve(t *testing.T, org, repo string, texts []string, myOrgs map[string]bool, orgRepos func(string) ([]string, error)) []XrefTarget { func mustResolve(t *testing.T, org, repo string, texts []string, myOrgs map[string]bool, orgRepos func(string) ([]string, error)) []XrefTarget {
t.Helper() t.Helper()
targets, err := ResolveXrefTargets(org, repo, texts, myOrgs, orgRepos) targets, err := ResolveXrefTargets(org, repo, texts, myOrgs, orgRepos)
@@ -157,21 +160,21 @@ func TestScanStalledIssueReasons(t *testing.T) {
now := at(0) now := at(0)
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{ repos: map[string][]Repo{
"alterminal": repoWith("agents", true), "alterminal": repoWith("agents", true),
}, },
issues: map[string]map[int]Issue{ issues: map[string]map[int64]Issue{
"alterminal/agents": { "alterminal/agents": {
1: {Number: 1, Title: "無分派者", State: "open", CreatedAt: at(-600), UpdatedAt: at(-600), Repository: Reference{"alterminal/agents"}, URL: "u1"}, 1: {Number: 1, Title: "無分派者", State: "open", CreatedAt: iso(-600), UpdatedAt: iso(-600), Repository: &Repository{FullName: "alterminal/agents"}, URL: "u1"},
2: {Number: 2, Title: "分派者自己最後留言", State: "open", Author: User{"ceo"}, CreatedAt: at(-600), UpdatedAt: at(-300), Assignees: []User{{"ceo"}}, Repository: Reference{"alterminal/agents"}, URL: "u2"}, 2: {Number: 2, Title: "分派者自己最後留言", State: "open", User: &User{UserName: "ceo"}, CreatedAt: iso(-600), UpdatedAt: iso(-300), Assignees: []User{{UserName: "ceo"}}, Repository: &Repository{FullName: "alterminal/agents"}, URL: "u2"},
3: {Number: 3, Title: "無留言", State: "open", CreatedAt: at(-600), UpdatedAt: at(-300), Assignees: []User{{"ceo"}}, Repository: Reference{"alterminal/agents"}, URL: "u3"}, 3: {Number: 3, Title: "無留言", State: "open", CreatedAt: iso(-600), UpdatedAt: iso(-300), Assignees: []User{{UserName: "ceo"}}, Repository: &Repository{FullName: "alterminal/agents"}, URL: "u3"},
4: {Number: 4, Title: "等外部回應", State: "open", CreatedAt: at(-600), UpdatedAt: at(-300), Assignees: []User{{"ceo"}}, Repository: Reference{"alterminal/agents"}, URL: "u4"}, 4: {Number: 4, Title: "等外部回應", State: "open", CreatedAt: iso(-600), UpdatedAt: iso(-300), Assignees: []User{{UserName: "ceo"}}, Repository: &Repository{FullName: "alterminal/agents"}, URL: "u4"},
5: {Number: 5, Title: "太新不在門檻內", State: "open", CreatedAt: at(-30), UpdatedAt: at(-30), Assignees: []User{{"ceo"}}, Repository: Reference{"alterminal/agents"}, URL: "u5"}, 5: {Number: 5, Title: "太新不在門檻內", State: "open", CreatedAt: iso(-30), UpdatedAt: iso(-30), Assignees: []User{{UserName: "ceo"}}, Repository: &Repository{FullName: "alterminal/agents"}, URL: "u5"},
}, },
}, },
comments: map[string][]Comment{ comments: map[string][]Comment{
"alterminal/agents/2": {{User: User{"ceo"}, CreatedAt: at(-300)}}, "alterminal/agents/2": {{User: &User{UserName: "ceo"}, CreatedAt: iso(-300)}},
"alterminal/agents/4": {{User: User{"dan"}, CreatedAt: at(-300)}}, "alterminal/agents/4": {{User: &User{UserName: "dan"}, CreatedAt: iso(-300)}},
}, },
} }
got, err := ScanStalled(src, "admin", 4.0, now) got, err := ScanStalled(src, "admin", 4.0, now)
@@ -182,11 +185,11 @@ func TestScanStalledIssueReasons(t *testing.T) {
if len(got) != 4 { if len(got) != 4 {
t.Fatalf("應 4 筆,got %d:%v", len(got), got) t.Fatalf("應 4 筆,got %d:%v", len(got), got)
} }
byNumber := map[int]Item{} byNumber := map[int64]Item{}
for _, it := range got { for _, it := range got {
byNumber[it.Number] = it byNumber[it.Number] = it
} }
for n, wantReason := range map[int]string{1: "no-assignee", 2: "assignee-idle", 3: "no-commenter", 4: "waiting-outside"} { for n, wantReason := range map[int64]string{1: "no-assignee", 2: "assignee-idle", 3: "no-commenter", 4: "waiting-outside"} {
if byNumber[n].Reason != wantReason { if byNumber[n].Reason != wantReason {
t.Errorf("issue %d reason = %q, want %q", n, byNumber[n].Reason, wantReason) t.Errorf("issue %d reason = %q, want %q", n, byNumber[n].Reason, wantReason)
} }
@@ -204,15 +207,15 @@ func TestScanStalledNudgeCooldown(t *testing.T) {
now := at(0) now := at(0)
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{"alterminal": repoWith("agents", true)}, repos: map[string][]Repo{"alterminal": repoWith("agents", true)},
issues: map[string]map[int]Issue{ issues: map[string]map[int64]Issue{
"alterminal/agents": { "alterminal/agents": {
1: {Number: 1, State: "open", CreatedAt: at(-600), UpdatedAt: at(-600), Assignees: []User{{"max"}}, Repository: Reference{"alterminal/agents"}}, 1: {Number: 1, State: "open", CreatedAt: iso(-600), UpdatedAt: iso(-600), Assignees: []User{{UserName: "max"}}, Repository: &Repository{FullName: "alterminal/agents"}},
}, },
}, },
comments: map[string][]Comment{ comments: map[string][]Comment{
// admin(掃描者)10 小時前催促過 → 冷卻中,不列入。 // admin(掃描者)10 小時前催促過 → 冷卻中,不列入。
"alterminal/agents/1": {{User: User{"admin"}, CreatedAt: at(-600)}}, "alterminal/agents/1": {{User: &User{UserName: "admin"}, CreatedAt: iso(-600)}},
}, },
} }
got, err := ScanStalled(src, "admin", 4.0, now) got, err := ScanStalled(src, "admin", 4.0, now)
@@ -223,7 +226,7 @@ func TestScanStalledNudgeCooldown(t *testing.T) {
t.Fatalf("催促冷卻中的項目不應列入,got %v", got) t.Fatalf("催促冷卻中的項目不應列入,got %v", got)
} }
// 冷卻過了(25 小時前催促)→ 重新列入。 // 冷卻過了(25 小時前催促)→ 重新列入。
src.comments["alterminal/agents/1"] = []Comment{{User: User{"admin"}, CreatedAt: at(-1500)}} src.comments["alterminal/agents/1"] = []Comment{{User: &User{UserName: "admin"}, CreatedAt: iso(-1500)}}
got, err = ScanStalled(src, "admin", 4.0, now) got, err = ScanStalled(src, "admin", 4.0, now)
if err != nil { if err != nil {
t.Fatalf("ScanStalled: %v", err) t.Fatalf("ScanStalled: %v", err)
@@ -240,15 +243,15 @@ func TestScanStalledParentTracking(t *testing.T) {
// 應以 parent-tracking 列入並附子項摘要;子項本身則是 assignee-idle。 // 應以 parent-tracking 列入並附子項摘要;子項本身則是 assignee-idle。
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{"alterminal": repoWith("teai", true)}, repos: map[string][]Repo{"alterminal": repoWith("teai", true)},
issues: map[string]map[int]Issue{ issues: map[string]map[int64]Issue{
"alterminal/teai": { "alterminal/teai": {
3: {Number: 3, Title: "父", State: "open", Body: "子項:#7", CreatedAt: at(-700), UpdatedAt: at(-300), Assignees: []User{{"ceo"}}, Repository: Reference{"alterminal/teai"}}, 3: {Number: 3, Title: "父", State: "open", Body: "子項:#7", CreatedAt: iso(-700), UpdatedAt: iso(-300), Assignees: []User{{UserName: "ceo"}}, Repository: &Repository{FullName: "alterminal/teai"}},
7: {Number: 7, Title: "子", State: "open", CreatedAt: at(-650), UpdatedAt: at(-300), Assignees: []User{{"max"}}, Repository: Reference{"alterminal/teai"}}, 7: {Number: 7, Title: "子", State: "open", CreatedAt: iso(-650), UpdatedAt: iso(-300), Assignees: []User{{UserName: "max"}}, Repository: &Repository{FullName: "alterminal/teai"}},
}, },
}, },
comments: map[string][]Comment{ comments: map[string][]Comment{
"alterminal/teai/7": {{User: User{"max"}, CreatedAt: at(-300)}}, "alterminal/teai/7": {{User: &User{UserName: "max"}, CreatedAt: iso(-300)}},
}, },
} }
got, err := ScanStalled(src, "admin", 4.0, now) got, err := ScanStalled(src, "admin", 4.0, now)
@@ -286,9 +289,9 @@ func TestScanStalledParentTracking(t *testing.T) {
// 子項 1 小時前恢復活動 → 父項 last_activity 跟著更新,未達門檻不列入; // 子項 1 小時前恢復活動 → 父項 last_activity 跟著更新,未達門檻不列入;
// 子項本身也不停滯。整份輸出應為空。 // 子項本身也不停滯。整份輸出應為空。
child := src.issues["alterminal/teai"][7] child := src.issues["alterminal/teai"][7]
child.UpdatedAt = at(-60) child.UpdatedAt = iso(-60)
src.issues["alterminal/teai"][7] = child src.issues["alterminal/teai"][7] = child
src.comments["alterminal/teai/7"] = []Comment{{User: User{"max"}, CreatedAt: at(-60)}} src.comments["alterminal/teai/7"] = []Comment{{User: &User{UserName: "max"}, CreatedAt: iso(-60)}}
got, err = ScanStalled(src, "admin", 4.0, now) got, err = ScanStalled(src, "admin", 4.0, now)
if err != nil { if err != nil {
t.Fatalf("ScanStalled: %v", err) t.Fatalf("ScanStalled: %v", err)
@@ -303,11 +306,11 @@ func TestScanStalledParentTrackingNotChild(t *testing.T) {
now := at(0) now := at(0)
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{"alterminal": repoWith("teai", true)}, repos: map[string][]Repo{"alterminal": repoWith("teai", true)},
issues: map[string]map[int]Issue{ issues: map[string]map[int64]Issue{
"alterminal/teai": { "alterminal/teai": {
47: {Number: 47, State: "open", Body: "規劃見 #39(更早)", CreatedAt: at(-700), UpdatedAt: at(-600), Assignees: []User{{"ceo"}}, Repository: Reference{"alterminal/teai"}}, 47: {Number: 47, State: "open", Body: "規劃見 #39(更早)", CreatedAt: iso(-700), UpdatedAt: iso(-600), Assignees: []User{{UserName: "ceo"}}, Repository: &Repository{FullName: "alterminal/teai"}},
39: {Number: 39, State: "open", CreatedAt: at(-2000), UpdatedAt: at(-60), Repository: Reference{"alterminal/teai"}}, 39: {Number: 39, State: "open", CreatedAt: iso(-2000), UpdatedAt: iso(-60), Repository: &Repository{FullName: "alterminal/teai"}},
}, },
}, },
} }
@@ -345,30 +348,30 @@ func TestScanStalledPullReasons(t *testing.T) {
h := true h := true
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{"alterminal": {{Name: "teai", HasPullRequests: &h}}}, repos: map[string][]Repo{"alterminal": {{Name: "teai", HasPullRequests: &h}}},
pulls: map[string][]PullRequest{ pulls: map[string][]PullRequest{
"alterminal/teai": { "alterminal/teai": {
{Number: 10, State: "open", Author: User{"max"}, CreatedAt: at(-600), UpdatedAt: at(-600)}, {Number: 10, User: &User{UserName: "max"}, CreatedAt: iso(-600), UpdatedAt: iso(-600)},
{Number: 11, State: "open", Author: User{"max"}, CreatedAt: at(-600), UpdatedAt: at(-300), RequestedReviewers: []User{{"ceo"}}}, {Number: 11, User: &User{UserName: "max"}, CreatedAt: iso(-600), UpdatedAt: iso(-300), RequestedReviewers: []User{{UserName: "ceo"}}},
{Number: 12, State: "open", Author: User{"max"}, CreatedAt: at(-600), UpdatedAt: at(-300), RequestedReviewers: []User{{"ceo"}}}, {Number: 12, User: &User{UserName: "max"}, CreatedAt: iso(-600), UpdatedAt: iso(-300), RequestedReviewers: []User{{UserName: "ceo"}}},
{Number: 13, State: "open", Author: User{"max"}, CreatedAt: at(-600), UpdatedAt: at(-300), RequestedReviewers: []User{{"ceo"}}}, {Number: 13, User: &User{UserName: "max"}, CreatedAt: iso(-600), UpdatedAt: iso(-300), RequestedReviewers: []User{{UserName: "ceo"}}},
}, },
}, },
comments: map[string][]Comment{ comments: map[string][]Comment{
"alterminal/teai/11": {{User: User{"max"}, CreatedAt: at(-300)}}, // author-idle "alterminal/teai/11": {{User: &User{UserName: "max"}, CreatedAt: iso(-300)}}, // author-idle
"alterminal/teai/12": {{User: User{"ceo"}, CreatedAt: at(-300)}}, // reviewer-idle "alterminal/teai/12": {{User: &User{UserName: "ceo"}, CreatedAt: iso(-300)}}, // reviewer-idle
"alterminal/teai/13": {{User: User{"dan"}, CreatedAt: at(-300)}}, // no-commenter(外部留言) "alterminal/teai/13": {{User: &User{UserName: "dan"}, CreatedAt: iso(-300)}}, // no-commenter(外部留言)
}, },
} }
got, err := ScanStalled(src, "admin", 4.0, now) got, err := ScanStalled(src, "admin", 4.0, now)
if err != nil { if err != nil {
t.Fatalf("ScanStalled: %v", err) t.Fatalf("ScanStalled: %v", err)
} }
byNumber := map[int]Item{} byNumber := map[int64]Item{}
for _, it := range got { for _, it := range got {
byNumber[it.Number] = it byNumber[it.Number] = it
} }
for n, wantReason := range map[int]string{10: "no-reviewer", 11: "author-idle", 12: "reviewer-idle", 13: "no-commenter"} { for n, wantReason := range map[int64]string{10: "no-reviewer", 11: "author-idle", 12: "reviewer-idle", 13: "no-commenter"} {
item, ok := byNumber[n] item, ok := byNumber[n]
if !ok { if !ok {
t.Fatalf("PR %d 應列入,got %v", n, got) t.Fatalf("PR %d 應列入,got %v", n, got)
@@ -391,15 +394,15 @@ func TestScanStalledNoPullsFlag(t *testing.T) {
h := false h := false
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{"alterminal": {{Name: "docs", HasPullRequests: &h}}}, repos: map[string][]Repo{"alterminal": {{Name: "docs", HasPullRequests: &h}}},
issues: map[string]map[int]Issue{ issues: map[string]map[int64]Issue{
"alterminal/docs": { "alterminal/docs": {
1: {Number: 1, State: "open", CreatedAt: at(-600), UpdatedAt: at(-600), Repository: Reference{"alterminal/docs"}}, 1: {Number: 1, State: "open", CreatedAt: iso(-600), UpdatedAt: iso(-600), Repository: &Repository{FullName: "alterminal/docs"}},
}, },
}, },
pulls: map[string][]PullRequest{ pulls: map[string][]PullRequest{
// 即使假資料有 PR,flag 關閉也不應掃。 // 即使假資料有 PR,flag 關閉也不應掃。
"alterminal/docs": {{Number: 2, State: "open", Author: User{"max"}, CreatedAt: at(-600), UpdatedAt: at(-600)}}, "alterminal/docs": {{Number: 2, User: &User{UserName: "max"}, CreatedAt: iso(-600), UpdatedAt: iso(-600)}},
}, },
} }
got, err := ScanStalled(src, "admin", 4.0, now) got, err := ScanStalled(src, "admin", 4.0, now)
@@ -416,10 +419,10 @@ func TestScanStalledSkipsPRsInIssues(t *testing.T) {
now := at(0) now := at(0)
src := &fakeSource{ src := &fakeSource{
orgs: []string{"alterminal"}, orgs: []string{"alterminal"},
repos: map[string][]Repository{"alterminal": repoWith("teai", true)}, repos: map[string][]Repo{"alterminal": repoWith("teai", true)},
issues: map[string]map[int]Issue{ issues: map[string]map[int64]Issue{
"alterminal/teai": { "alterminal/teai": {
1: {Number: 1, State: "open", CreatedAt: at(-600), UpdatedAt: at(-600), Repository: Reference{"alterminal/teai"}, PullRequest: struct{}{}}, 1: {Number: 1, State: "open", CreatedAt: iso(-600), UpdatedAt: iso(-600), Repository: &Repository{FullName: "alterminal/teai"}, PullRequest: &pullMarker{}},
}, },
}, },
} }
@@ -480,7 +483,7 @@ func TestPullJSON(t *testing.T) {
// TestScanStalledEmpty 無停滯項目 → 空輸出(CLI 層輸出 [])。 // TestScanStalledEmpty 無停滯項目 → 空輸出(CLI 層輸出 [])。
func TestScanStalledEmpty(t *testing.T) { func TestScanStalledEmpty(t *testing.T) {
now := at(0) now := at(0)
src := &fakeSource{orgs: []string{"alterminal"}, repos: map[string][]Repository{"alterminal": repoWith("teai", true)}} src := &fakeSource{orgs: []string{"alterminal"}, repos: map[string][]Repo{"alterminal": repoWith("teai", true)}}
got, err := ScanStalled(src, "admin", 4.0, now) got, err := ScanStalled(src, "admin", 4.0, now)
if err != nil { if err != nil {
t.Fatalf("ScanStalled: %v", err) t.Fatalf("ScanStalled: %v", err)
@@ -501,15 +504,15 @@ func TestScanStalledOrgError(t *testing.T) {
type errSource struct{} type errSource struct{}
func (errSource) MyOrganizations() ([]string, error) { return nil, errors.New("boom") } func (errSource) MyOrganizations() ([]string, error) { return nil, errors.New("boom") }
func (e errSource) OrganizationRepositories(string) ([]Repository, error) { func (e errSource) OrganizationRepositories(string) ([]Repo, error) {
return nil, errors.New("boom") return nil, errors.New("boom")
} }
func (e errSource) OpenIssues(string, string) ([]Issue, error) { return nil, errors.New("boom") } func (e errSource) OpenIssues(string, string) ([]Issue, error) { return nil, errors.New("boom") }
func (e errSource) OpenPulls(string, string) ([]PullRequest, error) { return nil, errors.New("boom") } func (e errSource) OpenPulls(string, string) ([]PullRequest, error) { return nil, errors.New("boom") }
func (e errSource) IssueComments(string, string, int) ([]Comment, error) { func (e errSource) IssueComments(string, string, int64) ([]Comment, error) {
return nil, errors.New("boom") return nil, errors.New("boom")
} }
func (e errSource) TryGetIssue(string, string, int) (Issue, bool, error) { func (e errSource) TryGetIssue(string, string, int64) (Issue, bool, error) {
return Issue{}, false, errors.New("boom") return Issue{}, false, errors.New("boom")
} }