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