0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-04-11 15:51:19 +00:00

Add support for multiple, comma-separated keywords (cc: )

This commit is contained in:
Pēteris Caune 2020-07-23 12:06:17 +03:00
parent 5acea4c89d
commit 43e56ce788
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2
5 changed files with 42 additions and 13 deletions

View file

@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- Spike.sh integration (#402)
- Updated Discord integration to use discord.com instead of discordapp.com
- Add "Failure Keyword" filtering for inbound emails (#396)
- Add support for multiple, comma-separated keywords (#396)
### Bug Fixes
- Removing Pager Team integration, project appears to be discontinued

View file

@ -12,6 +12,15 @@ RE_UUID = re.compile(
)
def _match(subject, keywords):
for s in keywords.split(","):
s = s.strip()
if s and s in subject:
return True
return False
def _process_message(remote_addr, mailfrom, mailto, data):
to_parts = mailto.split("@")
code = to_parts[0]
@ -33,9 +42,9 @@ def _process_message(remote_addr, mailfrom, mailto, data):
if check.subject or check.subject_fail:
action = "ign"
subject = email.message_from_string(data).get("subject", "")
if check.subject and check.subject in subject:
if check.subject and _match(subject, check.subject):
action = "success"
elif check.subject_fail and check.subject_fail in subject:
elif check.subject_fail and _match(subject, check.subject_fail):
action = "fail"
ua = "Email from %s" % mailfrom

View file

@ -74,3 +74,27 @@ class SmtpdTestCase(BaseTestCase):
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, "ign")
def test_it_handles_multiple_subject_keywords(self):
self.check.subject = "SUCCESS, OK"
self.check.save()
body = PAYLOAD_TMPL % "[OK] Backup completed"
_process_message("1.2.3.4", "foo@example.org", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, None)
def test_it_handles_multiple_subject_fail_keywords(self):
self.check.subject_fail = "FAIL, WARNING"
self.check.save()
body = PAYLOAD_TMPL % "[WARNING] Backup did not complete"
_process_message("1.2.3.4", "foo@example.org", self.email, body.encode("utf8"))
ping = Ping.objects.latest("id")
self.assertEqual(ping.scheme, "email")
self.assertEqual(ping.ua, "Email from foo@example.org")
self.assertEqual(ping.kind, "fail")

View file

@ -73,12 +73,7 @@
</p>
<code>{{ check.url }}</code>
<p>
{% if check.subject %}
Or by sending emails with "{{ check.subject }}"
in the subject line to this address:
{% else %}
Or by sending emails to this address:
{% endif %}
</p>
<code>{{ check.email }}</code>

View file

@ -61,7 +61,7 @@
<div class="form-group">
<label for="update-name-input" class="col-sm-4 control-label">
Success Keyword
Success Keywords
</label>
<div class="col-sm-7">
<input
@ -71,14 +71,14 @@
{% if not check.subject and not check.subject_fail %}disabled{% endif %}
class="input-name form-control filter-by-subject" />
<span class="help-block">
If Subject contains this keyword,
treat it as "success".
Comma-separated list of keywords. If Subject contains
any of the keywords, treat it as "success".
</span>
</div>
</div>
<div class="form-group">
<label for="update-name-input" class="col-sm-4 control-label">
Failure Keyword
Failure Keywords
</label>
<div class="col-sm-7">
<input
@ -88,8 +88,8 @@
{% if not check.subject and not check.subject_fail %}disabled{% endif %}
class="input-name form-control filter-by-subject" />
<span class="help-block">
If Subject contains this keyword,
treat it as "failure".
Comma-separated list of keywords. If Subject contains
any of the keywords, treat it as "failure".
</span>
</div>
</div>