feat(filters): add regex support (#258)

This commit is contained in:
Ludvig Lundgren 2022-04-30 14:53:24 +02:00 committed by GitHub
parent e6c151a029
commit 5d032dd075
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package domain
import (
"context"
"regexp"
"strconv"
"strings"
"time"
@ -113,13 +114,24 @@ func (f Filter) CheckFilter(r *Release) ([]string, bool) {
}
// matchRelease
// TODO allow to match against regex
if f.MatchReleases != "" && !containsFuzzy(r.TorrentName, f.MatchReleases) {
r.addRejectionF("match release not matching. got: %v want: %v", r.TorrentName, f.MatchReleases)
}
// match against regex
if f.UseRegex {
if f.MatchReleases != "" && !matchRegex(r.TorrentName, f.MatchReleases) {
r.addRejectionF("match release regex not matching. got: %v want: %v", r.TorrentName, f.MatchReleases)
}
if f.ExceptReleases != "" && containsFuzzy(r.TorrentName, f.ExceptReleases) {
r.addRejectionF("except releases: unwanted release. got: %v want: %v", r.TorrentName, f.ExceptReleases)
if f.ExceptReleases != "" && matchRegex(r.TorrentName, f.ExceptReleases) {
r.addRejectionF("except releases regex: unwanted release. got: %v want: %v", r.TorrentName, f.ExceptReleases)
}
} else {
if f.MatchReleases != "" && !containsFuzzy(r.TorrentName, f.MatchReleases) {
r.addRejectionF("match release not matching. got: %v want: %v", r.TorrentName, f.MatchReleases)
}
if f.ExceptReleases != "" && containsFuzzy(r.TorrentName, f.ExceptReleases) {
r.addRejectionF("except releases: unwanted release. got: %v want: %v", r.TorrentName, f.ExceptReleases)
}
}
if f.MatchReleaseGroups != "" && !contains(r.Group, f.MatchReleaseGroups) {
@ -307,6 +319,15 @@ func (f Filter) checkSizeFilter(r *Release, minSize string, maxSize string) bool
return true
}
func matchRegex(tag string, filter string) bool {
re, err := regexp.Compile(`(?i)(?:` + filter + `)`)
if err != nil {
return false
}
return re.MatchString(tag)
}
// checkFilterIntStrings "1,2,3-20"
func containsIntStrings(value int, filterList string) bool {
filters := strings.Split(filterList, ",")