internal/workflow + cli:gitea.py 工作流命令 Go 化(#6)
- internal/workflow:Client 介面+判定語義(最後留言、作者/審核者跟進、 assignee 交集、最久未更新優先),含假 client 單元測試 - internal/workflow/giteaapi.go:以 internal/gitea(#5)實作 Client - internal/cli:註冊 whoami/orgs/members/mine/pulls/next;全域選項允許 出現在命令之後;結束碼 0/2/3 - 與 gitea.py 交叉驗證:next/mine/pulls/members(--has-work) 拾取一致 - 依 #5 分支 feature/issue5-gitea-client 為基底
This commit is contained in:
@@ -6,7 +6,7 @@ teai 是 Gitea 的命令列(CLI)輔助工具,補足官方 `tea` CLI 缺少
|
|||||||
|
|
||||||
- **語言:** Go(與 Gitea 本體一致),僅用標準庫,離線可建置
|
- **語言:** Go(與 Gitea 本體一致),僅用標準庫,離線可建置
|
||||||
- **目標站點:** `https://gitea.alterminal.com`
|
- **目標站點:** `https://gitea.alterminal.com`
|
||||||
- **狀態:** 專案初始化中,功能尚未開始開發;本文的指令集為設計藍圖
|
- **狀態:** 基礎建設(#5:internal/gitea)與工作流命令(#6:whoami/orgs/members/mine/pulls/next)已實作;stalled/xrefs(#7)與日常操作命令(#8)進行中
|
||||||
|
|
||||||
## 安裝與建置
|
## 安裝與建置
|
||||||
|
|
||||||
|
|||||||
+10
-5
@@ -12,7 +12,6 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -213,14 +212,20 @@ func parseGlobals(g *Globals, args []string) ([]string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dispatch 執行已註冊的子命令,把錯誤轉成結束碼並輸出。
|
// dispatch 執行已註冊的子命令,把錯誤轉成結束碼並輸出。
|
||||||
|
// 旗標剖析交由各命令自行處理(不同命令有不同旗標,如 pulls --mine);
|
||||||
|
// 全域選項允許出現在命令之後(README 範例:teai next --output table),
|
||||||
|
// 先從 args 剝離併入 Globals 再分派。
|
||||||
func dispatch(env *Env, name string, args []string) int {
|
func dispatch(env *Env, name string, args []string) int {
|
||||||
cmd := commands[name]
|
cmd := commands[name]
|
||||||
fs := flag.NewFlagSet("teai "+name, flag.ContinueOnError)
|
g := env.Globals
|
||||||
fs.SetOutput(env.Err)
|
rest, err := parseGlobals(&g, args)
|
||||||
if err := fs.Parse(args); err != nil {
|
if err != nil {
|
||||||
|
fmt.Fprintf(env.Err, "teai: %v\n\n", err)
|
||||||
|
printUsage(env.Err)
|
||||||
return int(ExitUsage)
|
return int(ExitUsage)
|
||||||
}
|
}
|
||||||
if err := cmd.run(env, fs.Args()); err != nil {
|
env.Globals = g
|
||||||
|
if err := cmd.run(env, rest); err != nil {
|
||||||
fmt.Fprintf(env.Err, "teai %s: %v\n", name, err)
|
fmt.Fprintf(env.Err, "teai %s: %v\n", name, err)
|
||||||
return int(exitCodeFor(err))
|
return int(exitCodeFor(err))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,242 @@
|
|||||||
|
// workflow_commands.go — gitea.py 工作流命令的 CLI 接線(#6):
|
||||||
|
// whoami、orgs、members [--has-work]、mine、pulls [--mine|--reviewer] [--repo]、next。
|
||||||
|
//
|
||||||
|
// 輸出走 internal/gitea 的 Printer(--output json|table);錯誤經 cli.exitCodeFor
|
||||||
|
// 映射結束碼(用法 2/API 3)。token 解析選序見 internal/gitea/auth.go。
|
||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.alterminal.com/alterminal/teai/internal/gitea"
|
||||||
|
"gitea.alterminal.com/alterminal/teai/internal/workflow"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
register := func(c *command) {
|
||||||
|
commands[c.name] = c
|
||||||
|
}
|
||||||
|
register(&command{name: "whoami", usage: "目前帳號", run: runWhoami})
|
||||||
|
register(&command{name: "orgs", usage: "我所屬的組織", run: runOrgs})
|
||||||
|
register(&command{name: "members", usage: "Agents 團隊成員;--has-work 只列有未完成工作者", run: runMembers})
|
||||||
|
register(&command{name: "mine", usage: "分派給我、待跟進的 issues", run: runMine})
|
||||||
|
register(&command{name: "pulls", usage: "跨倉庫 PR 跟進清單(--mine/--reviewer/--repo)", run: runPulls})
|
||||||
|
register(&command{name: "next", usage: "下一件該做的事", run: runNext})
|
||||||
|
}
|
||||||
|
|
||||||
|
// errNoRepoFlag 是 --repo 值格式錯誤。
|
||||||
|
var errNoRepoFlag = errors.New("--repo 需為 owner/repo 形式")
|
||||||
|
|
||||||
|
// newWorkflowClient 依全域選項建立 workflow 所需的 API client。
|
||||||
|
func newWorkflowClient(env *Env) (*workflow.Scanner, error) {
|
||||||
|
token, _, err := gitea.ResolveToken(gitea.AuthOptions{
|
||||||
|
FlagToken: env.Globals.Token,
|
||||||
|
SiteURL: env.Globals.URL,
|
||||||
|
ConfigPath: env.Globals.ConfigPath,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
client, err := gitea.New(env.Globals.URL, token, env.Globals.Timeout)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &workflow.Scanner{Client: &workflow.APIClient{Client: client}}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// printer 依全域選項建立輸出器。
|
||||||
|
func printer(env *Env) gitea.Printer {
|
||||||
|
return gitea.Printer{Format: env.Globals.Output, Out: env.Out}
|
||||||
|
}
|
||||||
|
|
||||||
|
// runWhoami:teai whoami。
|
||||||
|
func runWhoami(env *Env, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return &ErrUsage{Msg: "whoami 不接受參數"}
|
||||||
|
}
|
||||||
|
s, err := newWorkflowClient(env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
name, err := s.Whoami()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return printer(env).PrintOne(
|
||||||
|
[]gitea.Field{{Name: "username"}},
|
||||||
|
gitea.Values{name},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runOrgs:teai orgs。
|
||||||
|
func runOrgs(env *Env, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return &ErrUsage{Msg: "orgs 不接受參數"}
|
||||||
|
}
|
||||||
|
s, err := newWorkflowClient(env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
orgs, err := s.Orgs()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([]gitea.Values, 0, len(orgs))
|
||||||
|
for _, o := range orgs {
|
||||||
|
rows = append(rows, gitea.Values{o})
|
||||||
|
}
|
||||||
|
return printer(env).PrintList(
|
||||||
|
[]gitea.Field{{Name: "username"}},
|
||||||
|
rows,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runMembers:teai members [--has-work]。輸出每行一個帳號(同 gitea.py)。
|
||||||
|
func runMembers(env *Env, args []string) error {
|
||||||
|
fs := flag.NewFlagSet("teai members", flag.ContinueOnError)
|
||||||
|
fs.SetOutput(env.Err)
|
||||||
|
hasWork := fs.Bool("has-work", false, "只列出有未完成工作的成員")
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return &ErrUsage{Msg: err.Error()}
|
||||||
|
}
|
||||||
|
if fs.NArg() != 0 {
|
||||||
|
return &ErrUsage{Msg: "members 不接受位置參數"}
|
||||||
|
}
|
||||||
|
s, err := newWorkflowClient(env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
names, err := s.Members(*hasWork)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// gitea.py members 每行輸出一個帳號(非 JSON);保持一致以便交叉驗證。
|
||||||
|
for _, name := range names {
|
||||||
|
fmt.Fprintln(env.Out, name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// runMine:teai mine。
|
||||||
|
func runMine(env *Env, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return &ErrUsage{Msg: "mine 不接受參數"}
|
||||||
|
}
|
||||||
|
s, err := newWorkflowClient(env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
issues, err := s.Mine()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return printer(env).PrintList(
|
||||||
|
[]gitea.Field{
|
||||||
|
{Name: "repo"}, {Name: "number"}, {Name: "title"}, {Name: "url"},
|
||||||
|
},
|
||||||
|
issueRows(issues),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runPulls:teai pulls [--mine|--reviewer] [--repo owner/repo]。
|
||||||
|
func runPulls(env *Env, args []string) error {
|
||||||
|
fs := flag.NewFlagSet("teai pulls", flag.ContinueOnError)
|
||||||
|
fs.SetOutput(env.Err)
|
||||||
|
mine := fs.Bool("mine", false, "限定作者是目前帳號")
|
||||||
|
reviewer := fs.Bool("reviewer", false, "限定自己是 requested reviewer")
|
||||||
|
repo := fs.String("repo", "", "限定單一倉庫(owner/repo)")
|
||||||
|
if err := fs.Parse(args); err != nil {
|
||||||
|
return &ErrUsage{Msg: err.Error()}
|
||||||
|
}
|
||||||
|
if fs.NArg() != 0 {
|
||||||
|
return &ErrUsage{Msg: "pulls 不接受位置參數"}
|
||||||
|
}
|
||||||
|
if *mine && *reviewer {
|
||||||
|
return &ErrUsage{Msg: "--mine 與 --reviewer 不能併用"}
|
||||||
|
}
|
||||||
|
var owner, name string
|
||||||
|
if *repo != "" {
|
||||||
|
parts := strings.SplitN(*repo, "/", 2)
|
||||||
|
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
|
||||||
|
return &ErrUsage{Msg: errNoRepoFlag.Error()}
|
||||||
|
}
|
||||||
|
owner, name = parts[0], parts[1]
|
||||||
|
}
|
||||||
|
s, err := newWorkflowClient(env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
items, err := s.Pulls(*mine, *reviewer)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows := make([]gitea.Values, 0, len(items))
|
||||||
|
for _, item := range items {
|
||||||
|
if *repo != "" && (item.Owner != owner || item.Repo != name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
rows = append(rows, gitea.Values{
|
||||||
|
item.Owner + "/" + item.Repo,
|
||||||
|
item.Pull.Number,
|
||||||
|
item.Pull.Title,
|
||||||
|
item.Pull.WebURL(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return printer(env).PrintList(
|
||||||
|
[]gitea.Field{
|
||||||
|
{Name: "repo"}, {Name: "number"}, {Name: "title"}, {Name: "url"},
|
||||||
|
},
|
||||||
|
rows,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// runNext:teai next。無工作輸出 null(exit 0)。
|
||||||
|
func runNext(env *Env, args []string) error {
|
||||||
|
if len(args) != 0 {
|
||||||
|
return &ErrUsage{Msg: "next 不接受參數"}
|
||||||
|
}
|
||||||
|
s, err := newWorkflowClient(env)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
work, err := s.Next()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p := printer(env)
|
||||||
|
if work == nil {
|
||||||
|
return p.PrintNull()
|
||||||
|
}
|
||||||
|
// gitea.py 的 _work_dict:role 僅 pull 帶(issue 不輸出 role 欄位)。
|
||||||
|
fields := []gitea.Field{
|
||||||
|
{Name: "type"}, {Name: "role"}, {Name: "repo"},
|
||||||
|
{Name: "number"}, {Name: "title"}, {Name: "url"},
|
||||||
|
}
|
||||||
|
row := gitea.Values{work.Type, work.Role, work.Repo, work.Number, work.Title, work.URL}
|
||||||
|
if work.Role == "" {
|
||||||
|
fields = []gitea.Field{
|
||||||
|
{Name: "type"}, {Name: "repo"},
|
||||||
|
{Name: "number"}, {Name: "title"}, {Name: "url"},
|
||||||
|
}
|
||||||
|
row = gitea.Values{work.Type, work.Repo, work.Number, work.Title, work.URL}
|
||||||
|
}
|
||||||
|
return p.PrintOne(fields, row)
|
||||||
|
}
|
||||||
|
|
||||||
|
// issueRows 把 issues 轉成輸出列(repo 欄位由掃描脈絡補齊)。
|
||||||
|
func issueRows(issues []workflow.Issue) []gitea.Values {
|
||||||
|
rows := make([]gitea.Values, 0, len(issues))
|
||||||
|
for i := range issues {
|
||||||
|
issue := &issues[i]
|
||||||
|
rows = append(rows, gitea.Values{
|
||||||
|
issue.FullName(),
|
||||||
|
issue.Num(),
|
||||||
|
issue.Title,
|
||||||
|
issue.WebURL(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return rows
|
||||||
|
}
|
||||||
@@ -0,0 +1,124 @@
|
|||||||
|
// giteaapi.go — 以 internal/gitea 的 HTTP 用戶端實作 workflow.Client。
|
||||||
|
//
|
||||||
|
// 端點與查詢參數對照 gitea.py:清單一律帶 limit 分頁(ListAll 固定 50);
|
||||||
|
// issues 端點帶 type=issues(排除 PR)與 state=open;pulls 帶 state=open。
|
||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"gitea.alterminal.com/alterminal/teai/internal/gitea"
|
||||||
|
)
|
||||||
|
|
||||||
|
// errNoUsername 表示 /user 回應中找不到帳號名(伺服端回應異常)。
|
||||||
|
var errNoUsername = errors.New("gitea: GET /user: response has no username")
|
||||||
|
|
||||||
|
// APIClient 是 Client 介面的 HTTP 實作,包裝 internal/gitea.Client。
|
||||||
|
type APIClient struct {
|
||||||
|
Client *gitea.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// 確認 APIClient 實作 Client 介面(編譯期檢查)。
|
||||||
|
var _ Client = (*APIClient)(nil)
|
||||||
|
|
||||||
|
func (a *APIClient) CurrentUser() (string, error) {
|
||||||
|
var u User
|
||||||
|
if err := a.Client.GetJSON(context.Background(), "/user", &u); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
name := u.Name()
|
||||||
|
if name == "" {
|
||||||
|
return "", errNoUsername
|
||||||
|
}
|
||||||
|
return name, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListOrgs() ([]string, error) {
|
||||||
|
var orgs []Org
|
||||||
|
if err := a.Client.ListAll(context.Background(), "/user/orgs", gitea.ListOptions{}, &orgs); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
names := make([]string, 0, len(orgs))
|
||||||
|
for _, o := range orgs {
|
||||||
|
if n := o.Account(); n != "" {
|
||||||
|
names = append(names, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListTeams(org string) ([]Team, error) {
|
||||||
|
var teams []Team
|
||||||
|
path := "/orgs/" + url.PathEscape(org) + "/teams"
|
||||||
|
if err := a.Client.ListAll(context.Background(), path, gitea.ListOptions{}, &teams); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return teams, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListTeamMembers(teamID int64) ([]string, error) {
|
||||||
|
var users []User
|
||||||
|
path := "/teams/" + strconv.FormatInt(teamID, 10) + "/members"
|
||||||
|
if err := a.Client.ListAll(context.Background(), path, gitea.ListOptions{}, &users); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
names := make([]string, 0, len(users))
|
||||||
|
for i := range users {
|
||||||
|
if n := users[i].Name(); n != "" {
|
||||||
|
names = append(names, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListOrgRepos(org string) ([]Repo, error) {
|
||||||
|
var repos []Repo
|
||||||
|
path := "/orgs/" + url.PathEscape(org) + "/repos"
|
||||||
|
if err := a.Client.ListAll(context.Background(), path, gitea.ListOptions{}, &repos); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return repos, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListIssues(owner, repo string) ([]Issue, error) {
|
||||||
|
var issues []Issue
|
||||||
|
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) + "/issues"
|
||||||
|
opts := gitea.ListOptions{Extra: url.Values{
|
||||||
|
"type": {"issues"},
|
||||||
|
"state": {"open"},
|
||||||
|
}}
|
||||||
|
if err := a.Client.ListAll(context.Background(), path, opts, &issues); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// /issues 也會回傳 PR(帶 pull_request 欄位);一律排除。
|
||||||
|
out := make([]Issue, 0, len(issues))
|
||||||
|
for i := range issues {
|
||||||
|
if issues[i].PullRequest == nil {
|
||||||
|
out = append(out, issues[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListPulls(owner, repo string) ([]PullRequest, error) {
|
||||||
|
var pulls []PullRequest
|
||||||
|
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) + "/pulls"
|
||||||
|
opts := gitea.ListOptions{Extra: url.Values{"state": {"open"}}}
|
||||||
|
if err := a.Client.ListAll(context.Background(), path, opts, &pulls); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return pulls, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *APIClient) ListComments(owner, repo string, number int64) ([]Comment, error) {
|
||||||
|
var comments []Comment
|
||||||
|
path := "/repos/" + url.PathEscape(owner) + "/" + url.PathEscape(repo) +
|
||||||
|
"/issues/" + strconv.FormatInt(number, 10) + "/comments"
|
||||||
|
if err := a.Client.ListAll(context.Background(), path, gitea.ListOptions{}, &comments); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return comments, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
package workflow
|
||||||
|
|
||||||
|
import "sort"
|
||||||
|
|
||||||
|
// sortComments 依 created_at 字典序穩定排序(ISO8601 字串字典序與時間序一致),
|
||||||
|
// 對應 gitea.py 各處的 comments.sort(key=created_at)。
|
||||||
|
func sortComments(comments []Comment) []Comment {
|
||||||
|
out := append([]Comment(nil), comments...)
|
||||||
|
sort.SliceStable(out, func(a, b int) bool { return out[a].CreatedAt < out[b].CreatedAt })
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastComment 回傳依 created_at 排序後的最後一則留言;沒有留言回 nil。
|
||||||
|
func LastComment(comments []Comment) *Comment {
|
||||||
|
ordered := sortComments(comments)
|
||||||
|
if len(ordered) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &ordered[len(ordered)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// lastCommentAuthor 回傳最後一則留言的作者;無留言或作者未知回空字串。
|
||||||
|
func lastCommentAuthor(comments []Comment) string {
|
||||||
|
if last := LastComment(comments); last != nil {
|
||||||
|
return last.Author()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsString(items []string, target string) bool {
|
||||||
|
for _, item := range items {
|
||||||
|
if item == target {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// NeedsAuthorFollowup 判定 PR 是否需要發起者跟進:有留言、且最後一則留言
|
||||||
|
// 不是 PR 作者(gitea.py 的 _pr_needs_author_followup)。
|
||||||
|
// 無留言或作者未知 → false(尚無留言的 PR 不列入跟進清單)。
|
||||||
|
func NeedsAuthorFollowup(pr *PullRequest, comments []Comment) bool {
|
||||||
|
author := pr.Author()
|
||||||
|
if author == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
last := lastCommentAuthor(comments)
|
||||||
|
return last != "" && last != author
|
||||||
|
}
|
||||||
|
|
||||||
|
// NeedsReviewerFollowup 判定使用者是否需要審核此 PR:被指定為審核者、
|
||||||
|
// 不是作者、最後一則留言不是自己(含尚無留言)
|
||||||
|
// (gitea.py 的 _pr_needs_reviewer_followup)。
|
||||||
|
func NeedsReviewerFollowup(pr *PullRequest, comments []Comment, username string) bool {
|
||||||
|
if username == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if pr.Author() == username {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !containsString(ReviewerNames(pr), username) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return lastCommentAuthor(comments) != username
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReviewerNames 回傳 PR 的 requested reviewers(去重、排序)。
|
||||||
|
func ReviewerNames(pr *PullRequest) []string {
|
||||||
|
seen := map[string]bool{}
|
||||||
|
names := make([]string, 0, len(pr.RequestedReviewers))
|
||||||
|
for i := range pr.RequestedReviewers {
|
||||||
|
name := pr.RequestedReviewers[i].Name()
|
||||||
|
if name != "" && !seen[name] {
|
||||||
|
seen[name] = true
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssigneeNames 回傳 issue 的全部 assignees(assignee 欄位+assignees 陣列,
|
||||||
|
// 去重、排序),對應 gitea.py 的 _assignee_usernames。
|
||||||
|
func AssigneeNames(issue *Issue) []string {
|
||||||
|
seen := map[string]bool{}
|
||||||
|
add := func(u *User) {
|
||||||
|
if u == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if name := u.Name(); name != "" && !seen[name] {
|
||||||
|
seen[name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(issue.Assignee)
|
||||||
|
for i := range issue.Assignees {
|
||||||
|
add(&issue.Assignees[i])
|
||||||
|
}
|
||||||
|
names := make([]string, 0, len(seen))
|
||||||
|
for name := range seen {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
// AssigneeOverlaps 判定 issue 是否分派給 members 中至少一人
|
||||||
|
// (members 為空 → false,同 gitea.py filter_issues_by_assignee 的空成員集行為)。
|
||||||
|
func AssigneeOverlaps(issue *Issue, members []string) bool {
|
||||||
|
if len(members) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
assignees := AssigneeNames(issue)
|
||||||
|
for _, member := range members {
|
||||||
|
if member != "" && containsString(assignees, member) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// LastCommentNotByAssignee 判定 issue 的最後一則留言是否不是任何 assignee
|
||||||
|
// 留的(無留言 → true,視為需要處理)。
|
||||||
|
// 注意:判定對象是該 issue 的**全部** assignees,不只是 members 中的成員
|
||||||
|
// (gitea.py _last_comment_is_from_assignee 的語義)。
|
||||||
|
func LastCommentNotByAssignee(issue *Issue, comments []Comment) bool {
|
||||||
|
last := lastCommentAuthor(comments)
|
||||||
|
if last == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return !containsString(AssigneeNames(issue), last)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IssueNeedsWork 組合判定:分派給 members 中至少一人,且最後一則留言
|
||||||
|
// 不是 assignee(gitea.py filter_issues_by_assignee 保留的項目)。
|
||||||
|
func IssueNeedsWork(issue *Issue, comments []Comment, members []string) bool {
|
||||||
|
return AssigneeOverlaps(issue, members) && LastCommentNotByAssignee(issue, comments)
|
||||||
|
}
|
||||||
@@ -0,0 +1,145 @@
|
|||||||
|
// judge_test.go — 驗證工作流判定語義(對照 gitea.py):
|
||||||
|
// 最後留言判定、作者/審核者跟進條件、assignee 交集與排序。
|
||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mkUser 構造 User。
|
||||||
|
func mkUser(name string) *User { return &User{UserName: name} }
|
||||||
|
|
||||||
|
func TestUserNameFallback(t *testing.T) {
|
||||||
|
if got := (&User{UserName: "a", Login: "b"}).Name(); got != "a" {
|
||||||
|
t.Errorf("username 優先,得到 %q", got)
|
||||||
|
}
|
||||||
|
if got := (&User{Login: "b"}).Name(); got != "b" {
|
||||||
|
t.Errorf("login 回退,得到 %q", got)
|
||||||
|
}
|
||||||
|
if got := mkUser("").Name(); got != "" {
|
||||||
|
t.Errorf("空帳號,得到 %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLastComment(t *testing.T) {
|
||||||
|
comments := []Comment{
|
||||||
|
{CreatedAt: "2026-09-09T01:00:00Z", User: mkUser("alex")},
|
||||||
|
{CreatedAt: "2026-09-09T03:00:00Z", User: mkUser("max")},
|
||||||
|
{CreatedAt: "2026-09-09T02:00:00Z", User: mkUser("ceo")},
|
||||||
|
}
|
||||||
|
last := LastComment(comments)
|
||||||
|
if last == nil || last.Author() != "max" {
|
||||||
|
t.Errorf("應依 created_at 排序取最後(03:00 max),得到 %+v", last)
|
||||||
|
}
|
||||||
|
if LastComment(nil) != nil {
|
||||||
|
t.Errorf("無留言應回 nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNeedsAuthorFollowup(t *testing.T) {
|
||||||
|
pr := &PullRequest{User: mkUser("alex")}
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
comments []Comment
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"無留言", nil, false},
|
||||||
|
{"最後留言是作者", []Comment{{CreatedAt: "a", User: mkUser("alex")}}, false},
|
||||||
|
{"最後留言是他人", []Comment{
|
||||||
|
{CreatedAt: "a", User: mkUser("alex")},
|
||||||
|
{CreatedAt: "b", User: mkUser("ceo")},
|
||||||
|
}, true},
|
||||||
|
{"留言者未知", []Comment{{CreatedAt: "a"}}, false},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := NeedsAuthorFollowup(pr, c.comments); got != c.want {
|
||||||
|
t.Errorf("%s:得到 %v,want %v", c.name, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNeedsReviewerFollowup(t *testing.T) {
|
||||||
|
pr := &PullRequest{
|
||||||
|
User: mkUser("max"),
|
||||||
|
RequestedReviewers: []User{*mkUser("alex"), *mkUser("queena")},
|
||||||
|
}
|
||||||
|
comments := []Comment{{CreatedAt: "a", User: mkUser("max")}}
|
||||||
|
if !NeedsReviewerFollowup(pr, comments, "alex") {
|
||||||
|
t.Errorf("審核者且最後留言非自己 → 應需跟進")
|
||||||
|
}
|
||||||
|
if NeedsReviewerFollowup(pr, comments, "max") {
|
||||||
|
t.Errorf("作者本人 → 不應列入審核清單")
|
||||||
|
}
|
||||||
|
if NeedsReviewerFollowup(pr, []Comment{{CreatedAt: "a", User: mkUser("alex")}}, "alex") {
|
||||||
|
t.Errorf("最後留言是自己 → 不應需跟進")
|
||||||
|
}
|
||||||
|
if NeedsReviewerFollowup(pr, comments, "ceo") {
|
||||||
|
t.Errorf("非審核者 → 不應需跟進")
|
||||||
|
}
|
||||||
|
// 尚無留言的 PR:審核者仍應被列入(含尚無留言)。
|
||||||
|
if !NeedsReviewerFollowup(pr, nil, "queena") {
|
||||||
|
t.Errorf("尚無留言 → 審核者應需跟進")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAssigneeNames(t *testing.T) {
|
||||||
|
issue := &Issue{
|
||||||
|
Assignee: mkUser("alex"),
|
||||||
|
Assignees: []User{*mkUser("alex"), *mkUser("max")},
|
||||||
|
}
|
||||||
|
if got := AssigneeNames(issue); !reflect.DeepEqual(got, []string{"alex", "max"}) {
|
||||||
|
t.Errorf("assignee 應去重排序,得到 %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAssigneeOverlapsEmptyMembers(t *testing.T) {
|
||||||
|
issue := &Issue{Assignee: mkUser("alex")}
|
||||||
|
if AssigneeOverlaps(issue, nil) {
|
||||||
|
t.Errorf("成員集為空 → 不應列入(同 gitea.py filter_issues_by_assignee)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLastCommentNotByAssignee(t *testing.T) {
|
||||||
|
issue := &Issue{Assignee: mkUser("alex")}
|
||||||
|
if !LastCommentNotByAssignee(issue, nil) {
|
||||||
|
t.Errorf("無留言 → 應視為需要處理")
|
||||||
|
}
|
||||||
|
if LastCommentNotByAssignee(issue, []Comment{{CreatedAt: "a", User: mkUser("alex")}}) {
|
||||||
|
t.Errorf("最後留言來自 assignee → 不應列入")
|
||||||
|
}
|
||||||
|
if !LastCommentNotByAssignee(issue, []Comment{{CreatedAt: "a", User: mkUser("ceo")}}) {
|
||||||
|
t.Errorf("最後留言來自他人 → 應列入")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUpdatedKeyFallbackChain(t *testing.T) {
|
||||||
|
i := &Issue{UpdatedAt: "", Updated: "u", CreatedAt: "c"}
|
||||||
|
if got := i.updatedKey(); got != "u" {
|
||||||
|
t.Errorf("updated 回退鏈,得到 %q", got)
|
||||||
|
}
|
||||||
|
if got := (&Issue{}).updatedKey(); got != "9999" {
|
||||||
|
t.Errorf("全缺 → 9999,得到 %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFullNameFallbacks(t *testing.T) {
|
||||||
|
pr := &PullRequest{
|
||||||
|
HTMLURL: "https://gitea.alterminal.com/alterminal/teai/pulls/2",
|
||||||
|
Base: &PRRef{Repo: &Repository{FullName: "alterminal/teai"}},
|
||||||
|
}
|
||||||
|
if got := pr.FullName(); got != "alterminal/teai" {
|
||||||
|
t.Errorf("base.repo 優先,得到 %q", got)
|
||||||
|
}
|
||||||
|
pr2 := &PullRequest{HTMLURL: "https://gitea.alterminal.com/alterminal/teai/pulls/2"}
|
||||||
|
if got := pr2.FullName(); got != "alterminal/teai" {
|
||||||
|
t.Errorf("html_url 回退,得到 %q", got)
|
||||||
|
}
|
||||||
|
i := &Issue{Repository: &Repository{FullName: "alterminal/agents"}}
|
||||||
|
if got := i.FullName(); got != "alterminal/agents" {
|
||||||
|
t.Errorf("repository 欄位,得到 %q", got)
|
||||||
|
}
|
||||||
|
if got := (&Issue{}).FullName(); got != "" {
|
||||||
|
t.Errorf("全缺 → 空,得到 %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// errIncompatibleFlags 對應 gitea.py 的「--mine 與 --reviewer 不能併用」。
|
||||||
|
var errIncompatibleFlags = errors.New("--mine 與 --reviewer 不能併用")
|
||||||
|
|
||||||
|
// Work 是 next 命令的輸出物件:Type 為 "pull" 或 "issue";Role 僅 pull 帶
|
||||||
|
// ("author" 或 "reviewer"),對應 gitea.py 的 _work_dict。
|
||||||
|
type Work struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Role string `json:"role,omitempty"`
|
||||||
|
Repo string `json:"repo"`
|
||||||
|
Number int64 `json:"number"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// issueNumber 回傳 issue 編號:number 缺漏時回退 index(gitea.py 語義)。
|
||||||
|
func (i *Issue) issueNumber() int64 {
|
||||||
|
if i.Number != 0 {
|
||||||
|
return i.Number
|
||||||
|
}
|
||||||
|
return i.Index
|
||||||
|
}
|
||||||
|
|
||||||
|
// Num 是 issueNumber 的公開介面(CLI 輸出用)。
|
||||||
|
func (i *Issue) Num() int64 { return i.issueNumber() }
|
||||||
|
|
||||||
|
// Next 回傳下一件未完成工作:先自己發起的待跟進 PR(role=author),再待審核
|
||||||
|
// 的 PR(role=reviewer,含尚無留言),再分派給自己的 open issue。各層皆以
|
||||||
|
// 「最久未更新優先」排序後取第一件(gitea.py 的 find_next_work)。
|
||||||
|
// 沒有任何工作時回傳 nil。
|
||||||
|
func (s *Scanner) Next() (*Work, error) {
|
||||||
|
username, err := s.Whoami()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_ = username
|
||||||
|
|
||||||
|
// 第一層:自己發起、最後一則留言不是自己的 open PR。
|
||||||
|
minePulls, err := s.Pulls(true, false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(minePulls) > 0 {
|
||||||
|
sorted := sortPullItemsByUpdated(minePulls)
|
||||||
|
return workFromPull(sorted[0], "author"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第二層:自己是審核者、最後一則留言不是自己的 open PR。
|
||||||
|
reviewerPulls, err := s.Pulls(false, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(reviewerPulls) > 0 {
|
||||||
|
sorted := sortPullItemsByUpdated(reviewerPulls)
|
||||||
|
return workFromPull(sorted[0], "reviewer"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 第三層:分派給自己、最後一則留言不是 assignee 的 open issue。
|
||||||
|
issues, err := s.Mine()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(issues) > 0 {
|
||||||
|
sorted := sortIssuesByUpdated(issues)
|
||||||
|
return workFromIssue(&sorted[0]), nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sortPullItemsByUpdated 依 updatedKey 升冪(最久未更新在前)穩定排序。
|
||||||
|
func sortPullItemsByUpdated(items []PullItem) []PullItem {
|
||||||
|
out := append([]PullItem(nil), items...)
|
||||||
|
sort.SliceStable(out, func(a, b int) bool {
|
||||||
|
return out[a].Pull.updatedKey() < out[b].Pull.updatedKey()
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// sortIssuesByUpdated 依 updatedKey 升冪(最久未更新在前)穩定排序。
|
||||||
|
func sortIssuesByUpdated(items []Issue) []Issue {
|
||||||
|
out := append([]Issue(nil), items...)
|
||||||
|
sort.SliceStable(out, func(a, b int) bool {
|
||||||
|
return out[a].updatedKey() < out[b].updatedKey()
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// workFromPull 由 PR 組出 Work 物件。
|
||||||
|
func workFromPull(item PullItem, role string) *Work {
|
||||||
|
return &Work{
|
||||||
|
Type: "pull",
|
||||||
|
Role: role,
|
||||||
|
Repo: item.Owner + "/" + item.Repo,
|
||||||
|
Number: item.Pull.Number,
|
||||||
|
Title: item.Pull.Title,
|
||||||
|
URL: item.Pull.WebURL(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// workFromIssue 由 issue 組出 Work 物件。
|
||||||
|
func workFromIssue(issue *Issue) *Work {
|
||||||
|
return &Work{
|
||||||
|
Type: "issue",
|
||||||
|
Repo: issue.FullName(),
|
||||||
|
Number: issue.issueNumber(),
|
||||||
|
Title: issue.Title,
|
||||||
|
URL: issue.WebURL(),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,351 @@
|
|||||||
|
// Package workflow 實作 AGENTS.md 工作流判定:跟進、審核、next 拾取與排序。
|
||||||
|
//
|
||||||
|
// 行為基準是 alterminal/agents 的 scripts/lib/gitea.py;判定規則以 AGENTS.md
|
||||||
|
// 為唯一來源,本套件只實作、不改義。API 存取以 Client 介面抽象,HTTP 實作
|
||||||
|
// 由 internal/gitea(#5 基礎建設)提供;測試以假 client 注入。
|
||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AgentsTeamName 是工作流團隊的名稱(AGENTS.md:調度成員由 Agents 團隊決定)。
|
||||||
|
const AgentsTeamName = "Agents"
|
||||||
|
|
||||||
|
// Client 抽象 teai 需要的 Gitea API 讀取操作(僅唯讀)。
|
||||||
|
// #5 的 internal/gitea HTTP 用戶端實作此介面;workflow 不依賴 HTTP 細節。
|
||||||
|
type Client interface {
|
||||||
|
// CurrentUser 回傳目前帳號名(GET /user)。
|
||||||
|
CurrentUser() (string, error)
|
||||||
|
// ListOrgs 回傳帳號所屬組織名(GET /user/orgs,分頁)。
|
||||||
|
ListOrgs() ([]string, error)
|
||||||
|
// ListTeams 回傳組織的團隊清單(GET /orgs/{org}/teams,分頁)。
|
||||||
|
ListTeams(org string) ([]Team, error)
|
||||||
|
// ListTeamMembers 回傳團隊成員帳號(GET /teams/{id}/members,分頁)。
|
||||||
|
ListTeamMembers(teamID int64) ([]string, error)
|
||||||
|
// ListOrgRepos 回傳組織的倉庫物件(GET /orgs/{org}/repos,分頁)。
|
||||||
|
ListOrgRepos(org string) ([]Repo, error)
|
||||||
|
// ListIssues 回傳倉庫的 open issues(GET /repos/{owner}/{repo}/issues?state=open&type=issues,
|
||||||
|
// 分頁;已排除帶 pull_request 標記的 PR)。assignee/留言過濾由 workflow 層負責。
|
||||||
|
ListIssues(owner, repo string) ([]Issue, error)
|
||||||
|
// ListPulls 回傳倉庫的 open PR(GET /repos/{owner}/{repo}/pulls?state=open,分頁)。
|
||||||
|
ListPulls(owner, repo string) ([]PullRequest, error)
|
||||||
|
// ListComments 回傳 issue/PR 的留言(GET /repos/{owner}/{repo}/issues/{n}/comments,分頁)。
|
||||||
|
ListComments(owner, repo string, number int64) ([]Comment, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RepoPulls 是單一倉庫的 open PR 清單(掃描的中間產物)。
|
||||||
|
type RepoPulls struct {
|
||||||
|
Owner string
|
||||||
|
Repo string
|
||||||
|
Pulls []PullRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
// PullItem 是跟進清單中的 PR,附掃描時的所屬倉庫(owner/repo),
|
||||||
|
// 供輸出時補齊 gitea.py 工作物件的 repo 欄位。
|
||||||
|
type PullItem struct {
|
||||||
|
Owner string
|
||||||
|
Repo string
|
||||||
|
Pull PullRequest
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scanner 在 Client 之上組合出各命令的資料(whoami/orgs/members/mine/pulls/next)。
|
||||||
|
type Scanner struct {
|
||||||
|
Client Client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Whoami 回傳目前帳號。
|
||||||
|
func (s *Scanner) Whoami() (string, error) {
|
||||||
|
return s.Client.CurrentUser()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Orgs 回傳目前帳號所屬的組織(去重、排序)。
|
||||||
|
func (s *Scanner) Orgs() ([]string, error) {
|
||||||
|
names, err := s.Client.ListOrgs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
seen := map[string]bool{}
|
||||||
|
out := []string{}
|
||||||
|
for _, n := range names {
|
||||||
|
if n != "" && !seen[n] {
|
||||||
|
seen[n] = true
|
||||||
|
out = append(out, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(out)
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// agentsTeamID 找出組織內名為 Agents 的團隊 ID;沒有回 (0,false)。
|
||||||
|
func (s *Scanner) agentsTeamID(org string) (int64, bool, error) {
|
||||||
|
teams, err := s.Client.ListTeams(org)
|
||||||
|
if err != nil {
|
||||||
|
return 0, false, err
|
||||||
|
}
|
||||||
|
for _, team := range teams {
|
||||||
|
if team.Name == AgentsTeamName {
|
||||||
|
return team.ID, true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Members 回傳跨組織 Agents 團隊成員(去重、排序)。
|
||||||
|
// hasWork 為 true 時只保留有未完成工作者(判定同 Next 的拾取規則)。
|
||||||
|
func (s *Scanner) Members(hasWork bool) ([]string, error) {
|
||||||
|
orgs, err := s.Orgs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
seen := map[string]bool{}
|
||||||
|
names := []string{}
|
||||||
|
for _, org := range orgs {
|
||||||
|
teamID, ok, err := s.agentsTeamID(org)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
members, err := s.Client.ListTeamMembers(teamID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, m := range members {
|
||||||
|
if m != "" && !seen[m] {
|
||||||
|
seen[m] = true
|
||||||
|
names = append(names, m)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
if !hasWork {
|
||||||
|
return names, nil
|
||||||
|
}
|
||||||
|
busy, err := s.usernamesWithUnfinishedWork(names)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := make([]string, 0, len(names))
|
||||||
|
for _, name := range names {
|
||||||
|
if busy[name] {
|
||||||
|
out = append(out, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterOrgRepos 產出 (org, repoName);空倉庫跳過(沒有內容可掃)。
|
||||||
|
func (s *Scanner) iterOrgRepos() ([][2]string, error) {
|
||||||
|
orgs, err := s.Orgs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := [][2]string{}
|
||||||
|
for _, org := range orgs {
|
||||||
|
repos, err := s.Client.ListOrgRepos(org)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, repo := range repos {
|
||||||
|
if repo.Name == "" || repo.Empty {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, [2]string{org, repo.Name})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterOpenPulls 跨倉庫收集 open PR(附所屬 owner/repo)。
|
||||||
|
// 對應 gitea.py 的 _iter_open_pulls:空倉庫、明確停用 PR 的倉庫跳過。
|
||||||
|
func (s *Scanner) iterOpenPulls() ([]RepoPulls, error) {
|
||||||
|
orgs, err := s.Orgs()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := []RepoPulls{}
|
||||||
|
for _, org := range orgs {
|
||||||
|
repos, err := s.Client.ListOrgRepos(org)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, repo := range repos {
|
||||||
|
if repo.Name == "" || repo.Empty {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if repo.HasPullRequests != nil && !*repo.HasPullRequests {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pulls, err := s.Client.ListPulls(org, repo.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(pulls) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, RepoPulls{Owner: org, Repo: repo.Name, Pulls: pulls})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// orgIssue 是掃描到的 issue,附所屬 owner/repo。
|
||||||
|
type orgIssue struct {
|
||||||
|
Owner string
|
||||||
|
Repo string
|
||||||
|
Issue Issue
|
||||||
|
}
|
||||||
|
|
||||||
|
// collectMemberIssues 跨組織收集分派給 members(至少一人)且最後一則留言
|
||||||
|
// 不是 assignee 的 open issues(gitea.py 的 for_all_organizations)。
|
||||||
|
func (s *Scanner) collectMemberIssues(members []string) ([]orgIssue, error) {
|
||||||
|
pairs, err := s.iterOrgRepos()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := []orgIssue{}
|
||||||
|
for _, pair := range pairs {
|
||||||
|
owner, repo := pair[0], pair[1]
|
||||||
|
issues, err := s.Client.ListIssues(owner, repo)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for i := range issues {
|
||||||
|
issue := &issues[i]
|
||||||
|
if !AssigneeOverlaps(issue, members) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
comments, err := s.Client.ListComments(owner, repo, issue.issueNumber())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !LastCommentNotByAssignee(issue, comments) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, orgIssue{Owner: owner, Repo: repo, Issue: *issue})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mine 回傳分派給目前帳號、且最後一則留言不是 assignee 的 open issues
|
||||||
|
// (跨所屬組織;gitea.py 的 list_my_followup_issues)。
|
||||||
|
func (s *Scanner) Mine() ([]Issue, error) {
|
||||||
|
username, err := s.Whoami()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
found, err := s.collectMemberIssues([]string{username})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := make([]Issue, 0, len(found))
|
||||||
|
for _, item := range found {
|
||||||
|
out = append(out, item.Issue)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pulls 回傳跟進清單(跨倉庫;gitea.py 的 list_followup_pulls 系列):
|
||||||
|
// - 預設:「有留言且最後一則不是作者」的 open PR。
|
||||||
|
// - mine=true:再限定作者是目前帳號(list_my_followup_pulls)。
|
||||||
|
// - reviewer=true:改為「自己是 requested reviewer、不是作者、最後一則
|
||||||
|
// 留言不是自己(含尚無留言)」(list_my_reviewer_pulls)。
|
||||||
|
//
|
||||||
|
// mine 與 reviewer 不可併用。
|
||||||
|
func (s *Scanner) Pulls(mine, reviewer bool) ([]PullItem, error) {
|
||||||
|
if mine && reviewer {
|
||||||
|
return nil, errIncompatibleFlags
|
||||||
|
}
|
||||||
|
username := ""
|
||||||
|
if mine || reviewer {
|
||||||
|
var err error
|
||||||
|
username, err = s.Whoami()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
repoPulls, err := s.iterOpenPulls()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
found := []PullItem{}
|
||||||
|
for _, rp := range repoPulls {
|
||||||
|
for i := range rp.Pulls {
|
||||||
|
pr := &rp.Pulls[i]
|
||||||
|
comments, err := s.Client.ListComments(rp.Owner, rp.Repo, pr.Number)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var match bool
|
||||||
|
switch {
|
||||||
|
case reviewer:
|
||||||
|
match = NeedsReviewerFollowup(pr, comments, username)
|
||||||
|
case mine:
|
||||||
|
match = NeedsAuthorFollowup(pr, comments) && pr.Author() == username
|
||||||
|
default:
|
||||||
|
match = NeedsAuthorFollowup(pr, comments)
|
||||||
|
}
|
||||||
|
if match {
|
||||||
|
found = append(found, PullItem{Owner: rp.Owner, Repo: rp.Repo, Pull: *pr})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return found, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// usernamesWithUnfinishedWork 從待跟進 issue/PR 清單收集有未完成工作的
|
||||||
|
// 成員帳號(gitea.py 的 _usernames_with_unfinished_work;判定同 Next)。
|
||||||
|
func (s *Scanner) usernamesWithUnfinishedWork(members []string) (map[string]bool, error) {
|
||||||
|
memberSet := map[string]bool{}
|
||||||
|
for _, m := range members {
|
||||||
|
if m != "" {
|
||||||
|
memberSet[m] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
busy := map[string]bool{}
|
||||||
|
if len(memberSet) == 0 {
|
||||||
|
return busy, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// issues:分派給任一成員、最後留言不是 assignee。
|
||||||
|
issues, err := s.collectMemberIssues(members)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, item := range issues {
|
||||||
|
for _, name := range AssigneeNames(&item.Issue) {
|
||||||
|
if memberSet[name] {
|
||||||
|
busy[name] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PR:作者待跟進/審核者待審。
|
||||||
|
repoPulls, err := s.iterOpenPulls()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, rp := range repoPulls {
|
||||||
|
for i := range rp.Pulls {
|
||||||
|
pr := &rp.Pulls[i]
|
||||||
|
comments, err := s.Client.ListComments(rp.Owner, rp.Repo, pr.Number)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if NeedsAuthorFollowup(pr, comments) {
|
||||||
|
if author := pr.Author(); memberSet[author] {
|
||||||
|
busy[author] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, reviewer := range ReviewerNames(pr) {
|
||||||
|
if memberSet[reviewer] && NeedsReviewerFollowup(pr, comments, reviewer) {
|
||||||
|
busy[reviewer] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return busy, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,261 @@
|
|||||||
|
// scan_test.go — 以假 client 驗證 Scanner 的命令層語義(對照 gitea.py):
|
||||||
|
// next 三層拾取與「最久未更新優先」、mine/pulls 過濾、members --has-work。
|
||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakeClient 以記憶體資料實作 Client。
|
||||||
|
type fakeClient struct {
|
||||||
|
currentUser string
|
||||||
|
orgs []Org
|
||||||
|
teams map[string][]Team // org → teams
|
||||||
|
teamMembers map[int64][]string // teamID → 帳號
|
||||||
|
repos map[string][]Repo // org → repos
|
||||||
|
issues map[string][]Issue // "owner/repo" → issues(open,不含 PR)
|
||||||
|
pulls map[string][]PullRequest // "owner/repo" → open PR
|
||||||
|
comments map[string][]Comment // "owner/repo#number" → 留言
|
||||||
|
failOn map[string]error // 端點 → 錯誤(測失敗傳播)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) CurrentUser() (string, error) {
|
||||||
|
if err := f.failOn["user"]; err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return f.currentUser, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListOrgs() ([]string, error) {
|
||||||
|
if err := f.failOn["orgs"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
out := make([]string, 0, len(f.orgs))
|
||||||
|
for _, o := range f.orgs {
|
||||||
|
out = append(out, o.Account())
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListTeams(org string) ([]Team, error) {
|
||||||
|
if err := f.failOn["teams"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.teams[org], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListTeamMembers(teamID int64) ([]string, error) {
|
||||||
|
if err := f.failOn["members"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.teamMembers[teamID], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListOrgRepos(org string) ([]Repo, error) {
|
||||||
|
if err := f.failOn["repos"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.repos[org], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListIssues(owner, repo string) ([]Issue, error) {
|
||||||
|
if err := f.failOn["issues"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.issues[owner+"/"+repo], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListPulls(owner, repo string) ([]PullRequest, error) {
|
||||||
|
if err := f.failOn["pulls"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.pulls[owner+"/"+repo], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakeClient) ListComments(owner, repo string, number int64) ([]Comment, error) {
|
||||||
|
if err := f.failOn["comments"]; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return f.comments[fmt.Sprintf("%s/%s#%d", owner, repo, number)], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newTestScanner 構造測試情境:一個組織、一個 Agents 團隊、兩個倉庫。
|
||||||
|
func newTestScanner() (*Scanner, *fakeClient) {
|
||||||
|
fc := &fakeClient{
|
||||||
|
currentUser: "alex",
|
||||||
|
orgs: []Org{{UserName: "alterminal"}},
|
||||||
|
teams: map[string][]Team{"alterminal": {{ID: 1, Name: "Agents"}}},
|
||||||
|
teamMembers: map[int64][]string{1: {"alex", "max", "queena"}},
|
||||||
|
repos: map[string][]Repo{"alterminal": {{Name: "teai"}, {Name: "agents"}}},
|
||||||
|
issues: map[string][]Issue{},
|
||||||
|
pulls: map[string][]PullRequest{},
|
||||||
|
comments: map[string][]Comment{},
|
||||||
|
failOn: map[string]error{},
|
||||||
|
}
|
||||||
|
return &Scanner{Client: fc}, fc
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextLayerPriority(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
// 三層各有工作:author PR、reviewer PR、issue。
|
||||||
|
fc.pulls["alterminal/teai"] = []PullRequest{
|
||||||
|
{
|
||||||
|
Number: 10, Title: "我的 PR", UpdatedAt: "2026-09-09T00:00:00Z",
|
||||||
|
User: mkUser("alex"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Number: 11, Title: "待我審核的 PR", UpdatedAt: "2026-09-09T01:00:00Z",
|
||||||
|
User: mkUser("max"),
|
||||||
|
RequestedReviewers: []User{*mkUser("alex")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// author PR #10 有他人留言 → 需跟進。
|
||||||
|
fc.comments["alterminal/teai#10"] = []Comment{
|
||||||
|
{CreatedAt: "2026-09-09T00:10:00Z", User: mkUser("ceo")},
|
||||||
|
}
|
||||||
|
// reviewer PR #11 尚無留言 → 審核者仍應被列入。
|
||||||
|
fc.issues["alterminal/agents"] = []Issue{
|
||||||
|
{Number: 30, Title: "分派給 alex", UpdatedAt: "2026-09-08T00:00:00Z", Assignees: []User{*mkUser("alex")}},
|
||||||
|
}
|
||||||
|
work, err := s.Next()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if work.Type != "pull" || work.Role != "author" || work.Number != 10 {
|
||||||
|
t.Errorf("第一層應選 author PR #10,得到 %+v", work)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextLayerFallbackToReviewer(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
fc.pulls["alterminal/teai"] = []PullRequest{
|
||||||
|
{
|
||||||
|
Number: 11, Title: "待審核", UpdatedAt: "2026-09-09T01:00:00Z",
|
||||||
|
User: mkUser("max"),
|
||||||
|
RequestedReviewers: []User{*mkUser("alex")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fc.issues["alterminal/agents"] = []Issue{
|
||||||
|
{Number: 30, Title: "分派給 alex", UpdatedAt: "2026-09-08T00:00:00Z", Assignees: []User{*mkUser("alex")}},
|
||||||
|
}
|
||||||
|
work, err := s.Next()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if work.Type != "pull" || work.Role != "reviewer" || work.Number != 11 {
|
||||||
|
t.Errorf("第二層應選 reviewer PR #11,得到 %+v", work)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextLayerFallbackToIssue(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
fc.issues["alterminal/agents"] = []Issue{
|
||||||
|
{Number: 30, Title: "分派給 alex", UpdatedAt: "2026-09-08T00:00:00Z", Assignees: []User{*mkUser("alex")}},
|
||||||
|
}
|
||||||
|
work, err := s.Next()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if work.Type != "issue" || work.Number != 30 {
|
||||||
|
t.Errorf("第三層應選 issue #30,得到 %+v", work)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextNone(t *testing.T) {
|
||||||
|
s, _ := newTestScanner()
|
||||||
|
work, err := s.Next()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if work != nil {
|
||||||
|
t.Errorf("無工作應回 nil,得到 %+v", work)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNextOldestFirst(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
fc.issues["alterminal/agents"] = []Issue{
|
||||||
|
{Number: 31, Title: "較新", UpdatedAt: "2026-09-09T00:00:00Z", Assignees: []User{*mkUser("alex")}},
|
||||||
|
{Number: 30, Title: "較舊", UpdatedAt: "2026-09-08T00:00:00Z", Assignees: []User{*mkUser("alex")}},
|
||||||
|
}
|
||||||
|
work, err := s.Next()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if work.Number != 30 {
|
||||||
|
t.Errorf("應選最久未更新的 #30,得到 %+v", work)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMineSkipsLastCommentByAssignee(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
fc.issues["alterminal/agents"] = []Issue{
|
||||||
|
{Number: 30, Title: "已在處理", UpdatedAt: "a", Assignees: []User{*mkUser("alex")}},
|
||||||
|
}
|
||||||
|
fc.comments["alterminal/agents#30"] = []Comment{
|
||||||
|
{CreatedAt: "b", User: mkUser("alex")},
|
||||||
|
}
|
||||||
|
issues, err := s.Mine()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(issues) != 0 {
|
||||||
|
t.Errorf("最後留言來自 assignee → 應略過,得到 %d 件", len(issues))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPullsMineReviewersExclusive(t *testing.T) {
|
||||||
|
s, _ := newTestScanner()
|
||||||
|
if _, err := s.Pulls(true, true); !errors.Is(err, errIncompatibleFlags) {
|
||||||
|
t.Errorf("--mine 與 --reviewer 併用應回 errIncompatibleFlags,得到 %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMembersHasWork(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
// alex 有待審核 PR;max 無任何工作;queena 有分派 issue。
|
||||||
|
fc.pulls["alterminal/teai"] = []PullRequest{
|
||||||
|
{
|
||||||
|
Number: 11, UpdatedAt: "a", User: mkUser("max"),
|
||||||
|
RequestedReviewers: []User{*mkUser("alex")},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fc.issues["alterminal/agents"] = []Issue{
|
||||||
|
{Number: 30, UpdatedAt: "b", Assignees: []User{*mkUser("queena")}},
|
||||||
|
}
|
||||||
|
got, err := s.Members(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(got, []string{"alex", "queena"}) {
|
||||||
|
t.Errorf("應只有 alex 與 queena 有工作,得到 %v", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrorPropagation(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
fc.failOn["orgs"] = errors.New("boom")
|
||||||
|
if _, err := s.Next(); err == nil {
|
||||||
|
t.Errorf("API 錯誤應向上傳播,不應吞掉")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPullsDefault(t *testing.T) {
|
||||||
|
s, fc := newTestScanner()
|
||||||
|
fc.pulls["alterminal/teai"] = []PullRequest{
|
||||||
|
{Number: 10, UpdatedAt: "a", User: mkUser("alex")},
|
||||||
|
}
|
||||||
|
fc.comments["alterminal/teai#10"] = []Comment{
|
||||||
|
{CreatedAt: "b", User: mkUser("ceo")},
|
||||||
|
}
|
||||||
|
items, err := s.Pulls(false, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(items) != 1 || items[0].Repo != "teai" {
|
||||||
|
t.Errorf("應列出待跟進 PR,得到 %+v", items)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
package workflow
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// User 是 Gitea API 的使用者物件(僅取工作流判斷會用到的欄位)。
|
||||||
|
type User struct {
|
||||||
|
UserName string `json:"username"`
|
||||||
|
Login string `json:"login"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name 回傳帳號名:先 username,缺漏時回退 login
|
||||||
|
// (對應 gitea.py 的「username or login」取值順序)。
|
||||||
|
func (u *User) Name() string {
|
||||||
|
if u == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if u.UserName != "" {
|
||||||
|
return u.UserName
|
||||||
|
}
|
||||||
|
return u.Login
|
||||||
|
}
|
||||||
|
|
||||||
|
// Org 是帳號所屬的組織物件。
|
||||||
|
type Org struct {
|
||||||
|
UserName string `json:"username"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Account 回傳組織名:先 username,缺漏時回退 name
|
||||||
|
// (對應 gitea.py 的取值順序)。
|
||||||
|
func (o Org) Account() string {
|
||||||
|
if o.UserName != "" {
|
||||||
|
return o.UserName
|
||||||
|
}
|
||||||
|
return o.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
// Team 是組織內的團隊物件。
|
||||||
|
type Team struct {
|
||||||
|
ID int64 `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repo 是組織倉庫清單中的倉庫物件。Empty 與 HasPullRequests
|
||||||
|
// 對應 gitea.py _iter_open_pulls 的跳過條件:空倉庫、明確停用 PR
|
||||||
|
// (has_pull_requests 為 false)者不列入 PR 掃描。
|
||||||
|
type Repo struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Empty bool `json:"empty"`
|
||||||
|
HasPullRequests *bool `json:"has_pull_requests"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repository 是 issue/PR 物件內嵌的所屬倉庫資訊。
|
||||||
|
type Repository struct {
|
||||||
|
FullName string `json:"full_name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PRRef 是 PR 的 base/head 端點,含所屬倉庫。
|
||||||
|
type PRRef struct {
|
||||||
|
Repo *Repository `json:"repo"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Comment 是 issue/PR 的討論留言(PR 的留言也走 issues comments 端點)。
|
||||||
|
type Comment struct {
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
|
User *User `json:"user"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Author 回傳留言者帳號;空字串表示未知。
|
||||||
|
func (c *Comment) Author() string { return c.User.Name() }
|
||||||
|
|
||||||
|
// pullMarker 標記 /issues 端點回傳的物件其實是 PR
|
||||||
|
// (Gitea 只會在 PR 上附帶 pull_request 欄位;issue 沒有此欄位)。
|
||||||
|
type pullMarker struct{}
|
||||||
|
|
||||||
|
// Issue 是 issue 物件。/issues 端點也會以此形狀回傳 PR(帶非 nil 的
|
||||||
|
// PullRequest 標記),判斷時一律排除(同 gitea.py list_issues_for_repository)。
|
||||||
|
type Issue struct {
|
||||||
|
Number int64 `json:"number"`
|
||||||
|
Index int64 `json:"index"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
HTMLURL string `json:"html_url"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
UpdatedAt string `json:"updated_at"`
|
||||||
|
Updated string `json:"updated"`
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
|
Created string `json:"created"`
|
||||||
|
User *User `json:"user"`
|
||||||
|
Assignee *User `json:"assignee"`
|
||||||
|
Assignees []User `json:"assignees"`
|
||||||
|
PullRequest *pullMarker `json:"pull_request"`
|
||||||
|
Repository *Repository `json:"repository"`
|
||||||
|
|
||||||
|
// Raw 是本次回應的完整原始 JSON。清單類命令(mine/pulls)必須原樣輸出
|
||||||
|
// (gitea.py 直接傾印 API 原始物件),作為交叉驗證一致的基礎。
|
||||||
|
Raw json.RawMessage `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 解碼各判斷欄位,並保留原始 JSON 供原樣輸出。
|
||||||
|
func (i *Issue) UnmarshalJSON(data []byte) error {
|
||||||
|
type issueAlias Issue
|
||||||
|
var a issueAlias
|
||||||
|
if err := json.Unmarshal(data, &a); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*i = Issue(a)
|
||||||
|
i.Raw = append(json.RawMessage(nil), data...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// updatedKey 對應 gitea.py 的 _updated_at 回退鏈
|
||||||
|
// (updated_at → updated → created_at → created → "9999"),
|
||||||
|
// 供「最久未更新優先」排序;ISO8601 字串字典序與時間序一致。
|
||||||
|
func (i *Issue) updatedKey() string {
|
||||||
|
for _, v := range []string{i.UpdatedAt, i.Updated, i.CreatedAt, i.Created} {
|
||||||
|
if v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "9999"
|
||||||
|
}
|
||||||
|
|
||||||
|
// WebURL 回傳網頁網址:先 html_url,缺漏時回退 API url。
|
||||||
|
func (i *Issue) WebURL() string {
|
||||||
|
if i.HTMLURL != "" {
|
||||||
|
return i.HTMLURL
|
||||||
|
}
|
||||||
|
return i.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// PullRequest 是 pull request 物件(僅取判斷所需欄位)。
|
||||||
|
type PullRequest struct {
|
||||||
|
Number int64 `json:"number"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
HTMLURL string `json:"html_url"`
|
||||||
|
URL string `json:"url"`
|
||||||
|
UpdatedAt string `json:"updated_at"`
|
||||||
|
Updated string `json:"updated"`
|
||||||
|
CreatedAt string `json:"created_at"`
|
||||||
|
Created string `json:"created"`
|
||||||
|
User *User `json:"user"`
|
||||||
|
RequestedReviewers []User `json:"requested_reviewers"`
|
||||||
|
Base *PRRef `json:"base"`
|
||||||
|
Head *PRRef `json:"head"`
|
||||||
|
Repository *Repository `json:"repository"`
|
||||||
|
|
||||||
|
// Raw 意義同 Issue.Raw。
|
||||||
|
Raw json.RawMessage `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalJSON 解碼各判斷欄位,並保留原始 JSON 供原樣輸出。
|
||||||
|
func (p *PullRequest) UnmarshalJSON(data []byte) error {
|
||||||
|
type pullAlias PullRequest
|
||||||
|
var a pullAlias
|
||||||
|
if err := json.Unmarshal(data, &a); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*p = PullRequest(a)
|
||||||
|
p.Raw = append(json.RawMessage(nil), data...)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// updatedKey 意義同 Issue.updatedKey。
|
||||||
|
func (p *PullRequest) updatedKey() string {
|
||||||
|
for _, v := range []string{p.UpdatedAt, p.Updated, p.CreatedAt, p.Created} {
|
||||||
|
if v != "" {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "9999"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Author 回傳 PR 發起者帳號(user 欄位)。
|
||||||
|
func (p *PullRequest) Author() string { return p.User.Name() }
|
||||||
|
|
||||||
|
// WebURL 回傳網頁網址:先 html_url,缺漏時回退 API url。
|
||||||
|
func (p *PullRequest) WebURL() string {
|
||||||
|
if p.HTMLURL != "" {
|
||||||
|
return p.HTMLURL
|
||||||
|
}
|
||||||
|
return p.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
// htmlURLRepoRe 從網頁網址推回 "owner/repo"
|
||||||
|
// (gitea.py _full_name_of 的 html_url regex 回退)。
|
||||||
|
var htmlURLRepoRe = regexp.MustCompile(`/([^/]+/[^/]+)/(?:issues|pulls)/[0-9]+`)
|
||||||
|
|
||||||
|
func repoFullNameFromURL(webURL string) string {
|
||||||
|
if m := htmlURLRepoRe.FindStringSubmatch(webURL); m != nil {
|
||||||
|
return m[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// FullName 從 issue 物件推回 "owner/repo":先 repository 欄位,再以網頁網址回退。
|
||||||
|
func (i *Issue) FullName() string {
|
||||||
|
if i.Repository != nil && i.Repository.FullName != "" {
|
||||||
|
return i.Repository.FullName
|
||||||
|
}
|
||||||
|
return repoFullNameFromURL(i.WebURL())
|
||||||
|
}
|
||||||
|
|
||||||
|
// FullName 從 PR 物件推回 "owner/repo":repository → base.repo → head.repo →
|
||||||
|
// 網頁網址回退(Gitea 的 /pulls 回應通常沒有 repository 欄位)。
|
||||||
|
func (p *PullRequest) FullName() string {
|
||||||
|
if p.Repository != nil && p.Repository.FullName != "" {
|
||||||
|
return p.Repository.FullName
|
||||||
|
}
|
||||||
|
for _, ref := range []*PRRef{p.Base, p.Head} {
|
||||||
|
if ref != nil && ref.Repo != nil && ref.Repo.FullName != "" {
|
||||||
|
return ref.Repo.FullName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return repoFullNameFromURL(p.WebURL())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user