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"
|
2020-02-08 19:12:49 +03:00
|
|
|
"github.com/MichaelMure/git-bug/entity"
|
2018-09-09 21:19:50 +03:00
|
|
|
)
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
// resolver has the resolving functions needed by filters.
|
|
|
|
// This exist mainly to go through the functions of the cache with proper locking.
|
|
|
|
type resolver interface {
|
|
|
|
ResolveIdentityExcerpt(id entity.Id) (*IdentityExcerpt, error)
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:01:46 +03:00
|
|
|
// Filter is a predicate that match a subset of bugs
|
2020-02-08 19:12:49 +03:00
|
|
|
type Filter func(excerpt *BugExcerpt, resolver resolver) bool
|
2018-09-09 21:19:50 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2018-09-09 21:19:50 +03:00
|
|
|
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 {
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2019-01-20 17:41:27 +03:00
|
|
|
query = strings.ToLower(query)
|
|
|
|
|
2019-02-19 01:16:47 +03:00
|
|
|
// Normal identity
|
|
|
|
if excerpt.AuthorId != "" {
|
2020-02-08 19:12:49 +03:00
|
|
|
author, err := resolver.ResolveIdentityExcerpt(excerpt.AuthorId)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-02-19 01:16:47 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:28:41 +03:00
|
|
|
return author.Match(query)
|
2019-02-19 01:16:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Legacy identity support
|
2020-01-24 02:30:13 +03:00
|
|
|
return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), 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 {
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2018-09-09 21:19:50 +03:00
|
|
|
for _, l := range excerpt.Labels {
|
|
|
|
if string(l) == label {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-31 23:32:35 +03:00
|
|
|
// ActorFilter return a Filter that match a bug actor
|
2019-04-04 13:58:20 +03:00
|
|
|
func ActorFilter(query string) Filter {
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2019-04-04 13:58:20 +03:00
|
|
|
query = strings.ToLower(query)
|
|
|
|
|
|
|
|
for _, id := range excerpt.Actors {
|
2020-02-08 19:12:49 +03:00
|
|
|
identityExcerpt, err := resolver.ResolveIdentityExcerpt(id)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-04-04 13:58:20 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:28:41 +03:00
|
|
|
if identityExcerpt.Match(query) {
|
2019-04-04 13:58:20 +03:00
|
|
|
return true
|
2019-03-31 23:32:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParticipantFilter return a Filter that match a bug participant
|
2019-04-04 13:58:20 +03:00
|
|
|
func ParticipantFilter(query string) Filter {
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2019-04-04 13:58:20 +03:00
|
|
|
query = strings.ToLower(query)
|
|
|
|
|
|
|
|
for _, id := range excerpt.Participants {
|
2020-02-08 19:12:49 +03:00
|
|
|
identityExcerpt, err := resolver.ResolveIdentityExcerpt(id)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-04-04 13:58:20 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 17:28:41 +03:00
|
|
|
if identityExcerpt.Match(query) {
|
2019-04-04 13:58:20 +03:00
|
|
|
return true
|
2019-03-31 23:32:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 17:23:20 +03:00
|
|
|
// TitleFilter return a Filter that match if the title contains the given query
|
|
|
|
func TitleFilter(query string) Filter {
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2019-03-03 17:23:20 +03:00
|
|
|
return strings.Contains(
|
|
|
|
strings.ToLower(excerpt.Title),
|
|
|
|
strings.ToLower(query),
|
|
|
|
)
|
2019-02-28 00:19:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-02-08 19:12:49 +03:00
|
|
|
return func(excerpt *BugExcerpt, resolver resolver) bool {
|
2018-09-09 21:19:50 +03:00
|
|
|
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 {
|
2019-03-31 23:32:35 +03:00
|
|
|
Status []Filter
|
|
|
|
Author []Filter
|
|
|
|
Actor []Filter
|
|
|
|
Participant []Filter
|
|
|
|
Label []Filter
|
|
|
|
Title []Filter
|
|
|
|
NoFilters []Filter
|
2018-09-09 21:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Match check if a bug match the set of filters
|
2020-02-08 19:12:49 +03:00
|
|
|
func (f *Filters) Match(excerpt *BugExcerpt, resolver resolver) bool {
|
|
|
|
if match := f.orMatch(f.Status, excerpt, resolver); !match {
|
2018-09-09 21:19:50 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
if match := f.orMatch(f.Author, excerpt, resolver); !match {
|
2018-09-09 21:19:50 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
if match := f.orMatch(f.Participant, excerpt, resolver); !match {
|
2019-03-31 23:32:35 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
if match := f.orMatch(f.Actor, excerpt, resolver); !match {
|
2019-03-31 23:32:35 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
if match := f.andMatch(f.Label, excerpt, resolver); !match {
|
2018-09-09 21:19:50 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
if match := f.andMatch(f.NoFilters, excerpt, resolver); !match {
|
2018-09-09 21:19:50 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-08 19:12:49 +03:00
|
|
|
if match := f.andMatch(f.Title, excerpt, resolver); !match {
|
2019-02-28 00:19:35 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-09-09 21:19:50 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the filters provided match the bug
|
2020-02-08 19:12:49 +03:00
|
|
|
func (*Filters) orMatch(filters []Filter, excerpt *BugExcerpt, resolver resolver) bool {
|
2018-09-09 21:19:50 +03:00
|
|
|
if len(filters) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
match := false
|
|
|
|
for _, f := range filters {
|
2020-02-08 19:12:49 +03:00
|
|
|
match = match || f(excerpt, resolver)
|
2018-09-09 21:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return match
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if all of the filters provided match the bug
|
2020-02-08 19:12:49 +03:00
|
|
|
func (*Filters) andMatch(filters []Filter, excerpt *BugExcerpt, resolver resolver) bool {
|
2018-09-09 21:19:50 +03:00
|
|
|
if len(filters) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
match := true
|
|
|
|
for _, f := range filters {
|
2020-02-08 19:12:49 +03:00
|
|
|
match = match && f(excerpt, resolver)
|
2018-09-09 21:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return match
|
|
|
|
}
|