Exemple #1
0
def handle_release_commits_updated(removed_commit_ids, added_commit_ids,
                                   **kwargs):
    from sentry.incidents.tasks import calculate_incident_suspects
    incident_ids = IncidentSuspectCommit.objects.filter(
        commit_id__in=removed_commit_ids | added_commit_ids, ).values_list(
            'incident_id', flat=True).distinct()
    for incident_id in incident_ids:
        calculate_incident_suspects.apply_async(
            kwargs={'incident_id': incident_id})
Exemple #2
0
def create_incident(
    organization,
    type,
    title,
    query,
    date_started,
    date_detected=None,
    detection_uuid=None,
    projects=None,
    groups=None,
    user=None,
):
    if date_detected is None:
        date_detected = date_started

    if groups:
        group_projects = [g.project for g in groups]
        if projects is None:
            projects = []
        projects = list(set(projects + group_projects))

    with transaction.atomic():
        incident = Incident.objects.create(
            organization=organization,
            detection_uuid=detection_uuid,
            status=IncidentStatus.OPEN.value,
            type=type.value,
            title=title,
            query=query,
            date_started=date_started,
            date_detected=date_detected,
        )
        if projects:
            IncidentProject.objects.bulk_create([
                IncidentProject(incident=incident, project=project)
                for project in projects
            ])
        if groups:
            IncidentGroup.objects.bulk_create([
                IncidentGroup(incident=incident, group=group)
                for group in groups
            ])

        if type == IncidentType.CREATED:
            activity_status = IncidentActivityType.CREATED
        else:
            activity_status = IncidentActivityType.DETECTED

        event_stats_snapshot = create_initial_event_stats_snapshot(incident)
        create_incident_activity(
            incident,
            activity_status,
            event_stats_snapshot=event_stats_snapshot,
            user=user,
        )
        analytics.record(
            'incident.created',
            incident_id=incident.id,
            organization_id=incident.organization_id,
            incident_type=type.value,
        )

    calculate_incident_suspects.apply_async(
        kwargs={'incident_id': incident.id})
    return incident