1package cache
2
3import (
4 "strings"
5
6 "github.com/MichaelMure/git-bug/bug"
7)
8
9// Filter is a predicate that match a subset of bugs
10type Filter func(repoCache *RepoCache, excerpt *BugExcerpt) bool
11
12// StatusFilter return a Filter that match a bug status
13func StatusFilter(query string) (Filter, error) {
14 status, err := bug.StatusFromString(query)
15 if err != nil {
16 return nil, err
17 }
18
19 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
20 return excerpt.Status == status
21 }, nil
22}
23
24// AuthorFilter return a Filter that match a bug author
25func AuthorFilter(query string) Filter {
26 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
27 query = strings.ToLower(query)
28
29 // Normal identity
30 if excerpt.AuthorId != "" {
31 author, ok := repoCache.identitiesExcerpts[excerpt.AuthorId]
32 if !ok {
33 panic("missing identity in the cache")
34 }
35
36 return strings.Contains(strings.ToLower(author.Name), query) ||
37 strings.Contains(strings.ToLower(author.Login), query)
38 }
39
40 // Legacy identity support
41 return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), query) ||
42 strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Login), query)
43 }
44}
45
46// LabelFilter return a Filter that match a label
47func LabelFilter(label string) Filter {
48 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
49 for _, l := range excerpt.Labels {
50 if string(l) == label {
51 return true
52 }
53 }
54 return false
55 }
56}
57
58// TitleFilter return a Filter that match if the title contains the given query
59func TitleFilter(query string) Filter {
60 return func(repo *RepoCache, excerpt *BugExcerpt) bool {
61 return strings.Contains(
62 strings.ToLower(excerpt.Title),
63 strings.ToLower(query),
64 )
65 }
66}
67
68// NoLabelFilter return a Filter that match the absence of labels
69func NoLabelFilter() Filter {
70 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
71 return len(excerpt.Labels) == 0
72 }
73}
74
75// Filters is a collection of Filter that implement a complex filter
76type Filters struct {
77 Status []Filter
78 Author []Filter
79 Label []Filter
80 Title []Filter
81 NoFilters []Filter
82}
83
84// Match check if a bug match the set of filters
85func (f *Filters) Match(repoCache *RepoCache, excerpt *BugExcerpt) bool {
86 if match := f.orMatch(f.Status, repoCache, excerpt); !match {
87 return false
88 }
89
90 if match := f.orMatch(f.Author, repoCache, excerpt); !match {
91 return false
92 }
93
94 if match := f.andMatch(f.Label, repoCache, excerpt); !match {
95 return false
96 }
97
98 if match := f.andMatch(f.NoFilters, repoCache, excerpt); !match {
99 return false
100 }
101
102 if match := f.andMatch(f.Title, repoCache, excerpt); !match {
103 return false
104 }
105
106 return true
107}
108
109// Check if any of the filters provided match the bug
110func (*Filters) orMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
111 if len(filters) == 0 {
112 return true
113 }
114
115 match := false
116 for _, f := range filters {
117 match = match || f(repoCache, excerpt)
118 }
119
120 return match
121}
122
123// Check if all of the filters provided match the bug
124func (*Filters) andMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
125 if len(filters) == 0 {
126 return true
127 }
128
129 match := true
130 for _, f := range filters {
131 match = match && f(repoCache, excerpt)
132 }
133
134 return match
135}