Skip to content

Commit 111a087

Browse files
committed
feat(filter/regexTitle): add IncludeRegexp property
1 parent 4c789a6 commit 111a087

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,13 @@ filters:
197197
config:
198198
HourStart: 8
199199
HourEnd: 17
200-
# Events where the title matches the ExcludeRegexp (RE2 Regex) aren't synced
200+
# Include / Exclude events based on the title (RE2 Regex)
201+
# Events are included by default, ExcludeRegexp defines excluded events, IncludeRegexp defines exception from exclusion
202+
# If only inclusion should be defined, use ".*" for ExcludeRegexp
201203
- name: RegexTitle
202204
config:
203205
ExcludeRegexp: ".*test"
206+
IncludeRegexp: ".*test-but-included"
204207
```
205208

206209
## Auth

example.sync.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,13 @@ filters:
6969
config:
7070
HourStart: 8
7171
HourEnd: 17
72-
# Events where the title matches the ExcludeRegexp (RE2 Regex) aren't synced
72+
# Include / Exclude events based on the title (RE2 Regex)
73+
# Events are included by default, ExcludeRegexp defines excluded events, IncludeRegexp defines exception from exclusion
74+
# If only inclusion should be defined, use ".*" for ExcludeRegexp
7375
- name: RegexTitle
7476
config:
7577
ExcludeRegexp: ".*test"
78+
IncludeRegexp: ".*test-but-included"
7679

7780
# Perform multiple calendar updates concurrently
7881
# Defaults to 1 if not set

internal/filter/regexTitle.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,56 @@ import (
1010

1111
type RegexTitle struct {
1212
ExcludeRegexp string
13+
IncludeRegexp string
1314
}
1415

1516
func (a RegexTitle) Name() string {
1617
return "RegexTitle"
1718
}
1819

1920
func (a RegexTitle) Filter(event models.Event) bool {
21+
// Filter() return true if the event should be kept
2022

21-
if len(a.ExcludeRegexp) == 0 {
22-
log.Debugf("Regular Expression is empty, skipping Filter %s for event: %s", a.Name(), event.Title)
23+
// special case nothing excluded or included: keep all
24+
if len(a.ExcludeRegexp) == 0 && len(a.IncludeRegexp) == 0 {
2325
return true
2426
}
2527

28+
// matchExclude defaults to false (exclude no events)
29+
matchExclude := false
30+
if len(a.ExcludeRegexp) > 0 {
31+
matchExclude = a.MatchRegex(event, a.ExcludeRegexp)
32+
}
33+
// matchInclude defaults to false (don't include)
34+
matchInclude := false
35+
if len(a.IncludeRegexp) > 0 {
36+
matchInclude = a.MatchRegex(event, a.IncludeRegexp)
37+
}
38+
39+
// use allow - deny - allow pattern
40+
// (default is allow, then excluded are denied, but included are allowed again)
41+
if matchInclude && matchExclude {
42+
return true
43+
} else if matchExclude {
44+
return false
45+
} else {
46+
return true
47+
}
48+
}
49+
50+
func (a RegexTitle) MatchRegex(event models.Event, re string) bool {
2651
log.Debugf("Running Regexp %s on event title: %s", a.ExcludeRegexp, event.Title)
2752

28-
r, err := regexp.Compile(a.ExcludeRegexp)
53+
r, err := regexp.Compile(re)
2954
if err != nil {
3055
log.Fatalf("Regular expression of Filter %s is not valid, please check", a.Name())
3156
}
3257

3358
// if the title matches the Regexp, return false (filter the event)
3459
if r.MatchString(event.Title) {
35-
log.Debugf("Regular Expression %s matches the events title: %s, gets filtered", a.Name(), event.Title)
36-
return false
60+
log.Debugf("Regular Expression %s matches the events title: %s", a.Name(), event.Title)
61+
return true
3762
}
3863

39-
return true
64+
return false
4065
}

internal/filter/regexTitle_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ var sourceEvents = []models.Event{
2323
{
2424
ICalUID: "testId3",
2525
ID: "testUid2",
26-
Title: "foo",
26+
Title: "taste",
2727
Description: "bar",
2828
},
2929
}
3030

3131
// Some events should be filtered
32-
func TestRegexTitleFilter(t *testing.T) {
32+
func TestRegexTitleFilterExclude(t *testing.T) {
3333

3434
expectedSinkEvents := []models.Event{sourceEvents[1], sourceEvents[2]}
3535

@@ -48,3 +48,23 @@ func TestRegexTitleFilterEmptyRegex(t *testing.T) {
4848
}
4949
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents)
5050
}
51+
52+
// Only the included events should be there
53+
func TestRegexTitleFilterInclude(t *testing.T) {
54+
expectedSinkEvents := []models.Event{sourceEvents[0], sourceEvents[1]}
55+
eventFilter := filter.RegexTitle{
56+
ExcludeRegexp: ".*",
57+
IncludeRegexp: "[tT]est.*",
58+
}
59+
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents)
60+
}
61+
62+
// the excluded events should be excluded but the included should be included again
63+
func TestRegexTitleFilterExcludeInclude(t *testing.T) {
64+
expectedSinkEvents := []models.Event{sourceEvents[0]}
65+
eventFilter := filter.RegexTitle{
66+
ExcludeRegexp: "t.*",
67+
IncludeRegexp: "test.*",
68+
}
69+
checkEventFilter(t, eventFilter, sourceEvents, expectedSinkEvents)
70+
}

0 commit comments

Comments
 (0)