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" ) // 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) { List(cmd.Context()) }, } func List(ctx context.Context) { me, err := cnb.Me(ctx, Client) if err != nil { ui.ErrorWithExit(err.Error()) } opts := &c.IssueListOptions{ State: c.IssueStateOpen, Assignees: me.UserName, } issues, err := cnb.ListIssues(ctx, Client, Repo, opts) if err != nil { ui.ErrorWithExit(err.Error()) } table := &ui.Table{ Title: []interface{}{}, Items: [][]interface{}{}, } table.Title = []interface{}{"Number", "Title", "Author", "Assignees", "Priority"} addIssuesToTable(table, issues, "") // 追加同组织下 FEEDBACKREPO 仓库下属于本人的 issue, 忽略错误 feedbackIssues, _ := cnb.ListIssues(ctx, Client, getFeedBackRepo(Repo), opts) if feedbackIssues != nil { addIssuesToTable(table, feedbackIssues, FEEDBACK) } table.Print() } func addIssuesToTable(table *ui.Table, issues []c.Issue, feedbackRepo string) { for _, issue := range issues { assignees := []string{} for _, assigne := range issue.Assignees { assignees = append(assignees, assigne.NickName) } table.Items = append(table.Items, []interface{}{ fmt.Sprintf("%s#%s", feedbackRepo, issue.Number), issue.Title, issue.Author.NickName, strings.Join(assignees, ","), issue.Priority}) } } func getFeedBackRepo(repo string) string { s := strings.Split(repo, "/") group := strings.Join(s[:len(s)-1], "/") return fmt.Sprintf("%s/%s", group, FEEDBACK) }