package app import ( "context" "fmt" "strings" c "cnb.cool/cnb/go-cnb/cnb" "cnb.cool/looc/git-cnb/cnb" "cnb.cool/looc/git-cnb/ui" "github.com/spf13/cobra" ) type Issue struct { Type IssueType c.Issue } type IssueType string const ( // TypeToME ... TypeToME IssueType = "->ME" // TypeFromME ... TypeFromME = "ME->" // TypeBoth ... TypeBoth = "ME->ME" ) // FEEDBACKREPO 同时列出同组织下 feedback 仓库的 issue const FEEDBACK = "feedback" var IssuesCmd = &cobra.Command{ Use: "issue", Short: "print issues of this repo", Long: "print issues of this repo", Run: func(cmd *cobra.Command, args []string) { ListIssues(cmd.Context()) }, } func ListIssues(ctx context.Context) { me, err := cnb.Me(ctx, Client) if err != nil { ui.ErrorWithExit(err.Error()) } toMeOpts := &c.IssueListOptions{ State: c.IssueStateOpen, Assignees: me.UserName, } fromMeOpts := &c.IssueListOptions{ State: c.IssueStateOpen, Authors: me.UserName, } issuesToMe, err := cnb.ListIssues(ctx, Client, Repo, toMeOpts) if err != nil { ui.ErrorWithExit(err.Error()) } issuesFromMe, err := cnb.ListIssues(ctx, Client, Repo, fromMeOpts) if err != nil { ui.ErrorWithExit(err.Error()) } table := &ui.Table{ Titles: []ui.TableTitle{}, Items: [][]string{}, } table.Titles = []ui.TableTitle{ {Title: "NUMBER", Width: 15}, {Title: "TITLE", Width: 80}, {Title: "TYPE", Width: 10}, } addIssuesToTable(table, joinIssues(issuesToMe, issuesFromMe), "") // 追加同组织下 FEEDBACKREPO 仓库下属于本人的 issue, 忽略错误 feedbackIssuesToMe, _ := cnb.ListIssues(ctx, Client, getFeedBackRepo(Repo), toMeOpts) feedbackIssuesFromMe, _ := cnb.ListIssues(ctx, Client, getFeedBackRepo(Repo), fromMeOpts) if feedbackIssuesToMe != nil || feedbackIssuesFromMe != nil { addIssuesToTable( table, joinIssues(feedbackIssuesToMe, feedbackIssuesFromMe), FEEDBACK) } table.Print() } func addIssuesToTable(table *ui.Table, issues []*Issue, feedbackRepo string) { for _, issue := range issues { table.Items = append(table.Items, []string{ fmt.Sprintf("%s#%s", feedbackRepo, issue.Number), issue.Title, string(issue.Type)}) } } func joinIssues(toMeIssues []c.Issue, fromMeIssue []c.Issue) []*Issue { issues := []*Issue{} for _, i := range toMeIssues { issues = append(issues, &Issue{ Type: TypeToME, Issue: i, }) } for _, i := range fromMeIssue { bothIssue := findBothIssue(issues, i) if bothIssue != nil { bothIssue.Type = TypeBoth } else { issues = append(issues, &Issue{ Type: TypeFromME, Issue: i, }) } } return issues } func findBothIssue(toMeIssues []*Issue, issue c.Issue) *Issue { for _, i := range toMeIssues { if issue.Number == i.Number { return i } } return nil } func getFeedBackRepo(repo string) string { s := strings.Split(repo, "/") group := strings.Join(s[:len(s)-1], "/") return fmt.Sprintf("%s/%s", group, FEEDBACK) }