Skip to content

Commit 1dc777b

Browse files
authored
Merge pull request #6610 from stsewd/protection-for-none-when-sending-notifications
Protection against None when sending notifications
2 parents dd9fa04 + adeeb4a commit 1dc777b

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

docs/guides/build-notifications.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Enabling Build Notifications
22
============================
33

4+
.. note::
5+
6+
Currently we don't send notifications when
7+
a :doc:`build from a pull/merge request fails </guides/autobuild-docs-for-pull-requests>`.
8+
49
Using email
510
-----------
611

readthedocs/projects/tasks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,7 +1269,9 @@ def build_docs_class(self, builder_class):
12691269

12701270
def send_notifications(self, version_pk, build_pk, email=False):
12711271
"""Send notifications on build failure."""
1272-
if self.version.type != EXTERNAL:
1272+
# Try to infer the version type if we can
1273+
# before creating a task.
1274+
if not self.version or self.version.type != EXTERNAL:
12731275
send_notifications.delay(version_pk, build_pk=build_pk, email=email)
12741276

12751277
def is_type_sphinx(self):
@@ -1846,7 +1848,7 @@ def _sync_imported_files(version, build, changed_files):
18461848
def send_notifications(version_pk, build_pk, email=False):
18471849
version = Version.objects.get_object_or_log(pk=version_pk)
18481850

1849-
if not version:
1851+
if not version or version.type == EXTERNAL:
18501852
return
18511853

18521854
build = Build.objects.get(pk=build_pk)

readthedocs/rtd_tests/tests/test_build_notifications.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""Notifications sent after build is completed."""
32

43
import json
@@ -8,6 +7,7 @@
87
from django.test import TestCase
98
from unittest.mock import patch
109

10+
from readthedocs.builds.constants import EXTERNAL
1111
from readthedocs.builds.models import Build, Version
1212
from readthedocs.projects.forms import WebHookForm
1313
from readthedocs.projects.models import EmailHook, Project, WebHook
@@ -19,7 +19,7 @@ def setUp(self):
1919
self.project = fixture.get(Project)
2020
self.version = fixture.get(Version, project=self.project)
2121
self.build = fixture.get(Build, version=self.version)
22-
22+
2323
@patch('readthedocs.builds.managers.log')
2424
def test_send_notification_none_if_wrong_version_pk(self, mock_logger):
2525
self.assertFalse(Version.objects.filter(pk=345343).exists())
@@ -40,6 +40,18 @@ def test_send_webhook_notification(self):
4040

4141
self.assertEqual(len(mail.outbox), 0)
4242

43+
def test_dont_send_webhook_notifications_for_external_versions(self):
44+
fixture.get(WebHook, project=self.project)
45+
self.version.type = EXTERNAL
46+
self.version.save()
47+
48+
with patch('readthedocs.projects.tasks.requests.post') as mock:
49+
mock.return_value = None
50+
send_notifications(self.version.pk, self.build.pk)
51+
mock.assert_not_called()
52+
53+
self.assertEqual(len(mail.outbox), 0)
54+
4355
def test_send_webhook_notification_has_content_type_header(self):
4456
hook = fixture.get(WebHook, project=self.project)
4557
data = json.dumps({
@@ -67,6 +79,14 @@ def test_send_email_notification(self):
6779
send_notifications(self.version.pk, self.build.pk, email=True)
6880
self.assertEqual(len(mail.outbox), 1)
6981

82+
def test_dont_send_email_notifications_for_external_versions(self):
83+
fixture.get(EmailHook, project=self.project)
84+
self.version.type = EXTERNAL
85+
self.version.save()
86+
87+
send_notifications(self.version.pk, self.build.pk, email=True)
88+
self.assertEqual(len(mail.outbox), 0)
89+
7090
def test_send_email_and_webhook__notification(self):
7191
fixture.get(EmailHook, project=self.project)
7292
fixture.get(WebHook, project=self.project)

0 commit comments

Comments
 (0)