Skip to content

Commit

Permalink
Merge branch 'master' into enhancement/restrict-priority-for-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
whitdog47 authored Sep 11, 2023
2 parents 823e83a + 7b3ac8c commit 03f7036
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 100 deletions.
6 changes: 3 additions & 3 deletions requirements-base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ frozenlist==1.4.0
# aiosignal
google-api-core==2.11.1
# via google-api-python-client
google-api-python-client==2.97.0
google-api-python-client==2.98.0
# via -r requirements-base.in
google-auth==2.22.0
# via
Expand Down Expand Up @@ -269,7 +269,7 @@ preshed==3.0.8
# via
# spacy
# thinc
protobuf==4.24.2
protobuf==4.24.3
# via
# -r requirements-base.in
# google-api-core
Expand Down Expand Up @@ -308,7 +308,7 @@ pyrate-limiter==2.10.0
# via schemathesis
pyrsistent==0.19.3
# via jsonschema
pytest==7.4.1
pytest==7.4.2
# via
# pytest-subtests
# schemathesis
Expand Down
4 changes: 2 additions & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ executing==1.2.0
# stack-data
factory-boy==3.3.0
# via -r requirements-dev.in
faker==19.3.1
faker==19.4.0
# via
# -r requirements-dev.in
# factory-boy
Expand Down Expand Up @@ -84,7 +84,7 @@ pygments==2.16.1
# via
# devtools
# ipython
pytest==7.4.1
pytest==7.4.2
# via -r requirements-dev.in
python-dateutil==2.8.2
# via faker
Expand Down

This file was deleted.

26 changes: 14 additions & 12 deletions src/dispatch/signal/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
from dispatch.signal import flows as signal_flows
from dispatch.signal import service as signal_service
from dispatch.signal.enums import SignalEngagementStatus
from dispatch.signal.models import (
SignalFilterAction,
SignalInstance,
SignalInstanceCreate,
SignalEnvironment,
)
from dispatch.signal.models import SignalFilterAction, SignalInstance, SignalInstanceCreate
from dispatch.workflow import flows as workflow_flows
from dispatch.entity_type.models import EntityScopeEnum

