forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared.go
More file actions
165 lines (154 loc) · 3.92 KB
/
shared.go
File metadata and controls
165 lines (154 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package shared
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/cli/cli/v2/pkg/search"
"github.com/cli/cli/v2/pkg/text"
"github.com/cli/cli/v2/utils"
)
type EntityType int
const (
// Limitation of GitHub search see:
// https://docs.github.com/en/rest/reference/search
SearchMaxResults = 1000
Both EntityType = iota
Issues
PullRequests
)
type IssuesOptions struct {
Browser cmdutil.Browser
Entity EntityType
Exporter cmdutil.Exporter
IO *iostreams.IOStreams
Query search.Query
Searcher search.Searcher
WebMode bool
}
func Searcher(f *cmdutil.Factory) (search.Searcher, error) {
cfg, err := f.Config()
if err != nil {
return nil, err
}
host, err := cfg.DefaultHost()
if err != nil {
return nil, err
}
client, err := f.HttpClient()
if err != nil {
return nil, err
}
return search.NewSearcher(client, host), nil
}
func SearchIssues(opts *IssuesOptions) error {
io := opts.IO
if opts.WebMode {
url := opts.Searcher.URL(opts.Query)
if io.IsStdoutTTY() {
fmt.Fprintf(io.ErrOut, "Opening %s in your browser.\n", utils.DisplayURL(url))
}
return opts.Browser.Browse(url)
}
io.StartProgressIndicator()
result, err := opts.Searcher.Issues(opts.Query)
io.StopProgressIndicator()
if err != nil {
return err
}
if err := io.StartPager(); err == nil {
defer io.StopPager()
} else {
fmt.Fprintf(io.ErrOut, "failed to start pager: %v\n", err)
}
if opts.Exporter != nil {
return opts.Exporter.Write(io, result.Items)
}
return displayIssueResults(io, opts.Entity, result)
}
func displayIssueResults(io *iostreams.IOStreams, et EntityType, results search.IssuesResult) error {
cs := io.ColorScheme()
tp := utils.NewTablePrinter(io)
for _, issue := range results.Items {
if et == Both {
kind := "issue"
if issue.IsPullRequest() {
kind = "pr"
}
tp.AddField(kind, nil, nil)
}
comp := strings.Split(issue.RepositoryURL, "/")
name := comp[len(comp)-2:]
tp.AddField(strings.Join(name, "/"), nil, nil)
issueNum := strconv.Itoa(issue.Number)
if tp.IsTTY() {
issueNum = "#" + issueNum
}
tp.AddField(issueNum, nil, cs.ColorFromString(colorForIssueState(issue.State)))
if !tp.IsTTY() {
tp.AddField(issue.State, nil, nil)
}
tp.AddField(text.ReplaceExcessiveWhitespace(issue.Title), nil, nil)
tp.AddField(listIssueLabels(&issue, cs, tp.IsTTY()), nil, nil)
now := time.Now()
ago := now.Sub(issue.UpdatedAt)
if tp.IsTTY() {
tp.AddField(utils.FuzzyAgo(ago), nil, cs.Gray)
} else {
tp.AddField(issue.UpdatedAt.String(), nil, nil)
}
tp.EndRow()
}
if io.IsStdoutTTY() {
var header string
if len(results.Items) == 0 {
switch et {
case Both:
header = "No issues or pull requests matched your search\n"
case Issues:
header = "No issues matched your search\n"
case PullRequests:
header = "No pull requests matched your search\n"
}
} else {
switch et {
case Both:
header = fmt.Sprintf("Showing %d of %d issues and pull requests\n\n", len(results.Items), results.Total)
case Issues:
header = fmt.Sprintf("Showing %d of %d issues\n\n", len(results.Items), results.Total)
case PullRequests:
header = fmt.Sprintf("Showing %d of %d pull requests\n\n", len(results.Items), results.Total)
}
}
fmt.Fprintf(io.Out, "\n%s", header)
}
return tp.Render()
}
func listIssueLabels(issue *search.Issue, cs *iostreams.ColorScheme, colorize bool) string {
if len(issue.Labels) == 0 {
return ""
}
labelNames := make([]string, 0, len(issue.Labels))
for _, label := range issue.Labels {
if colorize {
labelNames = append(labelNames, cs.HexToRGB(label.Color, label.Name))
} else {
labelNames = append(labelNames, label.Name)
}
}
return strings.Join(labelNames, ", ")
}
func colorForIssueState(state string) string {
switch state {
case "open":
return "green"
case "closed":
return "red"
case "merged":
return "magenta"
default:
return ""
}
}