Skip to content

Commit

Permalink
new pagerduty step (#403)
Browse files Browse the repository at this point in the history
* new pagerduty step

* set true for golive
  • Loading branch information
dustinvanbuskirk authored Oct 14, 2021
1 parent 8379459 commit c7af690
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
5 changes: 5 additions & 0 deletions incubating/pagerduty-alert/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM python:3.9.7-alpine3.14

RUN pip install pdpyras

COPY lib/pagerduty_alert.py /pagerduty_alert.py
53 changes: 53 additions & 0 deletions incubating/pagerduty-alert/lib/pagerduty_alert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from pdpyras import APISession
from pdpyras import ChangeEventsAPISession

def main():

api_token = os.getenv('API_TOKEN')
assignee_user_id = os.getenv('ASSIGNEE_USER_ID')
cf_build_id = os.getenv('CF_BUILD_ID')
cf_build_url = os.getenv('CF_BUILD_URL')
event_source = os.getenv('EVENT_SOURCE')
event_summary = os.getenv('EVENT_SUMMARY')
from_email = os.getenv('FROM_EMAIL')
service_id = os.getenv('SERVICE_ID')
title = os.getenv('TITLE')
pagerduty_type = os.getenv('PAGERDUTY_ALERT_TYPE')

if pagerduty_type == 'incident':
session = APISession(api_token, default_from=from_email)

payload = {
'type': 'incident',
'title': '{}'.format(title),
'service': {
'id': '{}'.format(service_id),
'type': 'service_reference'
},
'assignments': [
{
'assignee': {
'id': '{}'.format(assignee_user_id),
'type': 'user_reference'
}
}
],
}

pd_incident = session.rpost('incidents', json=payload)

elif pagerduty_type == 'change_event':

session = ChangeEventsAPISession(api_token)

pd_change_event = session.submit(
summary='{}'.format(event_summary),
source='{}'.format(event_source),
custom_details={"Build ID":'{}'.format(cf_build_id)},
links=[{'href':'{}'.format(cf_build_url)}]
)


if __name__ == "__main__":
main()
Binary file added incubating/pagerduty-alert/pagerduty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 115 additions & 0 deletions incubating/pagerduty-alert/step.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
kind: step-type
version: '1.0'
metadata:
name: pagerduty-alert
version: 1.0.0
isPublic: true
description: Sends Alerts (Incidents or Change Events) to PagerDuty API
sources:
- https://github.com/codefresh-io/steps/tree/master/incubating/pagerduty-alert
stage: incubating
maintainers:
- name: Dustin Van Buskirk
email: dustin@codefresh.io
categories:
- notifications
official: true
tags:
- notifications
icon:
type: image
url: https://raw.githubusercontent.com/codefresh-io/steps/master/incubating/pagerduty-alert/pagerduty.png
background: "#f4f4f4"
size:
large:
url: >-
https://raw.githubusercontent.com/codefresh-io/steps/master/incubating/pagerduty-alert/pagerduty.png
examples:
- description: create-incident
workflow:
CreateIncident:
type: pagerduty-alert
arguments:
API_TOKEN: x2392fhhgdys867gt3fd
PAGERDUTY_ALERT_TYPE: incident
ASSIGNEE_USER_ID: PONU62J
FROM_EMAIL: dustin@codefresh.io
TITLE: "Codefresh Build Failed: ${{CF_BUILD_URL}}"
SERVICE_ID: 87hsd2fh38gh7g
- description: submit-change-event
workflow:
SubmitChangeEvent:
type: pagerduty-alert
arguments:
API_TOKEN: x2392fhhgdys867gt3fd
PAGERDUTY_ALERT_TYPE: change_event
EVENT_SOURCE: Codefresh
EVENT_SUMMARY: Deployment Completed
spec:
arguments: |-
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"additionalProperties": true,
"patterns": [],
"required": [
"API_TOKEN",
"PAGERDUTY_ALERT_TYPE"
],
"properties": {
"API_TOKEN": {
"type": "string",
"description": "Either User API Token or Routing Key for Change Event Service depending on PAGERDUTY_ALERT type"
},
"PAGERDUTY_ALERT_TYPE": {
"type": "string",
"description": "Alert type to send to PagerDuty",
"default": "change_event",
"enum": [
"incident",
"change_event"
]
},
"ASSIGNEE_USER_ID": {
"type": "string",
"description": "Required for incident type, PagerDuty User ID",
"default": "runlist.yaml"
},
"FROM_EMAIL": {
"type": "string",
"description": "Required for incident type, PagerDuty User Email"
},
"TITLE": {
"type": "string",
"description": "Title of Incident"
},
"SERVICE_ID": {
"type": "string",
"description": "ID of Service for Incident"
},
"EVENT_SOURCE": {
"type": "string",
"description": "Source of Change Event",
"default": "Codefresh"
},
"EVENT_SUMMARY": {
"type": "string",
"description": "Summary of Change Event",
"default": "Build Executed"
}
}
}
stepsTemplate: |-
pagerduty-alert:
name: pagerduty-alert
image: quay.io/codefreshplugins/pagerduty-alert:1.0.0
environment:
[[ range $key, $val := .Arguments ]]
- '[[ $key ]]=[[ $val ]]'
[[- end ]]
commands:
- python3 /pagerduty_alert.py
delimiters:
left: '[['
right: ']]'

0 comments on commit c7af690

Please sign in to comment.