Expand Down Expand Up @@ -98,28 +93,36 @@ def signal_instance_create_flow(
if not signal_instance.signal.create_case:
return signal_instance

# process signal <-> case overrides
# processes overrides for case creation
# we want the following order of precedence:
# 1. signal instance overrides
# 2. signal definition overrides
# 3. case type defaults

if signal_instance.case_priority:
case_priority = signal_instance.case_priority
else:
case_priority = signal_instance.signal.case_priority

# if the signal has provided a case type use it's values instead of the definitions
conversation_target = None
if signal_instance.case_type:
case_type = signal_instance.case_type
if signal_instance.signal.conversation_target:
conversation_target = signal_instance.case_type.conversation_target
else:
case_type = signal_instance.signal.case_type

if signal_instance.signal.conversation_target:
conversation_target = signal_instance.signal.conversation_target

assignee = None
if signal_instance.signal.oncall_service:
email = service_flows.resolve_oncall(
service=signal_instance.signal.oncall_service, db_session=db_session
)
assignee = {"individual": {"email": email}}

conversation_target = None
if signal_instance.signal.conversation_target:
conversation_target = signal_instance.signal.conversation_target

# create a case if not duplicate or snoozed and case creation is enabled
case_in = CaseCreate(
title=signal_instance.signal.name,
Expand Down Expand Up @@ -172,7 +175,6 @@ def create_signal_instance(
signal = signal_service.get_by_variant_or_external_id(
db_session=db_session,
project_id=project.id,
environment=signal_instance_data.get("environment", SignalEnvironment.PROD),
external_id=signal_instance_data.get("id"),
variant=signal_instance_data["variant"],
)
Expand Down
9 changes: 0 additions & 9 deletions src/dispatch/signal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ class SignalFilterAction(DispatchEnum):
none = "none"


class SignalEnvironment(DispatchEnum):
PROD = "prod"
TEST = "test"


class Signal(Base, TimeStampMixin, ProjectMixin):
id = Column(Integer, primary_key=True)
name = Column(String)
Expand All @@ -146,7 +141,6 @@ class Signal(Base, TimeStampMixin, ProjectMixin):
variant = Column(String)
loopin_signal_identity = Column(Boolean, default=False)
enabled = Column(Boolean, default=False)
environment = Column(String, default=SignalEnvironment.PROD)
case_type_id = Column(Integer, ForeignKey(CaseType.id))
case_type = relationship("CaseType", backref="signals")
case_priority_id = Column(Integer, ForeignKey(CasePriority.id))
Expand Down Expand Up @@ -310,7 +304,6 @@ class SignalBase(DispatchBase):
enabled: Optional[bool] = False
external_url: Optional[str]
create_case: Optional[bool] = True
environment: Optional[SignalEnvironment] = SignalEnvironment.PROD
oncall_service: Optional[Service]
source: Optional[SourceBase]
created_at: Optional[datetime] = None
Expand All @@ -337,7 +330,6 @@ class SignalUpdate(SignalBase):
class SignalRead(SignalBase):
id: PrimaryKey
engagements: Optional[List[SignalEngagementRead]] = []
environment: Optional[SignalEnvironment] = SignalEnvironment.PROD
entity_types: Optional[List[EntityTypeRead]] = []
filters: Optional[List[SignalFilterRead]] = []
workflows: Optional[List[WorkflowRead]] = []
Expand All @@ -352,7 +344,6 @@ class SignalReadMinimal(DispatchBase):
description: Optional[str]
variant: Optional[str]
external_id: str
environment: Optional[SignalEnvironment] = SignalEnvironment.PROD
enabled: Optional[bool] = False
external_url: Optional[str]
create_case: Optional[bool] = True
Expand Down
19 changes: 3 additions & 16 deletions src/dispatch/signal/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,31 +225,18 @@ def get_by_primary_or_external_id(


def get_by_variant_or_external_id(
*,
db_session: Session,
project_id: int,
environment: str,
external_id: str = None,
variant: str = None,
*, db_session: Session, project_id: int, external_id: str = None, variant: str = None
) -> Optional[Signal]:
"""Gets a signal it's external id (and variant if supplied)."""
if variant:
return (
db_session.query(Signal)
.filter(
Signal.project_id == project_id,
Signal.environment == environment,
Signal.variant == variant,
)
.filter(Signal.project_id == project_id, Signal.variant == variant)
.one_or_none()
)
return (
db_session.query(Signal)
.filter(
Signal.project_id == project_id,
Signal.environment == environment,
Signal.external_id == external_id,
)
.filter(Signal.project_id == project_id, Signal.external_id == external_id)
.one_or_none()
)

Expand Down
10 changes: 4 additions & 6 deletions src/dispatch/signal/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from typing import Union

from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Request, Response, status
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request, Response, status, Depends
from pydantic.error_wrappers import ErrorWrapper, ValidationError
from sqlalchemy.exc import IntegrityError

from dispatch.auth.permissions import PermissionsDependency, SensitiveProjectActionPermission
from dispatch.auth.permissions import SensitiveProjectActionPermission, PermissionsDependency
from dispatch.auth.service import CurrentUser
from dispatch.database.core import DbSession
from dispatch.database.service import CommonParameters, search_filter_sort_paginate
Expand All @@ -15,13 +15,11 @@
from dispatch.rate_limiter import limiter
from dispatch.signal import service as signal_service

from .flows import signal_instance_update_flow
from .models import (
SignalCreate,
SignalEngagementCreate,
SignalEngagementPagination,
SignalEngagementRead,
SignalEnvironment,
SignalFilterCreate,
SignalFilterPagination,
SignalFilterRead,
Expand All @@ -46,6 +44,8 @@
update_signal_filter,
)

from .flows import signal_instance_update_flow

router = APIRouter()

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -75,14 +75,12 @@ def create_signal_instance(
if not signal_instance_in.signal:
external_id = signal_instance_in.raw.get("externalId")
variant = signal_instance_in.raw.get("variant")
environment = signal_instance_in.raw.get("environment", SignalEnvironment.PROD)

if external_id or variant:
signal = signal_service.get_by_variant_or_external_id(
db_session=db_session,
project_id=project.id,
external_id=external_id,
environment=environment,
variant=variant,
)

Expand Down
1 change: 0 additions & 1 deletion tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,6 @@ class SignalFactory(BaseFactory):
description = "Test Description"
external_url = "https://test.com"
external_id = "1234"
environment = "test"
variant = "Test Variant"
enabled = True
loopin_signal_identity = False
Expand Down
23 changes: 2 additions & 21 deletions tests/signal/test_signal_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_create_signal_instance(session, signal, case_severity, case_priority, u
case_severity.default = True
case_severity.project_id = signal.project_id

instance_data = {"variant": signal.variant, "environment": "test"}
instance_data = {"variant": signal.variant}

assert create_signal_instance(
db_session=session,
Expand All @@ -31,25 +31,6 @@ def test_create_signal_instance_no_variant(session, signal, case_severity, case_
case_severity.default = True
case_severity.project_id = signal.project_id

instance_data = {"variant": "unknown", "environment": "test"}
with pytest.raises(DispatchException):
create_signal_instance(
db_session=session,
project=signal.project,
signal_instance_data=instance_data,
current_user=user,
)


def test_create_signal_instance_no_environment(session, signal, case_severity, case_priority, user):
from dispatch.signal.flows import create_signal_instance

case_priority.default = True
case_priority.project_id = signal.project_id

case_severity.default = True
case_severity.project_id = signal.project_id

instance_data = {"variant": "unknown"}
with pytest.raises(DispatchException):
create_signal_instance(
Expand All @@ -70,7 +51,7 @@ def test_create_signal_instance_not_enabled(session, signal, case_severity, case
case_severity.project_id = signal.project_id

signal.enabled = False
instance_data = {"variant": signal.variant, "environment": "test"}
instance_data = {"variant": signal.variant}
with pytest.raises(DispatchException):
create_signal_instance(
db_session=session,
Expand Down

0 comments on commit 03f7036

Please sign in to comment.