2018-09-09 21:19:50 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-01-20 17:41:27 +03:00
|
|
|
"strings"
|
|
|
|
|
2018-09-09 21:19:50 +03:00
|
|
|
"github.com/MichaelMure/git-bug/bug"
|
|
|
|
)
|
|
|
|
|
2018-09-10 13:47:05 +03:00
|
|
|
// Filter is a functor that match a subset of bugs
|
2018-09-09 21:19:50 +03:00
|
|
|
type Filter func(excerpt *BugExcerpt) bool
|
|
|
|
|
2018-09-10 13:47:05 +03:00
|
|
|
// StatusFilter return a Filter that match a bug status
|
2018-09-09 21:19:50 +03:00
|
|
|
func StatusFilter(query string) (Filter, error) {
|
|
|
|
status, err := bug.StatusFromString(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(excerpt *BugExcerpt) bool {
|
|
|
|
return excerpt.Status == status
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:47:05 +03:00
|
|
|
// AuthorFilter return a Filter that match a bug author
|
2018-09-09 21:19:50 +03:00
|
|
|
func AuthorFilter(query string) Filter {
|
|
|
|
return func(excerpt *BugExcerpt) bool {
|
2019-01-20 17:41:27 +03:00
|
|
|
query = strings.ToLower(query)
|
|
|
|
|
|
|
|
return strings.Contains(strings.ToLower(excerpt.Author.Name), query) ||
|
|
|
|
strings.Contains(strings.ToLower(excerpt.Author.Login), query)
|
2018-09-09 21:19:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:47:05 +03:00
|
|
|
// LabelFilter return a Filter that match a label
|
2018-09-09 21:19:50 +03:00
|
|
|
func LabelFilter(label string) Filter {
|
|
|
|
return func(excerpt *BugExcerpt) bool {
|
|
|
|
for _, l := range excerpt.Labels {
|
|
|
|
if string(l) == label {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:47:05 +03:00
|
|
|
// NoLabelFilter return a Filter that match the absence of labels
|
2018-09-09 21:19:50 +03:00
|
|
|
func NoLabelFilter() Filter {
|
|
|
|
return func(excerpt *BugExcerpt) bool {
|
|
|
|
return len(excerpt.Labels) == 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 13:47:05 +03:00
|
|
|
// Filters is a collection of Filter that implement a complex filter
|
2018-09-09 21:19:50 +03:00
|
|
|
type Filters struct {
|
|
|
|
Status []Filter
|
|
|
|
Author []Filter
|
|
|
|
Label []Filter
|
|
|
|
NoFilters []Filter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match check if a bug match the set of filters
|
|
|
|
func (f *Filters) Match(excerpt *BugExcerpt) bool {
|
|
|
|
if match := f.orMatch(f.Status, excerpt); !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if match := f.orMatch(f.Author, excerpt); !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if match := f.orMatch(f.Label, excerpt); !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if match := f.andMatch(f.NoFilters, excerpt); !match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the filters provided match the bug
|
|
|
|
func (*Filters) orMatch(filters []Filter, excerpt *BugExcerpt) bool {
|
|
|
|
if len(filters) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
match := false
|
|
|
|
for _, f := range filters {
|
|
|
|
match = match || f(excerpt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if all of the filters provided match the bug
|
|
|
|
func (*Filters) andMatch(filters []Filter, excerpt *BugExcerpt) bool {
|
|
|
|
if len(filters) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
match := true
|
|
|
|
for _, f := range filters {
|
|
|
|
match = match && f(excerpt)
|
|
|
|
}
|
|
|
|
|
|
|
|
return match
|
|
|
|
}
|