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