diff --git a/external_references/Jira/README.md b/external_references/Jira/README.md new file mode 100644 index 0000000..ad12a82 --- /dev/null +++ b/external_references/Jira/README.md @@ -0,0 +1,73 @@ +# PagerDuty to Jira Incident Mapping +The `pagerduty_jira_incidents.py` script sends a GET request to the incidents endpoint to retrieve incident data, filters for incidents containing Jira external_references, and then generates a CSV file mapping the PagerDuty incidents to their corresponding Jira tickets: + +- PagerDuty Incident Number +- Title +- Description +- Created At +- Updated At +- Status +- PagerDuty Incident URL +- Jira Ticket ID +- Jira Ticket URL + +## Requirements + +- Python 3.6+ +- PagerDuty API Access Token + +## Installation + +1. Clone this repository + ```bash + git clone https://github.com/PagerDuty/public-support-scripts.git + ``` + +2. `cd` to the Jira directory within `external_references`. + +3. Install required dependencies: + ```bash + pip install -r requirements.txt + ``` + +## Usage + +1. Create a `.env` file in the Jira directory. + +2. Update the `.env` file with your PagerDuty API key: + ``` + PAGERDUTY_REST_API_KEY=your_api_key_here + ``` + +3. Define your request parameters in the `request_parameters.py` file. + +4. Run the `pagerduty_jira_incidents.py` script in the Jira directory: + ```bash + python3 pagerduty_jira_incidents.py + ``` + +## How the Script Works + +The `pagerduty_jira_incidents.py` script will: + +1. Send a GET request to the incidents endpoint to retrieve incident data +2. Filter incidents to include only those with Jira external_references +3. Generate a CSV file named `pagerduty_incidents_mapped_to_jira.csv` in the Jira directory. + +## Error Handling + +The `pagerduty_jira_incidents.py` script will exit with an error message in the following cases: + +- If the PagerDuty API request fails +- If no incidents with Jira external_references are found + +## Security Notes + +1. Never commit your `.env` file +2. User Token Rest API Keys are restricted in accordance with the user's access and permissions, so the GET request to the incidents endpoint will only retrieve incidents that the user has access to. General Access Rest API Keys do not have such restrictions. + +## Resources + +1. [List incidents endpoint documentation](https://developer.pagerduty.com/api-reference/9d0b4b12e36f9-list-incidents) +2. [Pagination documentation](https://developer.pagerduty.com/docs/pagination) +3. [API Access Keys documentation](https://support.pagerduty.com/main/docs/api-access-keys) \ No newline at end of file diff --git a/external_references/Jira/pagerduty_jira_incidents.py b/external_references/Jira/pagerduty_jira_incidents.py new file mode 100644 index 0000000..f6a0e98 --- /dev/null +++ b/external_references/Jira/pagerduty_jira_incidents.py @@ -0,0 +1,127 @@ +import requests +import json +import csv +import os + +from dotenv import load_dotenv + +import request_parameters as parameters + +load_dotenv() +PAGERDUTY_REST_API_KEY = os.getenv("PAGERDUTY_REST_API_KEY") + + +def get_incidents(): + """ + Makes a GET request to the incidents endpoint to retrieve incident data. + + Raises: + SystemExit: If the API request fails or returns an error. + + Returns: + dict: JSON response containing incident data if successful. + """ + url = "https://api.pagerduty.com/incidents" + + querystring = {"include[]":"external_references"} + + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": f"Token token={PAGERDUTY_REST_API_KEY}" + } + + if parameters.since: + querystring["since"] = parameters.since + if parameters.until: + querystring["until"] = parameters.until + if parameters.limit: + querystring["limit"] = parameters.limit + if parameters.offset: + querystring["offset"] = parameters.offset + + try: + response = requests.get(url, headers=headers, params=querystring) + response.raise_for_status() + if response.text: + return response.json() + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + +def filter_incidents(incidents): + """ + Filter incidents to only include those with Jira external_reference. + + Iterates through the incidents and keeps only those containing + Jira external_reference. + + Args: + incidents (dict): JSON dict containing incident data returned from the GET request + to the incidents endpoint. + + Raises: + SystemExit: If no incidents with Jira external_reference are found. + + Returns: + list: incidents containing Jira external_reference. + """ + incidents_with_jira_reference = [] + jira_incidents_set = set() + + for incident in incidents["incidents"]: + if incident["external_references"] != []: + for reference in incident["external_references"]: + if "atlassian" in reference["external_url"] or "Jira issue" in reference["summary"]: + if incident["id"] not in jira_incidents_set: + incidents_with_jira_reference.append(incident) + jira_incidents_set.add(incident["id"]) + + if not incidents_with_jira_reference: + raise SystemExit("No Jira external_reference found in any incidents") + + return incidents_with_jira_reference + +def generate_csv(filtered_incidents): + """ + Generate a CSV file mapping PagerDuty incidents to Jira data. + + Creates a CSV file with relevant PagerDuty incident information (PagerDuty Incident Number", "Title", "Description", + "Created At", "Updated At", "Status", "PagerDuty Incident URL") and + the corresponding Jira ticket IDs and Jira ticket URLs. + + Args: + filtered_incidents (list): List of incidents containing Jira external_reference. + """ + csv_filename = "pagerduty_incidents_mapped_to_jira.csv" + # Define the header for the CSV + headers = ["PagerDuty Incident Number", "Title", "Description", "Created At", "Updated At", "Status", "PagerDuty Incident URL", "Jira Ticket ID", "Jira Ticket URL"] + # Write to CSV file + with open(csv_filename, mode="w", newline="", encoding="utf-8") as file: + writer = csv.DictWriter(file, fieldnames=headers) + writer.writeheader() + + for incident in filtered_incidents: + for reference in incident["external_references"]: + if "atlassian" in reference["external_url"] or "Jira issue" in reference["summary"]: + writer.writerow({ + "PagerDuty Incident Number": incident.get("incident_number", ""), + "Title": incident.get("title", ""), + "Description": incident.get("description", ""), + "Created At": incident.get("created_at", ""), + "Updated At": incident.get("updated_at", ""), + "Status": incident.get("status", ""), + "PagerDuty Incident URL": incident.get("html_url", ""), + "Jira Ticket ID": reference["external_id"], + "Jira Ticket URL": reference["external_url"], + }) + + +def main(): + incidents = get_incidents() + filtered_incidents = filter_incidents(incidents) + csv = generate_csv(filtered_incidents) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/external_references/Jira/request_parameters.py b/external_references/Jira/request_parameters.py new file mode 100644 index 0000000..36475c3 --- /dev/null +++ b/external_references/Jira/request_parameters.py @@ -0,0 +1,4 @@ +since = "" +until = "" +limit = None +offset = None \ No newline at end of file diff --git a/external_references/Jira/requirements.txt b/external_references/Jira/requirements.txt new file mode 100644 index 0000000..323ef8c --- /dev/null +++ b/external_references/Jira/requirements.txt @@ -0,0 +1,2 @@ +python-dotenv +requests \ No newline at end of file diff --git a/external_references/ServiceNow/README.md b/external_references/ServiceNow/README.md new file mode 100644 index 0000000..cb95211 --- /dev/null +++ b/external_references/ServiceNow/README.md @@ -0,0 +1,74 @@ +# PagerDuty to ServiceNow Incident Mapping +The `pagerduty_servicenow_incidents.py` script sends a GET request to the incidents endpoint to retrieve incident data, filters for incidents containing ServiceNow metadata, and then generates a CSV file mapping the PagerDuty incidents to their corresponding ServiceNow incidents: + +- PagerDuty Incident Number +- Title +- Description +- Created At +- Updated At +- Status +- PagerDuty Incident URL +- ServiceNow Incident ID +- ServiceNow Incident URL + +## Requirements + +- Python 3.6+ +- PagerDuty API Access Token +- ServiceNow v3 webhooks + +## Installation + +1. Clone this repository + ```bash + git clone https://github.com/PagerDuty/public-support-scripts.git + ``` + +2. `cd` to the ServiceNow directory within `external_references`. + +3. Install required dependencies: + ```bash + pip install -r requirements.txt + ``` + +## Usage + +1. Create a `.env` file in the ServiceNow directory. + +2. Update the `.env` file with your PagerDuty API key: + ``` + PAGERDUTY_REST_API_KEY=your_api_key_here + ``` + +3. Define your request parameters in the `request_parameters.py` file. + +4. Run the `pagerduty_servicenow_incidents.py` script in the ServiceNow directory: + ```bash + python3 pagerduty_servicenow_incidents.py + ``` + +## How the Script Works + +The `pagerduty_servicenow_incidents.py` script will: + +1. Send a GET request to the incidents endpoint to retrieve incident data +2. Filter incidents to include only those with ServiceNow references +3. Generate a CSV file named `pagerduty_incidents_mapped_to_servicenow.csv` in the ServiceNow directory. + +## Error Handling + +The `pagerduty_servicenow_incidents.py` script will exit with an error message in the following cases: + +- If the PagerDuty API request fails +- If no incidents with ServiceNow metadata are found + +## Security Notes + +1. Never commit your `.env` file +2. User Token Rest API Keys are restricted in accordance with the user's access and permissions, so the GET request to the incidents endpoint will only retrieve incidents that the user has access to. General Access Rest API Keys do not have such restrictions. + +## Resources + +1. [List incidents endpoint documentation](https://developer.pagerduty.com/api-reference/9d0b4b12e36f9-list-incidents) +2. [Pagination documentation](https://developer.pagerduty.com/docs/pagination) +3. [API Access Keys documentation](https://support.pagerduty.com/main/docs/api-access-keys) \ No newline at end of file diff --git a/external_references/ServiceNow/pagerduty_servicenow_incidents.py b/external_references/ServiceNow/pagerduty_servicenow_incidents.py new file mode 100644 index 0000000..b78e863 --- /dev/null +++ b/external_references/ServiceNow/pagerduty_servicenow_incidents.py @@ -0,0 +1,126 @@ +import requests +import json +import csv +import os + +from dotenv import load_dotenv + +import request_parameters as parameters + +load_dotenv() +PAGERDUTY_REST_API_KEY = os.getenv("PAGERDUTY_REST_API_KEY") + + +def get_incidents(): + """ + Makes a GET request to the incidents endpoint to retrieve incident data. + + Raises: + SystemExit: If the API request fails or returns an error. + + Returns: + dict: JSON response containing incident data if successful. + """ + url = "https://api.pagerduty.com/incidents" + + querystring = {"include[]":"metadata"} + + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": f"Token token={PAGERDUTY_REST_API_KEY}" + } + + if parameters.since: + querystring["since"] = parameters.since + if parameters.until: + querystring["until"] = parameters.until + if parameters.limit: + querystring["limit"] = parameters.limit + if parameters.offset: + querystring["offset"] = parameters.offset + + try: + response = requests.get(url, headers=headers, params=querystring) + response.raise_for_status() + if response.text: + return response.json() + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + +def filter_incidents(incidents): + """ + Filter incidents to only include those with ServiceNow metadata. + + Iterates through the incidents and keeps only those containing + servicenow metadata. + + Args: + incidents (dict): JSON dict containing incident data returned from the GET request + to the incidents endpoint. + + Raises: + SystemExit: If no incidents with ServiceNow metadata are found. + + Returns: + list: incidents containing ServiceNow metadata. + """ + incidents_with_metadata = [] + for incident in incidents["incidents"]: + if incident["metadata"] != []: + #only include incidents with servicenow metadata + if any("servicenow" in key.lower() for key in incident["metadata"]): + incidents_with_metadata.append(incident) + + if not incidents_with_metadata: + raise SystemExit("No ServiceNow metadata found in any incidents") + + return incidents_with_metadata + + +def generate_csv(filtered_incidents): + """ + Generate a CSV file mapping PagerDuty incidents to ServiceNow data. + + Creates a CSV file with relevant PagerDuty incident information (PagerDuty Incident Number", "Title", "Description", + "Created At", "Updated At", "Status", "PagerDuty Incident URL") and + the corresponding ServiceNow incident IDs and incident URLs. + + Args: + filtered_incidents (list): List of incidents containing ServiceNow metadata. + """ + csv_filename = "pagerduty_incidents_mapped_to_servicenow.csv" + # Define the header for the CSV + headers = ["PagerDuty Incident Number", "Title", "Description", "Created At", "Updated At", "Status", "PagerDuty Incident URL", "ServiceNow Incident ID", "ServiceNow Incident URL"] + # Write to CSV file + with open(csv_filename, mode="w", newline="", encoding="utf-8") as file: + writer = csv.DictWriter(file, fieldnames=headers) + writer.writeheader() + + for incident in filtered_incidents: + for key, value in incident["metadata"].items(): + if "servicenow" in key: + servicenow_value = json.loads(value) + external_name = servicenow_value.get("external_name", "") + external_url = servicenow_value.get("external_url", "") + writer.writerow({ + "PagerDuty Incident Number": incident.get("incident_number", ""), + "Title": incident.get("title", ""), + "Description": incident.get("description", ""), + "Created At": incident.get("created_at", ""), + "Updated At": incident.get("updated_at", ""), + "Status": incident.get("status", ""), + "PagerDuty Incident URL": incident.get("html_url", ""), + "ServiceNow Incident ID": external_name, + "ServiceNow Incident URL": external_url + }) + +def main(): + incidents = get_incidents() + filtered_incidents = filter_incidents(incidents) + csv = generate_csv(filtered_incidents) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/external_references/ServiceNow/request_parameters.py b/external_references/ServiceNow/request_parameters.py new file mode 100644 index 0000000..36475c3 --- /dev/null +++ b/external_references/ServiceNow/request_parameters.py @@ -0,0 +1,4 @@ +since = "" +until = "" +limit = None +offset = None \ No newline at end of file diff --git a/external_references/ServiceNow/requirements.txt b/external_references/ServiceNow/requirements.txt new file mode 100644 index 0000000..323ef8c --- /dev/null +++ b/external_references/ServiceNow/requirements.txt @@ -0,0 +1,2 @@ +python-dotenv +requests \ No newline at end of file diff --git a/external_references/Slack/README.md b/external_references/Slack/README.md new file mode 100644 index 0000000..36e4bce --- /dev/null +++ b/external_references/Slack/README.md @@ -0,0 +1,68 @@ +# PagerDuty to Slack Incident Mapping +The `pagerduty_slack_incidents.py` script sends a GET request to the log_entries endpoint to retrieve log entries data, filters for log entries containing a Slack incident post, and then generates a CSV file mapping PagerDuty incidents to their corresponding Slack incident posts: + +- PagerDuty Incident Title +- PagerDuty Incident URL +- Slack Channel Name +- Slack Incident Post URL + +## Requirements + +- Python 3.6+ +- PagerDuty API Access Token + +## Installation + +1. Clone this repository + ```bash + git clone https://github.com/PagerDuty/public-support-scripts.git + ``` + +2. `cd` to the Slack directory within `external_references`. + +3. Install required dependencies: + ```bash + pip install -r requirements.txt + ``` + +## Usage + +1. Create a `.env` file in the Slack directory. + +2. Update the `.env` file with your PagerDuty API key: + ``` + PAGERDUTY_REST_API_KEY=your_api_key_here + ``` + +3. Define your request parameters in the `request_parameters.py` file. + +4. Run the `pagerduty_slack_incidents.py` script in the Slack directory: + ```bash + pagerduty_slack_incidents.py + ``` + +## How the Script Works + +The `pagerduty_slack_incidents.py` script will: + +1. Send a GET request to the log_entries endpoint to retrieve log entry data +2. Filter log entries to include only those with a Slack incident post +3. Generate a CSV file named `pagerduty_incidents_mapped_to_slack.csv` in the Slack directory. + +## Error Handling + +The `pagerduty_slack_incidents.py` script will exit with an error message in the following cases: + +- If the PagerDuty API request fails +- If no log entries containing a Slack incident incident post + +## Security Notes + +1. Never commit your `.env` file +2. User Token Rest API Keys are restricted in accordance with the user's access and permissions, so the GET request to the incidents endpoint will only retrieve incidents that the user has access to. General Access Rest API Keys do not have such restrictions. + +## Resources + +1. [List log entries documentation](https://developer.pagerduty.com/api-reference/c661e065403b5-list-log-entries) +2. [Pagination documentation](https://developer.pagerduty.com/docs/pagination) +3. [API Access Keys documentation](https://support.pagerduty.com/main/docs/api-access-keys) \ No newline at end of file diff --git a/external_references/Slack/pagerduty_slack_incidents.py b/external_references/Slack/pagerduty_slack_incidents.py new file mode 100644 index 0000000..301cf63 --- /dev/null +++ b/external_references/Slack/pagerduty_slack_incidents.py @@ -0,0 +1,117 @@ +import requests +import json +import csv +import os + +from dotenv import load_dotenv + +import request_parameters as parameters + +load_dotenv() +PAGERDUTY_REST_API_KEY = os.getenv("PAGERDUTY_REST_API_KEY") + + +def get_log_entries(): + """ + Makes a GET request to the log_entries endpoint to retrieve incident log entries. + + Raises: + SystemExit: If the API request fails or returns an error. + + Returns: + dict: JSON response containing incident log entries if successful. + """ + url = "https://api.pagerduty.com/log_entries" + + + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": f"Token token={PAGERDUTY_REST_API_KEY}" + } + + querystring = {} + + if parameters.since: + querystring["since"] = parameters.since + if parameters.until: + querystring["until"] = parameters.until + if parameters.limit: + querystring["limit"] = parameters.limit + if parameters.offset: + querystring["offset"] = parameters.offset + + try: + response = requests.get(url, headers=headers, params=querystring) + response.raise_for_status() + if response.text: + return response.json() + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + +def filter_log_entries(log_entries): + """ + Filter log entries to only include those with Slack channel type. + + Iterates through the log entries and keeps only those containing + with Slack channel type. + + Args: + log_entries(dict): JSON dict containing log entries returned from the GET request + to the log_entries endpoint. + + Raises: + SystemExit: If no log entries with Slack channel type are found. + + Returns: + list: log entries containing Slack channel type. + """ + entries_with_slack_channel = [] + for entry in log_entries["log_entries"]: + if entry["channel"].get("type"): + channel_type = entry["channel"].get("type") + if channel_type == "slack": + entries_with_slack_channel.append(entry) + + if not entries_with_slack_channel: + raise SystemExit("No Slack incident posts found in any incidents") + + return entries_with_slack_channel + +def generate_csv(filtered_entries): + """ + Generate a CSV file mapping PagerDuty incidents to Slack incident posts. + + Creates a CSV file with relevant PagerDuty incident information (PagerDuty Incident Number", "Title", "Description", + "Created At", "Updated At", "Status", "PagerDuty Incident URL") and + the corresponding Slack channel names and Slack incident post URLs. + + Args: + filtered_entries(list): List of log entries containing Slack channel type. + """ + csv_filename = "pagerduty_incidents_mapped_to_slack.csv" + # Define the header for the CSV + headers = ["Title", "PagerDuty Incident URL", "Slack Channel Name", "Slack Incident Post URL"] + # Write to CSV file + with open(csv_filename, mode="w", newline="", encoding="utf-8") as file: + writer = csv.DictWriter(file, fieldnames=headers) + writer.writeheader() + + for entry in filtered_entries: + writer.writerow({ + "Title": entry["incident"]["summary"], + "PagerDuty Incident URL": entry["incident"]["html_url"], + "Slack Channel Name": entry["chat_channel_name"], + "Slack Incident Post URL": entry["chat_channel_web_link"], + }) + + +def main(): + log_entries = get_log_entries() + filtered_entries = filter_log_entries(log_entries) + csv = generate_csv(filtered_entries) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/external_references/Slack/request_parameters.py b/external_references/Slack/request_parameters.py new file mode 100644 index 0000000..36475c3 --- /dev/null +++ b/external_references/Slack/request_parameters.py @@ -0,0 +1,4 @@ +since = "" +until = "" +limit = None +offset = None \ No newline at end of file diff --git a/external_references/Slack/requirements.txt b/external_references/Slack/requirements.txt new file mode 100644 index 0000000..323ef8c --- /dev/null +++ b/external_references/Slack/requirements.txt @@ -0,0 +1,2 @@ +python-dotenv +requests \ No newline at end of file diff --git a/external_references/ZenDesk/README.md b/external_references/ZenDesk/README.md new file mode 100644 index 0000000..0df26d4 --- /dev/null +++ b/external_references/ZenDesk/README.md @@ -0,0 +1,72 @@ +# PagerDuty to ZenDesk Incident Mapping +The `pagerduty_zendesk_incidents.py` script sends a GET request to the incidents endpoint to retrieve incident data, filters for incidents containing ZenDesk metadata, and then generates a CSV file mapping the PagerDuty incidents to their corresponding ZenDesk tickets: + +- PagerDuty Incident Number +- Title +- Description +- Created At +- Updated At +- Status +- PagerDuty Incident URL +- ZenDesk Ticket ID +- ZenDesk Ticket URL + +## Requirements + +- Python 3.6+ +- PagerDuty API Access Token + +## Installation + +1. Clone this repository + ```bash + git clone https://github.com/shirleyilenechan/incident_mappings.git + ``` +2. `cd` to the ZenDesk directory within `external_references`. + +3. Install required dependencies: + ```bash + pip install -r requirements.txt + ``` + +## Usage + +1. Create a `.env` file in the ZenDesk directory. + +2. Update the `.env` file with your PagerDuty API key: + ``` + PAGERDUTY_REST_API_KEY=your_api_key_here + ``` + +3. Define your request parameters in the `request_parameters.py` file. + +4. Run the `pagerduty_zendesk_incidents.py` script in the ZenDesk directory: + ```bash + python3 pagerduty_zendesk_incidents.py + ``` + +## How the Script Works + +The `pagerduty_zendesk_incidents.py` script will: + +1. Send a GET request to the incidents endpoint to retrieve incident data +2. Filter incidents to include only those with ZenDesk references +3. Generate a CSV file named `pagerduty_incidents_mapped_to_zendesk.csv` in the ZenDesk directory. + +## Error Handling + +The `pagerduty_zendesk_incidents.py` script will exit with an error message in the following cases: + +- If the PagerDuty API request fails +- If no incidents with ZenDesk metadata are found + +## Security Notes + +1. Never commit your `.env` file +2. User Token Rest API Keys are restricted in accordance with the user's access and permissions, so the GET request to the incidents endpoint will only retrieve incidents that the user has access to. General Access Rest API Keys do not have such restrictions. + +## Resources + +1. [List incidents endpoint documentation](https://developer.pagerduty.com/api-reference/9d0b4b12e36f9-list-incidents) +2. [Pagination documentation](https://developer.pagerduty.com/docs/pagination) +3. [API Access Keys documentation](https://support.pagerduty.com/main/docs/api-access-keys) \ No newline at end of file diff --git a/external_references/ZenDesk/pagerduty_zendesk_incidents.py b/external_references/ZenDesk/pagerduty_zendesk_incidents.py new file mode 100644 index 0000000..f1a5964 --- /dev/null +++ b/external_references/ZenDesk/pagerduty_zendesk_incidents.py @@ -0,0 +1,127 @@ +import requests +import json +import csv +import os + +from dotenv import load_dotenv + +import request_parameters as parameters + +load_dotenv() +PAGERDUTY_REST_API_KEY = os.getenv("PAGERDUTY_REST_API_KEY") + + +def get_incidents(): + """ + Makes a GET request to the incidents endpoint to retrieve incident data. + + Raises: + SystemExit: If the API request fails or returns an error. + + Returns: + dict: JSON response containing incident data if successful. + """ + url = "https://api.pagerduty.com/incidents" + + querystring = {"include[]":"metadata"} + + headers = { + "Accept": "application/json", + "Content-Type": "application/json", + "Authorization": f"Token token={PAGERDUTY_REST_API_KEY}" + } + + if parameters.since: + querystring["since"] = parameters.since + if parameters.until: + querystring["until"] = parameters.until + if parameters.limit: + querystring["limit"] = parameters.limit + if parameters.offset: + querystring["offset"] = parameters.offset + + try: + response = requests.get(url, headers=headers, params=querystring) + response.raise_for_status() + if response.text: + return response.json() + except requests.exceptions.RequestException as e: + raise SystemExit(e) + + +def filter_incidents(incidents): + """ + Filter incidents to only include those with ZenDesk metadata. + + Iterates through the incidents and keeps only those containing + ZenDesk metadata. + + Args: + incidents (dict): JSON dict containing incident data returned from the GET request + to the incidents endpoint. + + Raises: + SystemExit: If no incidents with ZenDesk metadata are found. + + Returns: + list: incidents containing ZenDesk metadata. + """ + incidents_with_metadata = [] + for incident in incidents["incidents"]: + if incident["metadata"] != []: + #only include incidents with servicenow metadata + if any("zendesk" in key.lower() for key in incident["metadata"]): + incidents_with_metadata.append(incident) + + if not incidents_with_metadata: + raise SystemExit("No ZenDesk metadata found in any incidents") + + return incidents_with_metadata + + +def generate_csv(filtered_incidents): + """ + Generate a CSV file mapping PagerDuty incidents to ZenDesk data. + + Creates a CSV file with relevant PagerDuty incident information (PagerDuty Incident Number", "Title", "Description", + "Created At", "Updated At", "Status", "PagerDuty Incident URL") and + the corresponding ZenDesk ticket IDs and ticket URLs. + + Args: + filtered_incidents (list): List of incidents containing ZenDesk metadata. + """ + csv_filename = "pagerduty_incidents_mapped_to_zendesk.csv" + # Define the header for the CSV + headers = ["PagerDuty Incident Number", "Title", "Description", "Created At", "Updated At", "Status", "PagerDuty Incident URL", "ZenDesk Ticket ID", "ZenDesk Ticket URL"] + # Write to CSV file + with open(csv_filename, mode="w", newline="", encoding="utf-8") as file: + writer = csv.DictWriter(file, fieldnames=headers) + writer.writeheader() + + for incident in filtered_incidents: + for key, value in incident["metadata"].items(): + if "zendesk" in key: + zendesk_key = key + zendesk_value = json.loads(value) + external_name = zendesk_value.get("external_name", "") + external_url = zendesk_value.get("external_url", "") + writer.writerow({ + "PagerDuty Incident Number": incident.get("incident_number", ""), + "Title": incident.get("title", ""), + "Description": incident.get("description", ""), + "Created At": incident.get("created_at", ""), + "Updated At": incident.get("updated_at", ""), + "Status": incident.get("status", ""), + "PagerDuty Incident URL": incident.get("html_url", ""), + "ZenDesk Ticket ID": external_name, + "ZenDesk Ticket URL": external_url + }) + +def main(): + incidents = get_incidents() + filtered_incidents = filter_incidents(incidents) + csv = generate_csv(filtered_incidents) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/external_references/ZenDesk/request_parameters.py b/external_references/ZenDesk/request_parameters.py new file mode 100644 index 0000000..36475c3 --- /dev/null +++ b/external_references/ZenDesk/request_parameters.py @@ -0,0 +1,4 @@ +since = "" +until = "" +limit = None +offset = None \ No newline at end of file diff --git a/external_references/ZenDesk/requirements.txt b/external_references/ZenDesk/requirements.txt new file mode 100644 index 0000000..323ef8c --- /dev/null +++ b/external_references/ZenDesk/requirements.txt @@ -0,0 +1,2 @@ +python-dotenv +requests \ No newline at end of file