-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
874 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,11 @@ | ||
from db.types import Integration | ||
import httpx | ||
from llama_index.core import Document | ||
|
||
INCIDENT_TEXT_TEMPLATE = """ | ||
Incident title: {title} | ||
Incident description: {description} | ||
Incident summary: {summary} | ||
Incident status: {status} | ||
Service name: {service_name} | ||
Created at: {created_at} | ||
""" | ||
|
||
|
||
async def get_incidents(integration: Integration): | ||
access_token = integration.credentials["access_token"] | ||
integration_type = integration.type | ||
headers = {} | ||
if integration_type == "basic": | ||
headers["Authorization"] = f"Token token={access_token}" | ||
elif integration_type == "oauth": | ||
headers["Authorization"] = f"Bearer {access_token}" | ||
else: | ||
raise ValueError(f"Invalid integration type: {integration_type}") | ||
|
||
limit = 100 | ||
offset = 0 | ||
resolved_incidents = [] | ||
while True: | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get( | ||
"https://api.pagerduty.com/incidents", | ||
headers=headers, | ||
params={ | ||
"date_range": "all", | ||
"statuses[]": "resolved", | ||
"limit": limit, | ||
"offset": offset, | ||
}, | ||
) | ||
data = response.json() | ||
incidents = data["incidents"] | ||
resolved_incidents.extend(incidents) | ||
if not data["more"]: | ||
break | ||
offset += limit | ||
return resolved_incidents | ||
from loaders.readers.pagerduty import PagerDutyReader | ||
|
||
|
||
async def fetch_pagerduty_documents(integration: Integration): | ||
incidents = await get_incidents(integration) | ||
|
||
documents = [] | ||
for incident in incidents: | ||
service = incident.get("service", {}) | ||
service_name = service.get("summary", "Unknown") | ||
|
||
text = INCIDENT_TEXT_TEMPLATE.format( | ||
title=incident["title"], | ||
description=incident["description"], | ||
summary=incident["summary"], | ||
status=incident["status"], | ||
service_name=service_name, | ||
created_at=incident["created_at"], | ||
) | ||
metadata = { | ||
"source": "PagerDuty", | ||
"id": incident["id"], | ||
"link": incident["html_url"], | ||
"status": incident["status"], | ||
"urgency": incident["urgency"], | ||
"service_id": service.get("id", "Unknown"), | ||
"first_trigger_log_entry_id": incident.get( | ||
"first_trigger_log_entry", {} | ||
).get("id", "Unknown"), | ||
"created_at": incident["created_at"], | ||
"updated_at": incident["updated_at"], | ||
} | ||
|
||
document = Document(doc_id=incident["id"], text=text, metadata=metadata) | ||
documents.append(document) | ||
access_token = integration.credentials["access_token"] | ||
token_type = integration.type | ||
loader = PagerDutyReader(access_token, token_type) | ||
documents = await loader.load_data() | ||
|
||
return documents |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from typing import List, Optional, TypedDict | ||
|
||
from llama_index.core.readers.base import BaseReader | ||
from llama_index.core.schema import Document | ||
|
||
|
||
class BasicAuth(TypedDict): | ||
email: str | ||
api_token: str | ||
server_url: str | ||
|
||
|
||
class Oauth2(TypedDict): | ||
cloud_id: str | ||
api_token: str | ||
|
||
|
||
class JiraReader(BaseReader): | ||
"""Jira reader. Reads data from Jira issues from passed query. | ||
Args: | ||
Optional basic_auth:{ | ||
"email": "email", | ||
"api_token": "token", | ||
"server_url": "server_url" | ||
} | ||
Optional oauth:{ | ||
"cloud_id": "cloud_id", | ||
"api_token": "token" | ||
} | ||
""" | ||
|
||
def __init__( | ||
self, | ||
email: Optional[str] = None, | ||
api_token: Optional[str] = None, | ||
server_url: Optional[str] = None, | ||
BasicAuth: Optional[BasicAuth] = None, | ||
Oauth2: Optional[Oauth2] = None, | ||
) -> None: | ||
from jira import JIRA | ||
|
||
if email and api_token and server_url: | ||
if BasicAuth is None: | ||
BasicAuth = {} | ||
BasicAuth["email"] = email | ||
BasicAuth["api_token"] = api_token | ||
BasicAuth["server_url"] = server_url | ||
|
||
if Oauth2: | ||
options = { | ||
"server": f"https://api.atlassian.com/ex/jira/{Oauth2['cloud_id']}", | ||
"headers": {"Authorization": f"Bearer {Oauth2['api_token']}"}, | ||
} | ||
self.jira = JIRA(options=options) | ||
else: | ||
self.jira = JIRA( | ||
basic_auth=(BasicAuth["email"], BasicAuth["api_token"]), | ||
server=f"https://{BasicAuth['server_url']}", | ||
) | ||
|
||
def load_data(self, query: str) -> List[Document]: | ||
relevant_issues = self.jira.search_issues(query) | ||
|
||
issues = [] | ||
|
||
assignee = "" | ||
reporter = "" | ||
epic_key = "" | ||
epic_summary = "" | ||
epic_descripton = "" | ||
|
||
for issue in relevant_issues: | ||
# Iterates through only issues and not epics | ||
if "parent" in (issue.raw["fields"]): | ||
if issue.fields.assignee: | ||
assignee = issue.fields.assignee.displayName | ||
|
||
if issue.fields.reporter: | ||
reporter = issue.fields.reporter.displayName | ||
|
||
if issue.raw["fields"]["parent"]["key"]: | ||
epic_key = issue.raw["fields"]["parent"]["key"] | ||
|
||
if issue.raw["fields"]["parent"]["fields"]["summary"]: | ||
epic_summary = issue.raw["fields"]["parent"]["fields"]["summary"] | ||
|
||
if issue.raw["fields"]["parent"]["fields"]["status"]["description"]: | ||
epic_descripton = issue.raw["fields"]["parent"]["fields"]["status"][ | ||
"description" | ||
] | ||
|
||
issues.append( | ||
Document( | ||
text=f"{issue.fields.summary} \n {issue.fields.description}", | ||
doc_id=issue.id, | ||
extra_info={ | ||
"id": issue.id, | ||
"title": issue.fields.summary, | ||
"url": issue.permalink(), | ||
"created_at": issue.fields.created, | ||
"updated_at": issue.fields.updated, | ||
"labels": issue.fields.labels, | ||
"status": issue.fields.status.name, | ||
"assignee": assignee, | ||
"reporter": reporter, | ||
"project": issue.fields.project.name, | ||
"issue_type": issue.fields.issuetype.name, | ||
"priority": issue.fields.priority.name, | ||
"epic_key": epic_key, | ||
"epic_summary": epic_summary, | ||
"epic_description": epic_descripton, | ||
}, | ||
) | ||
) | ||
|
||
return issues |
Oops, something went wrong.