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
|
package cache
import (
"strings"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/query/ast"
)
// resolver has the resolving functions needed by filters.
// This exist mainly to go through the functions of the cache with proper locking.
type resolver interface {
ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, error)
}
// Filter is a predicate that match a subset of bugs
type Filter func(excerpt *BugExcerpt, resolver resolver) bool
// StatusFilter return a Filter that match a bug status
func StatusFilter(status bug.Status) Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
return excerpt.Status == status
}
}
// AuthorFilter return a Filter that match a bug author
func AuthorFilter(query string) Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
query = strings.ToLower(query)
// Normal identity
if excerpt.AuthorId != "" {
author, err := resolver.ResolveIdentityExcerpt(excerpt.AuthorId)
if err != nil {
panic(err)
}
return author.Match(query)
}
// Legacy identity support
return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), query) ||
strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Login), query)
}
}
// LabelFilter return a Filter that match a label
func LabelFilter(label string) Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
for _, l := range excerpt.Labels {
if string(l) == label {
return true
}
}
return false
}
}
// ActorFilter return a Filter that match a bug actor
func ActorFilter(query string) Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
query = strings.ToLower(query)
for _, id := range excerpt.Actors {
identityExcerpt, err := resolver.ResolveIdentityExcerpt(id)
if err != nil {
panic(err)
}
if identityExcerpt.Match(query) {
return true
}
}
return false
}
}
// ParticipantFilter return a Filter that match a bug participant
func ParticipantFilter(query string) Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
query = strings.ToLower(query)
for _, id := range excerpt.Participants {
identityExcerpt, err := resolver.ResolveIdentityExcerpt(id)
if err != nil {
panic(err)
}
if identityExcerpt.Match(query) {
return true
}
}
return false
}
}
// TitleFilter return a Filter that match if the title contains the given query
func TitleFilter(query string) Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
return strings.Contains(
strings.ToLower(excerpt.Title),
strings.ToLower(query),
)
}
}
// NoLabelFilter return a Filter that match the absence of labels
func NoLabelFilter() Filter {
return func(excerpt *BugExcerpt, resolver resolver) bool {
return len(excerpt.Labels) == 0
}
}
// Matcher is a collection of Filter that implement a complex filter
type Matcher struct {
Status []Filter
Author []Filter
Actor []Filter
Participant []Filter
Label []Filter
Title []Filter
NoFilters []Filter
}
// compileMatcher transform an ast.Filters into a specialized matcher
// for the cache.
func compileMatcher(filters ast.Filters) *Matcher {
result := &Matcher{}
for _, value := range filters.Status {
result.Status = append(result.Status, StatusFilter(value))
}
for _, value := range filters.Author {
result.Author = append(result.Author, AuthorFilter(value))
}
for _, value := range filters.Actor {
result.Actor = append(result.Actor, ActorFilter(value))
}
for _, value := range filters.Participant {
result.Participant = append(result.Participant, ParticipantFilter(value))
}
for _, value := range filters.Label {
result.Label = append(result.Label, LabelFilter(value))
}
for _, value := range filters.Title {
result.Title = append(result.Title, TitleFilter(value))
}
return result
}
// Match check if a bug match the set of filters
func (f *Matcher) Match(excerpt *BugExcerpt, resolver resolver) bool {
if match := f.orMatch(f.Status, excerpt, resolver); !match {
return false
}
if match := f.orMatch(f.Author, excerpt, resolver); !match {
return false
}
if match := f.orMatch(f.Participant, excerpt, resolver); !match {
return false
}
if match := f.orMatch(f.Actor, excerpt, resolver); !match {
return false
}
if match := f.andMatch(f.Label, excerpt, resolver); !match {
return false
}
if match := f.andMatch(f.NoFilters, excerpt, resolver); !match {
return false
}
if match := f.andMatch(f.Title, excerpt, resolver); !match {
return false
}
return true
}
// Check if any of the filters provided match the bug
func (*Matcher) orMatch(filters []Filter, excerpt *BugExcerpt, resolver resolver) bool {
if len(filters) == 0 {
return true
}
match := false
for _, f := range filters {
match = match || f(excerpt, resolver)
}
return match
}
// Check if all of the filters provided match the bug
func (*Matcher) andMatch(filters []Filter, excerpt *BugExcerpt, resolver resolver) bool {
if len(filters) == 0 {
return true
}
match := true
for _, f := range filters {
match = match && f(excerpt, resolver)
}
return match
}
|