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