Skip to content

Commit 198b37b

Browse files
committed
send Basecamp messages from sentry webhook Handling
1 parent c460f4f commit 198b37b

File tree

10 files changed

+37
-17
lines changed

10 files changed

+37
-17
lines changed

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ fastapi[all]
22
pytest
33
pip-tools
44
pytest-httpx
5+
pytest-mock

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ pytest==7.2.0
7474
# via
7575
# -r requirements.in
7676
# pytest-httpx
77+
# pytest-mock
7778
pytest-httpx==0.21.2
7879
# via -r requirements.in
80+
pytest-mock==3.10.0
81+
# via -r requirements.in
7982
python-dotenv==0.21.0
8083
# via uvicorn
8184
python-multipart==0.0.5

src/basecamp/__init.py__

Whitespace-only changes.

src/basecamp/campfire_bot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88

99
class CampfireBot:
10-
def send_message(self, message: str) -> Response:
10+
11+
@staticmethod
12+
def send_message(message: str) -> Response:
1113
""" https://github.com/basecamp/bc3-api/blob/master/sections/chatbots.md#create-a-line """
1214

13-
url = f'https://3.basecampapi.com/{settings.BASECAMP_ACCOUNT_ID}/integrations/{settings.BASECAMP_CHATBOT_KEY}/buckets/1/chats/2/lines.json'
15+
url = f'https://3.basecampapi.com/{settings.BASECAMP_ACCOUNT_ID}/integrations/{settings.BASECAMP_CHATBOT_KEY}/buckets/{settings.BASECAMP_PROJECT_ID}/chats/{settings.BASECAMP_CHAT_ID}/lines.json'
1416

1517
response = httpx.post(url, data={'content': message})
1618
response.raise_for_status()

src/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Settings(BaseSettings):
1010

1111
BASECAMP_ACCOUNT_ID: str = 'bc-acc-id'
1212
BASECAMP_CHATBOT_KEY: str = 'bc-bot-key'
13+
BASECAMP_PROJECT_ID: int = 111
14+
BASECAMP_CHAT_ID: int = 111
1315

1416
class Config:
1517
env_file = 'src/.env'

src/sentry/api.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@
44
from fastapi import status
55
from fastapi import APIRouter
66

7-
from sentry.types import Webhook
8-
7+
from basecamp.campfire_bot import CampfireBot
8+
from sentry.models import Webhook
99

1010
api_router = APIRouter()
1111

1212

1313
@api_router.post("/api/sentry/webhook", status_code=status.HTTP_200_OK)
1414
async def sentry_webhook(webhook: Webhook, request: Request):
15-
if request.headers.get('sentry-hook-resource') == 'issue':
16-
17-
#todo send webhook.data.get('title') to the Basecamp
18-
19-
return 'ok!'
20-
21-
elif request.headers.get('sentry-hook-resource') == 'event-alert':
22-
23-
return 'ok!'
15+
sentry_hook_resource = request.headers.get('sentry-hook-resource')
2416

17+
if sentry_hook_resource in ['issue', 'event-alert']:
18+
message = webhook.dispatch_webhook_title(sentry_hook_resource)
19+
CampfireBot.send_message(message)
2520
else:
26-
2721
return Response(status_code=status.HTTP_400_BAD_REQUEST)

src/sentry/types.py renamed to src/sentry/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ class Webhook(BaseModel, extra=Extra.allow):
1313
action: str
1414
data: Mapping[str, Any]
1515
installation: Installation
16+
17+
def dispatch_webhook_title(self, sentry_hook_resource: str) -> str:
18+
hook_title = {
19+
'issue': lambda: self.data['issue']['title'],
20+
'event-alert': lambda: self.data['event']['title'],
21+
}
22+
23+
return hook_title.get(sentry_hook_resource)()

src/sentry/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from fastapi.testclient import TestClient
33
from main import app
4-
from sentry.tests.request_data import event, issue
4+
from sentry.tests.mocked_request_data import event, issue
55

66

77
@pytest.fixture
File renamed without changes.

src/sentry/tests/test_webhook.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1+
import pytest
12

2-
def test_response_if_issue(client, issue_resolved_webhook, headers):
3+
4+
@pytest.fixture
5+
def mocked_basecamp_send_message(mocker):
6+
return mocker.patch('basecamp.campfire_bot.CampfireBot.send_message')
7+
8+
9+
def test_response_if_issue(client, issue_resolved_webhook, headers, mocked_basecamp_send_message):
310
response = client.post('/api/sentry/webhook', json=issue_resolved_webhook, headers=headers)
411

512
assert response.status_code == 200
13+
mocked_basecamp_send_message.assert_called_once()
614

715

8-
def test_response_if_alert(client, alert_triggered_webhook, headers):
16+
def test_response_if_alert(client, alert_triggered_webhook, headers, mocked_basecamp_send_message):
17+
headers['sentry-hook-resource'] = 'event-alert'
918
response = client.post('/api/sentry/webhook', json=alert_triggered_webhook, headers=headers)
1019

1120
assert response.status_code == 200
21+
mocked_basecamp_send_message.assert_called_once()
1222

1323

1424
def test_bad_response_if_bad_headers(client, issue_resolved_webhook):

0 commit comments

Comments
 (0)