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 author.Match(query)
37 }
38
39 // Legacy identity support
40 return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), query)
41 }
42}
43
44// LabelFilter return a Filter that match a label
45func LabelFilter(label string) Filter {
46 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
47 for _, l := range excerpt.Labels {
48 if string(l) == label {
49 return true
50 }
51 }
52 return false
53 }
54}
55
56// ActorFilter return a Filter that match a bug actor
57func ActorFilter(query string) Filter {
58 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
59 query = strings.ToLower(query)
60
61 for _, id := range excerpt.Actors {
62 identityExcerpt, ok := repoCache.identitiesExcerpts[id]
63 if !ok {
64 panic("missing identity in the cache")
65 }
66
67 if identityExcerpt.Match(query) {
68 return true
69 }
70 }
71 return false
72 }
73}
74
75// ParticipantFilter return a Filter that match a bug participant
76func ParticipantFilter(query string) Filter {
77 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
78 query = strings.ToLower(query)
79
80 for _, id := range excerpt.Participants {
81 identityExcerpt, ok := repoCache.identitiesExcerpts[id]
82 if !ok {
83 panic("missing identity in the cache")
84 }
85
86 if identityExcerpt.Match(query) {
87 return true
88 }
89 }
90 return false
91 }
92}
93
94// TitleFilter return a Filter that match if the title contains the given query
95func TitleFilter(query string) Filter {
96 return func(repo *RepoCache, excerpt *BugExcerpt) bool {
97 return strings.Contains(
98 strings.ToLower(excerpt.Title),
99 strings.ToLower(query),
100 )
101 }
102}
103
104// NoLabelFilter return a Filter that match the absence of labels
105func NoLabelFilter() Filter {
106 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
107 return len(excerpt.Labels) == 0
108 }
109}
110
111// Filters is a collection of Filter that implement a complex filter
112type Filters struct {
113 Status []Filter
114 Author []Filter
115 Actor []Filter
116 Participant []Filter
117 Label []Filter
118 Title []Filter
119 NoFilters []Filter
120}
121
122// Match check if a bug match the set of filters
123func (f *Filters) Match(repoCache *RepoCache, excerpt *BugExcerpt) bool {
124 if match := f.orMatch(f.Status, repoCache, excerpt); !match {
125 return false
126 }
127
128 if match := f.orMatch(f.Author, repoCache, excerpt); !match {
129 return false
130 }
131
132 if match := f.orMatch(f.Participant, repoCache, excerpt); !match {
133 return false
134 }
135
136 if match := f.orMatch(f.Actor, repoCache, excerpt); !match {
137 return false
138 }
139
140 if match := f.andMatch(f.Label, repoCache, excerpt); !match {
141 return false
142 }
143
144 if match := f.andMatch(f.NoFilters, repoCache, excerpt); !match {
145 return false
146 }
147
148 if match := f.andMatch(f.Title, repoCache, excerpt); !match {
149 return false
150 }
151
152 return true
153}
154
155// Check if any of the filters provided match the bug
156func (*Filters) orMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
157 if len(filters) == 0 {
158 return true
159 }
160
161 match := false
162 for _, f := range filters {
163 match = match || f(repoCache, excerpt)
164 }
165
166 return match
167}
168
169// Check if all of the filters provided match the bug
170func (*Filters) andMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
171 if len(filters) == 0 {
172 return true
173 }
174
175 match := true
176 for _, f := range filters {
177 match = match && f(repoCache, excerpt)
178 }
179
180 return match
181}