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 a title
59func TitleFilter(title string) Filter {
60 return func(excerpt *BugExcerpt) bool {
61 return strings.Contains(excerpt.Title, title)
62 }
63}
64
65// NoLabelFilter return a Filter that match the absence of labels
66func NoLabelFilter() Filter {
67 return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
68 return len(excerpt.Labels) == 0
69 }
70}
71
72// Filters is a collection of Filter that implement a complex filter
73type Filters struct {
74 Status []Filter
75 Author []Filter
76 Label []Filter
77 Title []Filter
78 NoFilters []Filter
79}
80
81// Match check if a bug match the set of filters
82func (f *Filters) Match(repoCache *RepoCache, excerpt *BugExcerpt) bool {
83 if match := f.orMatch(f.Status, repoCache, excerpt); !match {
84 return false
85 }
86
87 if match := f.orMatch(f.Author, repoCache, excerpt); !match {
88 return false
89 }
90
91 if match := f.orMatch(f.Label, repoCache, excerpt); !match {
92 return false
93 }
94
95 if match := f.andMatch(f.NoFilters, repoCache, excerpt); !match {
96 return false
97 }
98
99 if match := f.andMatch(f.Title, excerpt); !match {
100 return false
101 }
102
103 return true
104}
105
106// Check if any of the filters provided match the bug
107func (*Filters) orMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
108 if len(filters) == 0 {
109 return true
110 }
111
112 match := false
113 for _, f := range filters {
114 match = match || f(repoCache, excerpt)
115 }
116
117 return match
118}
119
120// Check if all of the filters provided match the bug
121func (*Filters) andMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
122 if len(filters) == 0 {
123 return true
124 }
125
126 match := true
127 for _, f := range filters {
128 match = match && f(repoCache, excerpt)
129 }
130
131 return match
132}