aboutsummaryrefslogtreecommitdiffstats
path: root/commands/bug/bug.go
blob: 4cf18ab93ba63510f775511493fc76be4a76ff2e (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package bugcmd

import (
	"fmt"
	"regexp"
	"strings"
	"time"

	text "github.com/MichaelMure/go-term-text"
	"github.com/spf13/cobra"

	"github.com/git-bug/git-bug/cache"
	"github.com/git-bug/git-bug/commands/cmdjson"
	"github.com/git-bug/git-bug/commands/completion"
	"github.com/git-bug/git-bug/commands/execenv"
	"github.com/git-bug/git-bug/entities/bug"
	"github.com/git-bug/git-bug/entities/common"
	"github.com/git-bug/git-bug/entity"
	"github.com/git-bug/git-bug/query"
	"github.com/git-bug/git-bug/util/colors"
)

type bugOptions struct {
	statusQuery         []string
	authorQuery         []string
	metadataQuery       []string
	participantQuery    []string
	actorQuery          []string
	labelQuery          []string
	titleQuery          []string
	noQuery             []string
	sortBy              string
	sortDirection       string
	outputFormat        string
	outputFormatChanged bool
}

func NewBugCommand(env *execenv.Env) *cobra.Command {
	options := bugOptions{}

	cmd := &cobra.Command{
		Use:   "bug [QUERY]",
		Short: "List bugs",
		Long: `Display a summary of each bugs.

You can pass an additional query to filter and order the list. This query can be expressed either with a simple query language, flags, a natural language full text search, or a combination of the aforementioned.`,
		Example: `List open bugs sorted by last edition with a query:
git bug status:open sort:edit-desc

List closed bugs sorted by creation with flags:
git bug --status closed --by creation

Do a full text search of all bugs:
git bug "foo bar" baz

Use queries, flags, and full text search:
git bug status:open --by creation "foo bar" baz
`,
		PreRunE: execenv.LoadBackend(env),
		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
			options.outputFormatChanged = cmd.Flags().Changed("format")
			return runBug(env, options, args)
		}),
		ValidArgsFunction: completion.Ls(env),
	}

	flags := cmd.Flags()
	flags.SortFlags = false

	flags.StringSliceVarP(&options.statusQuery, "status", "s", nil,
		"Filter by status. Valid values are [open,closed]")
	cmd.RegisterFlagCompletionFunc("status", completion.From([]string{"open", "closed"}))
	flags.StringSliceVarP(&options.authorQuery, "author", "a", nil,
		"Filter by author")
	flags.StringSliceVarP(&options.metadataQuery, "metadata", "m", nil,
		"Filter by metadata. Example: github-url=URL")
	cmd.RegisterFlagCompletionFunc("author", completion.UserForQuery(env))
	flags.StringSliceVarP(&options.participantQuery, "participant", "p", nil,
		"Filter by participant")
	cmd.RegisterFlagCompletionFunc("participant", completion.UserForQuery(env))
	flags.StringSliceVarP(&options.actorQuery, "actor", "A", nil,
		"Filter by actor")
	cmd.RegisterFlagCompletionFunc("actor", completion.UserForQuery(env))
	flags.StringSliceVarP(&options.labelQuery, "label", "l", nil,
		"Filter by label")
	cmd.RegisterFlagCompletionFunc("label", completion.Label(env))
	flags.StringSliceVarP(&options.titleQuery, "title", "t", nil,
		"Filter by title")
	flags.StringSliceVarP(&options.noQuery, "no", "n", nil,
		"Filter by absence of something. Valid values are [label]")
	cmd.RegisterFlagCompletionFunc("no", completion.Label(env))
	flags.StringVarP(&options.sortBy, "by", "b", "creation",
		"Sort the results by a characteristic. Valid values are [id,creation,edit]")
	cmd.RegisterFlagCompletionFunc("by", completion.From([]string{"id", "creation", "edit"}))
	flags.StringVarP(&options.sortDirection, "direction", "d", "asc",
		"Select the sorting direction. Valid values are [asc,desc]")
	cmd.RegisterFlagCompletionFunc("direction", completion.From([]string{"asc", "desc"}))
	flags.StringVarP(&options.outputFormat, "format", "f", "default",
		"Select the output formatting style. Valid values are [default,plain,id,json,org-mode]")
	cmd.RegisterFlagCompletionFunc("format",
		completion.From([]string{"default", "plain", "id", "json", "org-mode"}))

	const selectGroup = "select"
	cmd.AddGroup(&cobra.Group{ID: selectGroup, Title: "Implicit selection"})

	addCmdWithGroup := func(child *cobra.Command, groupID string) {
		cmd.AddCommand(child)
		child.GroupID = groupID
	}

	addCmdWithGroup(newBugDeselectCommand(env), selectGroup)
	addCmdWithGroup(newBugSelectCommand(env), selectGroup)

	cmd.AddCommand(newBugCommentCommand(env))
	cmd.AddCommand(newBugLabelCommand(env))
	cmd.AddCommand(newBugNewCommand(env))
	cmd.AddCommand(newBugRmCommand(env))
	cmd.AddCommand(newBugShowCommand(env))
	cmd.AddCommand(newBugStatusCommand(env))
	cmd.AddCommand(newBugTitleCommand(env))

	return cmd
}

