bug.go

  1package bugcmd
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"regexp"
  7	"strings"
  8	"time"
  9
 10	text "github.com/MichaelMure/go-term-text"
 11	"github.com/spf13/cobra"
 12
 13	"github.com/MichaelMure/git-bug/cache"
 14	"github.com/MichaelMure/git-bug/commands/cmdjson"
 15	"github.com/MichaelMure/git-bug/commands/completion"
 16	"github.com/MichaelMure/git-bug/commands/execenv"
 17	"github.com/MichaelMure/git-bug/entities/bug"
 18	"github.com/MichaelMure/git-bug/entities/common"
 19	"github.com/MichaelMure/git-bug/query"
 20	"github.com/MichaelMure/git-bug/util/colors"
 21)
 22
 23type bugOptions struct {
 24	statusQuery      []string
 25	authorQuery      []string
 26	metadataQuery    []string
 27	participantQuery []string
 28	actorQuery       []string
 29	labelQuery       []string
 30	titleQuery       []string
 31	noQuery          []string
 32	sortBy           string
 33	sortDirection    string
 34	outputFormat     string
 35}
 36
 37func NewBugCommand() *cobra.Command {
 38	env := execenv.NewEnv()
 39	options := bugOptions{}
 40
 41	cmd := &cobra.Command{
 42		Use:   "bug [QUERY]",
 43		Short: "List bugs",
 44		Long: `Display a summary of each bugs.
 45
 46You 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.`,
 47		Example: `List open bugs sorted by last edition with a query:
 48git bug status:open sort:edit-desc
 49
 50List closed bugs sorted by creation with flags:
 51git bug --status closed --by creation
 52
 53Do a full text search of all bugs:
 54git bug "foo bar" baz
 55
 56Use queries, flags, and full text search:
 57git bug status:open --by creation "foo bar" baz
 58`,
 59		PreRunE: execenv.LoadBackend(env),
 60		RunE: execenv.CloseBackend(env, func(cmd *cobra.Command, args []string) error {
 61			return runBug(env, options, args)
 62		}),
 63		ValidArgsFunction: completion.Ls(env),
 64	}
 65
 66	flags := cmd.Flags()
 67	flags.SortFlags = false
 68
 69	flags.StringSliceVarP(&options.statusQuery, "status", "s", nil,
 70		"Filter by status. Valid values are [open,closed]")
 71	cmd.RegisterFlagCompletionFunc("status", completion.From([]string{"open", "closed"}))
 72	flags.StringSliceVarP(&options.authorQuery, "author", "a", nil,
 73		"Filter by author")
 74	flags.StringSliceVarP(&options.metadataQuery, "metadata", "m", nil,
 75		"Filter by metadata. Example: github-url=URL")
 76	cmd.RegisterFlagCompletionFunc("author", completion.UserForQuery(env))
 77	flags.StringSliceVarP(&options.participantQuery, "participant", "p", nil,
 78		"Filter by participant")
 79	cmd.RegisterFlagCompletionFunc("participant", completion.UserForQuery(env))
 80	flags.StringSliceVarP(&options.actorQuery, "actor", "A", nil,
 81		"Filter by actor")
 82	cmd.RegisterFlagCompletionFunc("actor", completion.UserForQuery(env))
 83	flags.StringSliceVarP(&options.labelQuery, "label", "l", nil,
 84		"Filter by label")
 85	cmd.RegisterFlagCompletionFunc("label", completion.Label(env))
 86	flags.StringSliceVarP(&options.titleQuery, "title", "t", nil,
 87		"Filter by title")
 88	flags.StringSliceVarP(&options.noQuery, "no", "n", nil,
 89		"Filter by absence of something. Valid values are [label]")
 90	cmd.RegisterFlagCompletionFunc("no", completion.Label(env))
 91	flags.StringVarP(&options.sortBy, "by", "b", "creation",
 92		"Sort the results by a characteristic. Valid values are [id,creation,edit]")
 93	cmd.RegisterFlagCompletionFunc("by", completion.From([]string{"id", "creation", "edit"}))
 94	flags.StringVarP(&options.sortDirection, "direction", "d", "asc",
 95		"Select the sorting direction. Valid values are [asc,desc]")
 96	cmd.RegisterFlagCompletionFunc("direction", completion.From([]string{"asc", "desc"}))
 97	flags.StringVarP(&options.outputFormat, "format", "f", "default",
 98		"Select the output formatting style. Valid values are [default,plain,compact,id,json,org-mode]")
 99	cmd.RegisterFlagCompletionFunc("format",
100		completion.From([]string{"default", "plain", "compact", "id", "json", "org-mode"}))
101
102	const selectGroup = "select"
103	cmd.AddGroup(&cobra.Group{ID: selectGroup, Title: "Implicit selection"})
104
105	addCmdWithGroup := func(child *cobra.Command, groupID string) {
106		cmd.AddCommand(child)
107		child.GroupID = groupID
108	}
109
110	addCmdWithGroup(newBugDeselectCommand(), selectGroup)
111	addCmdWithGroup(newBugSelectCommand(), selectGroup)
112
113	cmd.AddCommand(newBugCommentCommand())
114	cmd.AddCommand(newBugLabelCommand())
115	cmd.AddCommand(newBugNewCommand())
116	cmd.AddCommand(newBugRmCommand())
117	cmd.AddCommand(newBugShowCommand())
118	cmd.AddCommand(newBugStatusCommand())
119	cmd.AddCommand(newBugTitleCommand())
120
121	return cmd
122}
123
124func runBug(env *execenv.Env, opts bugOptions, args []string) error {
125	var q *query.Query
126	var err error
127
128	if len(args) >= 1 {
129		// either the shell or cobra remove the quotes, we need them back for the query parsing
130		assembled := repairQuery(args)
131
132		q, err = query.Parse(assembled)
133		if err != nil {
134			return err
135		}
136	} else {
137		q = query.NewQuery()
138	}
139
140	err = completeQuery(q, opts)
141	if err != nil {
142		return err
143	}
144
145	allIds, err := env.Backend.QueryBugs(q)
146	if err != nil {
147		return err
148	}
149
150	bugExcerpt := make([]*cache.BugExcerpt, len(allIds))
151	for i, id := range allIds {
152		b, err := env.Backend.ResolveBugExcerpt(id)
153		if err != nil {
154			return err
155		}
156		bugExcerpt[i] = b
157	}
158
159	switch opts.outputFormat {
160	case "org-mode":
161		return bugsOrgmodeFormatter(env, bugExcerpt)
162	case "plain":
163		return bugsPlainFormatter(env, bugExcerpt)
164	case "json":
165		return bugsJsonFormatter(env, bugExcerpt)
166	case "compact":
167		return bugsCompactFormatter(env, bugExcerpt)
168	case "id":
169		return bugsIDFormatter(env, bugExcerpt)
170	case "default":
171		return bugsDefaultFormatter(env, bugExcerpt)
172	default:
173		return fmt.Errorf("unknown format %s", opts.outputFormat)
174	}
175}
176
177func repairQuery(args []string) string {
178	for i, arg := range args {
179		split := strings.Split(arg, ":")
180		for j, s := range split {
181			if strings.Contains(s, " ") {
182				split[j] = fmt.Sprintf("\"%s\"", s)
183			}
184		}
185		args[i] = strings.Join(split, ":")
186	}
187	return strings.Join(args, " ")
188}
189
190type JSONBugExcerpt struct {
191	Id         string       `json:"id"`
192	HumanId    string       `json:"human_id"`
193	CreateTime cmdjson.Time `json:"create_time"`
194	EditTime   cmdjson.Time `json:"edit_time"`
195
196	Status       string             `json:"status"`
197	Labels       []bug.Label        `json:"labels"`
198	Title        string             `json:"title"`
199	Actors       []cmdjson.Identity `json:"actors"`
200	Participants []cmdjson.Identity `json:"participants"`
201	Author       cmdjson.Identity   `json:"author"`
202
203	Comments int               `json:"comments"`
204	Metadata map[string]string `json:"metadata"`
205}
206
207func bugsJsonFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error {
208	jsonBugs := make([]JSONBugExcerpt, len(bugExcerpts))
209	for i, b := range bugExcerpts {
210		jsonBug := JSONBugExcerpt{
211			Id:         b.Id.String(),
212			HumanId:    b.Id.Human(),
213			CreateTime: cmdjson.NewTime(b.CreateTime(), b.CreateLamportTime),
214			EditTime:   cmdjson.NewTime(b.EditTime(), b.EditLamportTime),
215			Status:     b.Status.String(),
216			Labels:     b.Labels,
217			Title:      b.Title,
218			Comments:   b.LenComments,
219			Metadata:   b.CreateMetadata,
220		}
221
222		author, err := env.Backend.ResolveIdentityExcerpt(b.AuthorId)
223		if err != nil {
224			return err
225		}
226		jsonBug.Author = cmdjson.NewIdentityFromExcerpt(author)
227
228		jsonBug.Actors = make([]cmdjson.Identity, len(b.Actors))
229		for i, element := range b.Actors {
230			actor, err := env.Backend.ResolveIdentityExcerpt(element)
231			if err != nil {
232				return err
233			}
234			jsonBug.Actors[i] = cmdjson.NewIdentityFromExcerpt(actor)
235		}
236
237		jsonBug.Participants = make([]cmdjson.Identity, len(b.Participants))
238		for i, element := range b.Participants {
239			participant, err := env.Backend.ResolveIdentityExcerpt(element)
240			if err != nil {
241				return err
242			}
243			jsonBug.Participants[i] = cmdjson.NewIdentityFromExcerpt(participant)
244		}
245
246		jsonBugs[i] = jsonBug
247	}
248	jsonObject, _ := json.MarshalIndent(jsonBugs, "", "    ")
249	env.Out.Printf("%s\n", jsonObject)
250	return nil
251}
252
253func bugsCompactFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error {
254	for _, b := range bugExcerpts {
255		author, err := env.Backend.ResolveIdentityExcerpt(b.AuthorId)
256		if err != nil {
257			return err
258		}
259
260		var labelsTxt strings.Builder
261		for _, l := range b.Labels {
262			lc256 := l.Color().Term256()
263			labelsTxt.WriteString(lc256.Escape())
264			labelsTxt.WriteString("◼")
265			labelsTxt.WriteString(lc256.Unescape())
266		}
267
268		env.Out.Printf("%s %s %s %s %s\n",
269			colors.Cyan(b.Id.Human()),
270			colors.Yellow(b.Status),
271			text.LeftPadMaxLine(strings.TrimSpace(b.Title), 40, 0),
272			text.LeftPadMaxLine(labelsTxt.String(), 5, 0),
273			colors.Magenta(text.TruncateMax(author.DisplayName(), 15)),
274		)
275	}
276	return nil
277}
278
279func bugsIDFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error {
280	for _, b := range bugExcerpts {
281		env.Out.Println(b.Id.String())
282	}
283
284	return nil
285}
286
287func bugsDefaultFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error {
288	for _, b := range bugExcerpts {
289		author, err := env.Backend.ResolveIdentityExcerpt(b.AuthorId)
290		if err != nil {
291			return err
292		}
293
294		var labelsTxt strings.Builder
295		for _, l := range b.Labels {
296			lc256 := l.Color().Term256()
297			labelsTxt.WriteString(lc256.Escape())
298			labelsTxt.WriteString(" ◼")
299			labelsTxt.WriteString(lc256.Unescape())
300		}
301
302		// truncate + pad if needed
303		labelsFmt := text.TruncateMax(labelsTxt.String(), 10)
304		titleFmt := text.LeftPadMaxLine(strings.TrimSpace(b.Title), 50-text.Len(labelsFmt), 0)
305		authorFmt := text.LeftPadMaxLine(author.DisplayName(), 15, 0)
306
307		comments := fmt.Sprintf("%3d 💬", b.LenComments-1)
308		if b.LenComments-1 <= 0 {
309			comments = ""
310		}
311		if b.LenComments-1 > 999 {
312			comments = "  ∞ 💬"
313		}
314
315		env.Out.Printf("%s\t%s\t%s\t%s\t%s\n",
316			colors.Cyan(b.Id.Human()),
317			colors.Yellow(b.Status),
318			titleFmt+labelsFmt,
319			colors.Magenta(authorFmt),
320			comments,
321		)
322	}
323	return nil
324}
325
326func bugsPlainFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error {
327	for _, b := range bugExcerpts {
328		env.Out.Printf("%s [%s] %s\n", b.Id.Human(), b.Status, strings.TrimSpace(b.Title))
329	}
330	return nil
331}
332
333func bugsOrgmodeFormatter(env *execenv.Env, bugExcerpts []*cache.BugExcerpt) error {
334	// see https://orgmode.org/manual/Tags.html
335	orgTagRe := regexp.MustCompile("[^[:alpha:]_@]")
336	formatTag := func(l bug.Label) string {
337		return orgTagRe.ReplaceAllString(l.String(), "_")
338	}
339
340	formatTime := func(time time.Time) string {
341		return time.Format("[2006-01-02 Mon 15:05]")
342	}
343
344	env.Out.Println("#+TODO: OPEN | CLOSED")
345
346	for _, b := range bugExcerpts {
347		status := strings.ToUpper(b.Status.String())
348
349		var title string
350		if link, ok := b.CreateMetadata["github-url"]; ok {
351			title = fmt.Sprintf("[[%s][%s]]", link, b.Title)
352		} else {
353			title = b.Title
354		}
355
356		author, err := env.Backend.ResolveIdentityExcerpt(b.AuthorId)
357		if err != nil {
358			return err
359		}
360
361		var labels strings.Builder
362		labels.WriteString(":")
363		for i, l := range b.Labels {
364			if i > 0 {
365				labels.WriteString(":")
366			}
367			labels.WriteString(formatTag(l))
368		}
369		labels.WriteString(":")
370
371		env.Out.Printf("* %-6s %s %s %s: %s %s\n",
372			status,
373			b.Id.Human(),
374			formatTime(b.CreateTime()),
375			author.DisplayName(),
376			title,
377			labels.String(),
378		)
379
380		env.Out.Printf("** Last Edited: %s\n", formatTime(b.EditTime()))
381
382		env.Out.Printf("** Actors:\n")
383		for _, element := range b.Actors {
384			actor, err := env.Backend.ResolveIdentityExcerpt(element)
385			if err != nil {
386				return err
387			}
388
389			env.Out.Printf(": %s %s\n",
390				actor.Id.Human(),
391				actor.DisplayName(),
392			)
393		}
394
395		env.Out.Printf("** Participants:\n")
396		for _, element := range b.Participants {
397			participant, err := env.Backend.ResolveIdentityExcerpt(element)
398			if err != nil {
399				return err
400			}
401
402			env.Out.Printf(": %s %s\n",
403				participant.Id.Human(),
404				participant.DisplayName(),
405			)
406		}
407	}
408
409	return nil
410}
411
412// Finish the command flags transformation into the query.Query
413func completeQuery(q *query.Query, opts bugOptions) error {
414	for _, str := range opts.statusQuery {
415		status, err := common.StatusFromString(str)
416		if err != nil {
417			return err
418		}
419		q.Status = append(q.Status, status)
420	}
421
422	q.Author = append(q.Author, opts.authorQuery...)
423	for _, str := range opts.metadataQuery {
424		tokens := strings.Split(str, "=")
425		if len(tokens) < 2 {
426			return fmt.Errorf("no \"=\" in key=value metadata markup")
427		}
428		var pair query.StringPair
429		pair.Key = tokens[0]
430		pair.Value = tokens[1]
431		q.Metadata = append(q.Metadata, pair)
432	}
433	q.Participant = append(q.Participant, opts.participantQuery...)
434	q.Actor = append(q.Actor, opts.actorQuery...)
435	q.Label = append(q.Label, opts.labelQuery...)
436	q.Title = append(q.Title, opts.titleQuery...)
437
438	for _, no := range opts.noQuery {
439		switch no {
440		case "label":
441			q.NoLabel = true
442		default:
443			return fmt.Errorf("unknown \"no\" filter %s", no)
444		}
445	}
446
447	switch opts.sortBy {
448	case "id":
449		q.OrderBy = query.OrderById
450	case "creation":
451		q.OrderBy = query.OrderByCreation
452	case "edit":
453		q.OrderBy = query.OrderByEdit
454	default:
455		return fmt.Errorf("unknown sort flag %s", opts.sortBy)
456	}
457
458	switch opts.sortDirection {
459	case "asc":
460		q.OrderDirection = query.OrderAscending
461	case "desc":
462		q.OrderDirection = query.OrderDescending
463	default:
464		return fmt.Errorf("unknown sort direction %s", opts.sortDirection)
465	}
466
467	return nil
468}