mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
feat(search): gh-style issue and pull request search tables
search issue and search pr printed rows without a header, showed merged pull requests as closed and never said that more results exist. They now show REPO#ID TITLE STATE AUTHOR UPDATED under a context line that counts the matches and points at the next page, are also search issues and search prs, take -s -L -A -l -m and @me, and use --until for --before, which still works. Search results gain an author JSON field. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
6 files changed
+120
-58
No files matched your search
@@ -15,17 +15,23 @@ import (
|
||||
)
|
||||
|
||||
func (a *application) discussionSearchCommand(pulls bool) *cobra.Command {
|
||||
noun := "issue"
|
||||
noun, nouns, alias := "issue", "issues", "issues"
|
||||
if pulls {
|
||||
noun = "pr"
|
||||
noun, nouns, alias = "pr", "pull requests", "prs"
|
||||
}
|
||||
var opts services.DiscussionSearchOptions
|
||||
var labels []string
|
||||
var updated timeRange
|
||||
opts.Pulls = pulls
|
||||
command := &cobra.Command{Use: noun + " [query]...", Short: "Search " + noun + "s across the selected instance or repository", RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := oneOf("state", opts.State, "open", "closed", "all"); err != nil {
|
||||
return err
|
||||
}
|
||||
example := fmt.Sprintf(`$ tea search %[1]s "login error"
|
||||
$ tea search %[1]s --author @me --state all
|
||||
# Search one repository; its labels and milestones are checked
|
||||
$ tea search %[1]s crash -R alice/project --label bug --milestone v1.0`, noun)
|
||||
if pulls {
|
||||
example += "\n$ tea search pr --review-requested"
|
||||
}
|
||||
command := &cobra.Command{Use: noun + " [<query>...]", Aliases: []string{alias}, Short: "Search " + nouns, Long: fmt.Sprintf(`Search the %[1]s of every repository you can access on the instance, or of one
|
||||
repository with --repo. Words of the query match titles and bodies.`, nouns), Example: example, RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if opts.Author != "" && opts.Authored {
|
||||
return clierr.Wrap(clierr.Usage, errors.New("--author and --authored are mutually exclusive"))
|
||||
}
|
||||
@@ -33,8 +39,7 @@ func (a *application) discussionSearchCommand(pulls bool) *cobra.Command {
|
||||
if opts.Since, opts.Before, err = updated.parse(a.deps.Now()); err != nil {
|
||||
return err
|
||||
}
|
||||
labelNames, err := labelFilter(opts.Labels)
|
||||
if err != nil {
|
||||
if err := requireNames("label", "", labels); err != nil {
|
||||
return err
|
||||
}
|
||||
milestones, err := nameFilter("milestone", opts.Milestones)
|
||||
@@ -54,42 +59,68 @@ func (a *application) discussionSearchCommand(pulls bool) *cobra.Command {
|
||||
if opts.Team != "" && target.Repo != "" {
|
||||
return clierr.Wrap(clierr.Usage, errors.New("--team requires instance-wide search; omit --repo or TEA_REPO"))
|
||||
}
|
||||
opts.Author = target.expandMe(opts.Author)
|
||||
if target.Repo != "" {
|
||||
if opts.Labels, opts.Milestones, err = issueFilters(cmd, target, labelNames, milestones); err != nil {
|
||||
if opts.Labels, opts.Milestones, err = issueFilters(cmd, target, labels, milestones); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if len(labelNames)+len(milestones) > 0 {
|
||||
opts.Labels, opts.Milestones = strings.Join(labelNames, ","), strings.Join(milestones, ",")
|
||||
} else if len(labels)+len(milestones) > 0 {
|
||||
opts.Labels, opts.Milestones = strings.Join(labels, ","), strings.Join(milestones, ",")
|
||||
if _, err := fmt.Fprintln(cmd.ErrOrStderr(), "warning: instance-wide search ignores --label and --milestone values that match nothing; pass --repo to check them"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
opts.Query = strings.Join(args, " ")
|
||||
items, err := (services.Issues{Client: target.client, Repository: target.Repo}).Search(cmd.Context(), opts)
|
||||
page, err := (services.Issues{Client: target.client, Repository: target.Repo}).SearchPage(cmd.Context(), opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return writeList(a, cmd, items, searchRow)
|
||||
where := "on " + strings.TrimPrefix(strings.TrimPrefix(target.Host, "https://"), "http://")
|
||||
if target.Repo != "" {
|
||||
where = "in " + target.Repo
|
||||
}
|
||||
if opts.Query != "" {
|
||||
where = fmt.Sprintf("matching %q %s", opts.Query, where)
|
||||
}
|
||||
what := nouns
|
||||
if opts.State != "all" {
|
||||
what = opts.State + " " + nouns
|
||||
}
|
||||
return writeTable(a, cmd, page.Items, listSpec[services.SearchItem]{
|
||||
Columns: []terminal.Column{{Header: "Repo#ID"}, {Header: "Title", Flex: true}, {Header: "State"}, {Header: "Author"}, {Header: "Updated"}},
|
||||
Row: searchRow, Empty: "no " + what + " " + where, Context: what + " " + where,
|
||||
}, servicePage(page, opts.Page))
|
||||
}}
|
||||
flags := command.Flags()
|
||||
pageFlags(command, &opts.Limit, &opts.Page)
|
||||
flags.StringVar(&opts.State, "state", "open", "State: open, closed, all")
|
||||
flags.StringVar(&opts.Owner, "owner", "", "Repository owner")
|
||||
flags.StringVar(&opts.Team, "team", "", "Organization team name (instance search; requires --owner)")
|
||||
flags.StringVar(&opts.Author, "author", "", "Issue/PR author")
|
||||
flags.StringVar(&opts.Labels, "label", "", "Comma-separated label names")
|
||||
flags.StringVar(&opts.Milestones, "milestone", "", "Comma-separated milestone names")
|
||||
updated.register(command, "since", "Updated since a date, timestamp, or duration ago", "before", "Updated before a date, timestamp, or duration ago")
|
||||
flags.BoolVar(&opts.Assigned, "assigned", false, "Assigned to the selected account")
|
||||
flags.BoolVar(&opts.Authored, "authored", false, "Created by the selected account")
|
||||
flags.BoolVar(&opts.Mentioned, "mentioned", false, "Mentioning the selected account")
|
||||
enumFlag(command, &opts.State, "state", "s", "open", "Filter by state", "open", "closed", "all")
|
||||
flags.StringVar(&opts.Owner, "owner", "", "Search only the repositories of this `owner`")
|
||||
flags.StringVar(&opts.Team, "team", "", "Search only the repositories of this organization `team` (requires --owner)")
|
||||
flags.StringVarP(&opts.Author, "author", "A", "", "Filter by author; @me for yourself")
|
||||
flags.StringSliceVarP(&labels, "label", "l", nil, "Filter by label `names`")
|
||||
flags.StringVarP(&opts.Milestones, "milestone", "m", "", "Filter by milestone `titles`, comma-separated")
|
||||
updated.register(command, "since", "Updated since a date, timestamp, or duration ago", "until", "Updated before a date, timestamp, or duration ago")
|
||||
flagAliases(command, map[string]string{"before": "until"})
|
||||
flags.BoolVar(&opts.Assigned, "assigned", false, "Assigned to you")
|
||||
flags.BoolVar(&opts.Authored, "authored", false, "Created by you")
|
||||
flags.BoolVar(&opts.Mentioned, "mentioned", false, "Mentioning you")
|
||||
if pulls {
|
||||
flags.BoolVar(&opts.ReviewRequested, "review-requested", false, "Requesting review from the selected account (instance search)")
|
||||
flags.BoolVar(&opts.Reviewed, "reviewed", false, "Reviewed by the selected account (instance search)")
|
||||
flags.BoolVar(&opts.ReviewRequested, "review-requested", false, "Requesting your review (instance-wide search)")
|
||||
flags.BoolVar(&opts.Reviewed, "reviewed", false, "Reviewed by you (instance-wide search)")
|
||||
}
|
||||
_ = command.RegisterFlagCompletionFunc("label", a.completeLabels())
|
||||
_ = command.RegisterFlagCompletionFunc("milestone", commaSeparated(a.completeMilestones()))
|
||||
_ = command.RegisterFlagCompletionFunc("author", a.completeUsers())
|
||||
_ = command.RegisterFlagCompletionFunc("owner", a.completeOrgs())
|
||||
return command
|
||||
}
|
||||
|
||||
func searchRow(item services.SearchItem) []terminal.Cell {
|
||||
return []terminal.Cell{{Text: fmt.Sprintf("%s#%d", item.Repository, item.Number), Link: item.URL}, {Text: item.Title}, {Text: item.State, Color: stateColors[item.State]}}
|
||||
return []terminal.Cell{
|
||||
{Text: fmt.Sprintf("%s#%d", item.Repository, item.Number), Link: item.URL, Color: stateColors[item.State]},
|
||||
{Text: item.Title},
|
||||
{Text: item.State, Color: stateColors[item.State]},
|
||||
{Text: item.Author},
|
||||
{Time: terminal.ParseTime(item.UpdatedAt)},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user