func runBug(env *execenv.Env, opts bugOptions, args []string) error {
	var q *query.Query
	var err error

	if len(args) >= 1 {
		// either the shell or cobra remove the quotes, we need them back for the query parsing
		assembled := repairQuery(args)

		q, err = query.Parse(assembled)
		if err != nil {
			return err
		}
	} else {
		q = query.NewQuery()
	}

	err = completeQuery(q, opts)
	if err != nil {
		return err
	}

	allIds, err := env.Backend.Bugs().Query(q)
	if err != nil {
		return err
	}

	excerpts := make([]*cache.BugExcerpt, len(allIds))
	for i, id := range allIds {
		b, err := env.Backend.Bugs().ResolveExcerpt(id)
		if err != nil {
			return err
		}
		excerpts[i] = b
	}

	switch opts.outputFormat {
	case "default":
		if opts.outputFormatChanged {
			return bugsDefaultFormatter(env, excerpts)
		}
		if env.Out.IsTerminal() {
			return bugsDefaultFormatter(env, excerpts)
		} else {
			return bugsPlainFormatter(env, excerpts)
		}
	case "id":
		return bugsIDFormatter(env, excerpts)
	case "plain":
		return bugsPlainFormatter(env, excerpts)
	case "json":
		return bugsJsonFormatter(env, excerpts)
	case "org-mode":
		return bugsOrgmodeFormatter(env, excerpts)
	default:
		return fmt.Errorf("unknown format %s", opts.outputFormat)
	}
}

func repairQuery(args []string) string {
	for i, arg := range args {
		split := strings.Split(arg, ":")
		for j, s := range split {
			if strings.Contains(s, " ") {
				split[j] = fmt.Sprintf("\"%s\"", s)
			}
		}
		args[i] = strings.Join(split, ":")
	}
	return strings.Join(args, " ")
}

func bugsJsonFormatter(env *execenv.Env, excerpts []*cache.BugExcerpt) error {
	jsonBugs := make([]cmdjson.BugExcerpt, len(excerpts))
	for i, b := range excerpts {
		jsonBug, err := cmdjson.NewBugExcerpt(env.Backend, b)
		if err != nil {
			return err
		}
		jsonBugs[i] = jsonBug
	}
	return env.Out.PrintJSON(jsonBugs)
}

func bugsIDFormatter(env *execenv.Env, excerpts []*cache.BugExcerpt) error {
	for _, b := range excerpts {
		env.Out.Println(b.Id().String())
	}

	return nil
}

func bugsDefaultFormatter(env *execenv.Env, excerpts []*cache.BugExcerpt) error {
	width := env.Out.Width()
	widthId := entity.HumanIdLength
	widthStatus := len("closed")
	widthComment := 6

	widthRemaining := width -
		widthId - 1 -
		widthStatus - 1 -
		widthComment - 1

	widthTitle := int(float32(widthRemaining-3) * 0.7)
	if widthTitle < 0 {
		widthTitle = 0
	}

	widthRemaining = widthRemaining - widthTitle - 3 - 2
	widthAuthor := widthRemaining

	for _, b := range excerpts {
		author, err := env.Backend.Identities().ResolveExcerpt(b.AuthorId)
		if err != nil {
			return err
		}

		var labelsTxt strings.Builder
		for _, l := range b.Labels {
			lc256 := l.Color().Term256()
			labelsTxt.WriteString(lc256.Escape())
			labelsTxt.WriteString(" ◼")
			labelsTxt.WriteString(lc256.Unescape())
		}

		// truncate + pad if needed
		labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
		titleFmt := text.LeftPadMaxLine(strings.TrimSpace(b.Title), widthTitle-text.Len(labelsFmt), 0)
		authorFmt := text.LeftPadMaxLine(author.DisplayName(), widthAuthor, 0)

		comments := fmt.Sprintf("%3d 💬", b.LenComments-1)
		if b.LenComments-1 <= 0 {
			comments = ""
		}
		if b.LenComments-1 > 999 {
			comments = "  ∞ 💬"
		}

		env.Out.Printf("%s\t%s\t%s   %s %s\n",
			colors.Cyan(b.Id().Human()),
			colors.Yellow(b.Status),
			titleFmt+labelsFmt,
			colors.Magenta(authorFmt),
			comments,
		)
	}
	return nil
}

