git-bug/cache/filter.go

180 lines
4.4 KiB
Go
Raw Normal View History

2018-09-09 21:19:50 +03:00
package cache
import (
"strings"
2018-09-09 21:19:50 +03:00
"github.com/MichaelMure/git-bug/bug"
)
2019-02-23 15:01:46 +03:00
// Filter is a predicate that match a subset of bugs
2019-02-19 01:16:47 +03:00
type Filter func(repoCache *RepoCache, excerpt *BugExcerpt) 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
}
2019-02-19 01:16:47 +03:00
return func(repoCache *RepoCache, excerpt *BugExcerpt) 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 {
2019-02-19 01:16:47 +03:00
return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
query = strings.ToLower(query)
2019-02-19 01:16:47 +03:00
// Normal identity
if excerpt.AuthorId != "" {
author, ok := repoCache.identitiesExcerpts[excerpt.AuthorId]
if !ok {
panic("missing identity in the cache")
}
return strings.Contains(strings.ToLower(author.Name), query) ||
strings.Contains(strings.ToLower(author.Login), query)
}
// Legacy identity support
return strings.Contains(strings.ToLower(excerpt.LegacyAuthor.Name), query) ||
strings.Contains(strings.ToLower(excerpt.LegacyAuthor.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 {
2019-02-19 01:16:47 +03:00
return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
2018-09-09 21:19:50 +03:00
for _, l := range excerpt.Labels {
if string(l) == label {
return true
}
}
return false
}
}
// ActorFilter return a Filter that match a bug actor
func ActorFilter(actor string) Filter {
return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
for _, identityExcerpt := range repoCache.identitiesExcerpts {
if strings.Contains(strings.ToLower(identityExcerpt.Name), actor) ||
actor == identityExcerpt.Id || actor == identityExcerpt.Login {
for _, actorId := range excerpt.Actors {
if identityExcerpt.Id == actorId {
return true
}
}
}
}
return false
}
}
// ParticipantFilter return a Filter that match a bug participant
func ParticipantFilter(participant string) Filter {
return func(repoCache *RepoCache, excerpt *BugExcerpt) bool {
for _, identityExcerpt := range repoCache.identitiesExcerpts {
if strings.Contains(strings.ToLower(identityExcerpt.Name), participant) ||
participant == identityExcerpt.Id || participant == identityExcerpt.Login {
for _, participantId := range excerpt.Participants {
if identityExcerpt.Id == participantId {
return true
}
}
}
}
return false
}
}
// TitleFilter return a Filter that match if the title contains the given query
func TitleFilter(query string) Filter {
2019-03-02 21:28:15 +03:00
return func(repo *RepoCache, excerpt *BugExcerpt) bool {
return strings.Contains(
strings.ToLower(excerpt.Title),
strings.ToLower(query),
)
}
}
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 {
2019-02-19 01:16:47 +03:00
return func(repoCache *RepoCache, excerpt *BugExcerpt) 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 {
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
2019-02-19 01:16:47 +03:00
func (f *Filters) Match(repoCache *RepoCache, excerpt *BugExcerpt) bool {
if match := f.orMatch(f.Status, repoCache, excerpt); !match {
2018-09-09 21:19:50 +03:00
return false
}
2019-02-19 01:16:47 +03:00
if match := f.orMatch(f.Author, repoCache, excerpt); !match {
2018-09-09 21:19:50 +03:00
return false
}
if match := f.orMatch(f.Participant, repoCache, excerpt); !match {
return false
}
if match := f.orMatch(f.Actor, repoCache, excerpt); !match {
return false
}
if match := f.andMatch(f.Label, repoCache, excerpt); !match {
2018-09-09 21:19:50 +03:00
return false
}
2019-02-19 01:16:47 +03:00
if match := f.andMatch(f.NoFilters, repoCache, excerpt); !match {
2018-09-09 21:19:50 +03:00
return false
}
2019-03-02 21:28:15 +03:00
if match := f.andMatch(f.Title, repoCache, excerpt); !match {
return false
}
2018-09-09 21:19:50 +03:00
return true
}
// Check if any of the filters provided match the bug
2019-02-19 01:16:47 +03:00
func (*Filters) orMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
2018-09-09 21:19:50 +03:00
if len(filters) == 0 {
return true
}
match := false
for _, f := range filters {
2019-02-19 01:16:47 +03:00
match = match || f(repoCache, excerpt)
2018-09-09 21:19:50 +03:00
}
return match
}
// Check if all of the filters provided match the bug
2019-02-19 01:16:47 +03:00
func (*Filters) andMatch(filters []Filter, repoCache *RepoCache, excerpt *BugExcerpt) bool {
2018-09-09 21:19:50 +03:00
if len(filters) == 0 {
return true
}
match := true
for _, f := range filters {
2019-02-19 01:16:47 +03:00
match = match && f(repoCache, excerpt)
2018-09-09 21:19:50 +03:00
}
return match
}