Skip to content

Commit

Permalink
Merge pull request #4333 from opensafely-core/airlock-unknown-events
Browse files Browse the repository at this point in the history
Handle unknown airlock event types
  • Loading branch information
rebkwok committed May 14, 2024
2 parents d0d619d + 1570f39 commit 29700fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 7 additions & 3 deletions airlock/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ class AirlockEvent:

@classmethod
def from_payload(cls, data):
event_type = EventType[data.get("event_type").upper()]
event_type = data.get("event_type").upper()
try:
event_type = EventType[event_type]
except KeyError:
raise NotificationError(f"Unknown event type '{event_type}'")
updates = data.get("updates") or []
request_author = User.objects.get(username=data.get("request_author"))
username = data.get("user")
Expand Down Expand Up @@ -147,6 +151,7 @@ def email_author(airlock_event: AirlockEvent):
EVENT_NOTIFICATIONS = {
EventType.REQUEST_SUBMITTED: [create_issue],
EventType.REQUEST_WITHDRAWN: [close_issue],
EventType.REQUEST_APPROVED: [],
EventType.REQUEST_RELEASED: [email_author, close_issue],
EventType.REQUEST_REJECTED: [email_author, close_issue],
EventType.REQUEST_UPDATED: [email_author, update_issue],
Expand All @@ -160,9 +165,8 @@ def airlock_event_view(request):
# do token authentication
get_backend_from_token(token)

airlock_event = AirlockEvent.from_payload(request.data)

try:
airlock_event = AirlockEvent.from_payload(request.data)
handle_notifications(airlock_event)
except NotificationError as e:
return Response({"status": "error", "message": str(e)}, status=201)
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/airlock/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def close_issue(*args, **kwargs):
@pytest.mark.parametrize(
"event_type,author_is_user,updates,email_sent",
[
# No action for request_approved
("request_approved", True, None, False),
# author and user are different; emails are sent for rejected/released
("request_submitted", True, None, False),
("request_rejected", True, None, True),
Expand Down Expand Up @@ -162,6 +164,7 @@ def test_api_post_release_request_custom_org_and_repo(mock_create_issue, api_rf)
[{"update_type": "file_added", "group": "Group 1", "user": "user"}],
"Error creating GitHub issue comment",
),
("bad_event_type", None, "Unknown event type 'BAD_EVENT_TYPE'"),
],
)
@patch("airlock.views._get_github_api", FakeGithubApiWithError)
Expand Down

0 comments on commit 29700fc

Please sign in to comment.