func bugsPlainFormatter(env *execenv.Env, excerpts []*cache.BugExcerpt) error {
	for _, b := range excerpts {
		env.Out.Printf("%s\t%s\t%s\n", b.Id().Human(), b.Status, strings.TrimSpace(b.Title))
	}
	return nil
}

func bugsOrgmodeFormatter(env *execenv.Env, excerpts []*cache.BugExcerpt) error {
	// see https://orgmode.org/manual/Tags.html
	orgTagRe := regexp.MustCompile("[^[:alpha:]_@]")
	formatTag := func(l bug.Label) string {
		return orgTagRe.ReplaceAllString(l.String(), "_")
	}

	formatTime := func(time time.Time) string {
		return time.Format("[2006-01-02 Mon 15:05]")
	}

	env.Out.Println("#+TODO: OPEN | CLOSED")

	for _, b := range excerpts {
		status := strings.ToUpper(b.Status.String())

		var title string
		if link, ok := b.CreateMetadata["github-url"]; ok {
			title = fmt.Sprintf("[[%s][%s]]", link, b.Title)
		} else {
			title = b.Title
		}

		author, err := env.Backend.Identities().ResolveExcerpt(b.AuthorId)
		if err != nil {
			return err
		}

		var labels strings.Builder
		labels.WriteString(":")
		for i, l := range b.Labels {
			if i > 0 {
				labels.WriteString(":")
			}
			labels.WriteString(formatTag(l))
		}
		labels.WriteString(":")

		env.Out.Printf("* %-6s %s %s %s: %s %s\n",
			status,
			b.Id().Human(),
			formatTime(b.CreateTime()),
			author.DisplayName(),
			title,
			labels.String(),
		)

		env.Out.Printf("** Last Edited: %s\n", formatTime(b.EditTime()))

		env.Out.Printf("** Actors:\n")
		for _, element := range b.Actors {
			actor, err := env.Backend.Identities().ResolveExcerpt(element)
			if err != nil {
				return err
			}

			env.Out.Printf(": %s %s\n",
				actor.Id().Human(),
				actor.DisplayName(),
			)
		}

		env.Out.Printf("** Participants:\n")
		for _, element := range b.Participants {
			participant, err := env.Backend.Identities().ResolveExcerpt(element)
			if err != nil {
				return err
			}

			env.Out.Printf(": %s %s\n",
				participant.Id().Human(),
				participant.DisplayName(),
			)
		}
	}

	return nil
}

// Finish the command flags transformation into the query.Query
func completeQuery(q *query.Query, opts bugOptions) error {
	for _, str := range opts.statusQuery {
		status, err := common.StatusFromString(str)
		if err != nil {
			return err
		}
		q.Status = append(q.Status, status)
	}

	q.Author = append(q.Author, opts.authorQuery...)
	for _, str := range opts.metadataQuery {
		tokens := strings.Split(str, "=")
		if len(tokens) < 2 {
			return fmt.Errorf("no \"=\" in key=value metadata markup")
		}
		var pair query.StringPair
		pair.Key = tokens[0]
		pair.Value = tokens[1]
		q.Metadata = append(q.Metadata, pair)
	}
	q.Participant = append(q.Participant, opts.participantQuery...)
	q.Actor = append(q.Actor, opts.actorQuery...)
	q.Label = append(q.Label, opts.labelQuery...)
	q.Title = append(q.Title, opts.titleQuery...)

	for _, no := range opts.noQuery {
		switch no {
		case "label":
			q.NoLabel = true
		default:
			return fmt.Errorf("unknown \"no\" filter %s", no)
		}
	}

	switch opts.sortBy {
	case "id":
		q.OrderBy = query.OrderById
	case "creation":
		q.OrderBy = query.OrderByCreation
	case "edit":
		q.OrderBy = query.OrderByEdit
	default:
		return fmt.Errorf("unknown sort flag %s", opts.sortBy)
	}

	switch opts.sortDirection {
	case "asc":
		q.OrderDirection = query.OrderAscending
	case "desc":
		q.OrderDirection = query.OrderDescending
	default:
		return fmt.Errorf("unknown sort direction %s", opts.sortDirection)
	}

	return nil
}