def parse_pagerduty(message):

    incident_key = message['data']['incident']['incident_key']
    incident_number = message['data']['incident']['incident_number']
    html_url = message['data']['incident']['html_url']
    incident_url = '<a href="%s">#%s</a>' % (html_url, incident_number)

    try:
        alert = db.get_alerts(query={'attributes.incidentKey': incident_key}, limit=1)[0]
    except __HOLE__:
        raise

    from alerta.app import status_code

    if message['type'] == 'incident.trigger':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s assigned to %s' % (incident_url, user)
    elif message['type'] == 'incident.acknowledge':
        status = status_code.ACK
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s acknowledged by %s' % (incident_url, user)
    elif message['type'] == 'incident.unacknowledge':
        status = status_code.OPEN
        text = 'Incident %s unacknowledged due to timeout' % incident_url
    elif message['type'] == 'incident.resolve':
        status = status_code.CLOSED
        if message['data']['incident']['resolved_by_user']:
            user = message['data']['incident']['resolved_by_user']['name']
        else:
            user = 'n/a'
        text = 'Incident %s resolved by %s' % (incident_url, user)
    elif message['type'] == 'incident.assign':
        status = status_code.ASSIGN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s manually assigned to %s' % (incident_url, user)
    elif message['type'] == 'incident.escalate':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s escalated to %s' % (incident_url, user)
    elif message['type'] == 'incident.delegate':
        status = status_code.OPEN
        user = message['data']['incident']['assigned_to_user']['name']
        text = 'Incident %s reassigned due to escalation to %s' % (incident_url, user)
    else:
        status = status_code.UNKNOWN
        text = message['type']

    return alert.id, status, text
dataset/ETHPy150Open guardian/alerta/alerta/app/webhooks/views.py/parse_pagerduty