filter.go

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