Spaces:
Sleeping
Sleeping
Commit ·
099ebb2
1
Parent(s): 9206cfd
added changes
Browse files- api/webhook_routes.py +42 -8
- integrations/jira_service.py +16 -0
api/webhook_routes.py
CHANGED
|
@@ -3,6 +3,7 @@ import logging
|
|
| 3 |
from services.supabase_service import supabase_service
|
| 4 |
from integrations.jira_service import JiraIntegrationService
|
| 5 |
from typing import Dict, Any
|
|
|
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
|
@@ -41,16 +42,18 @@ async def receive_jira_webhook(
|
|
| 41 |
elif "user" in payload:
|
| 42 |
user_email = payload["user"].get("emailAddress")
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if not user_email:
|
| 45 |
logger.warning("Email not found in payload. Attempting to fetch user details from Jira API.")
|
| 46 |
|
| 47 |
-
# Try to get accountId from payload
|
| 48 |
-
account_id = None
|
| 49 |
-
if "user" in payload and "accountId" in payload["user"]:
|
| 50 |
-
account_id = payload["user"]["accountId"]
|
| 51 |
-
elif "issue" in payload and payload["issue"].get("fields", {}).get("assignee"):
|
| 52 |
-
account_id = payload["issue"]["fields"]["assignee"].get("accountId")
|
| 53 |
-
|
| 54 |
if account_id:
|
| 55 |
try:
|
| 56 |
jira_service = JiraIntegrationService()
|
|
@@ -74,8 +77,39 @@ async def receive_jira_webhook(
|
|
| 74 |
logger.warning(f"No Supabase user found for Jira email: {user_email}")
|
| 75 |
return {"status": "ignored", "reason": "user_not_found"}
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Store data
|
| 78 |
-
success = supabase_service.upsert_jira_data(firebase_id,
|
| 79 |
|
| 80 |
if success:
|
| 81 |
return {"status": "processed", "user": firebase_id}
|
|
|
|
| 3 |
from services.supabase_service import supabase_service
|
| 4 |
from integrations.jira_service import JiraIntegrationService
|
| 5 |
from typing import Dict, Any
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
|
|
|
| 42 |
elif "user" in payload:
|
| 43 |
user_email = payload["user"].get("emailAddress")
|
| 44 |
|
| 45 |
+
# Try to get accountId from payload to fetch full data later
|
| 46 |
+
account_id = None
|
| 47 |
+
if "user" in payload and "accountId" in payload["user"]:
|
| 48 |
+
account_id = payload["user"]["accountId"]
|
| 49 |
+
elif "issue" in payload and payload["issue"].get("fields", {}).get("assignee"):
|
| 50 |
+
account_id = payload["issue"]["fields"]["assignee"].get("accountId")
|
| 51 |
+
elif "issue" in payload and payload["issue"].get("fields", {}).get("reporter"):
|
| 52 |
+
account_id = payload["issue"]["fields"]["reporter"].get("accountId")
|
| 53 |
+
|
| 54 |
if not user_email:
|
| 55 |
logger.warning("Email not found in payload. Attempting to fetch user details from Jira API.")
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if account_id:
|
| 58 |
try:
|
| 59 |
jira_service = JiraIntegrationService()
|
|
|
|
| 77 |
logger.warning(f"No Supabase user found for Jira email: {user_email}")
|
| 78 |
return {"status": "ignored", "reason": "user_not_found"}
|
| 79 |
|
| 80 |
+
# Fetch all data for this user
|
| 81 |
+
full_data = {
|
| 82 |
+
"webhook_event": payload,
|
| 83 |
+
"synced_at": datetime.utcnow().isoformat(),
|
| 84 |
+
"assigned_issues": [],
|
| 85 |
+
"projects": [],
|
| 86 |
+
"boards": []
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
if account_id:
|
| 90 |
+
try:
|
| 91 |
+
# Initialize service (might already be initialized if we fetched email)
|
| 92 |
+
jira_service = JiraIntegrationService()
|
| 93 |
+
|
| 94 |
+
# 1. Fetch Assigned Issues
|
| 95 |
+
issues = jira_service.get_issues_assigned_to_user(account_id)
|
| 96 |
+
full_data["assigned_issues"] = [issue.model_dump(mode='json') for issue in issues]
|
| 97 |
+
|
| 98 |
+
# 2. Fetch All Projects
|
| 99 |
+
projects = jira_service.get_projects()
|
| 100 |
+
full_data["projects"] = [project.model_dump(mode='json') for project in projects]
|
| 101 |
+
|
| 102 |
+
# 3. Fetch All Boards
|
| 103 |
+
boards = jira_service.get_boards()
|
| 104 |
+
full_data["boards"] = boards # get_boards returns list of dicts already
|
| 105 |
+
|
| 106 |
+
logger.info(f"Synced for user {account_id}: {len(issues)} issues, {len(projects)} projects, {len(boards)} boards")
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
logger.error(f"Failed to fetch complete data: {str(e)}")
|
| 110 |
+
|
| 111 |
# Store data
|
| 112 |
+
success = supabase_service.upsert_jira_data(firebase_id, full_data)
|
| 113 |
|
| 114 |
if success:
|
| 115 |
return {"status": "processed", "user": firebase_id}
|
integrations/jira_service.py
CHANGED
|
@@ -110,6 +110,22 @@ class JiraIntegrationService:
|
|
| 110 |
logger.error(f"Error fetching issues for project {project_key}: {str(e)}")
|
| 111 |
raise
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def get_sprint_issues(self, sprint_id: int) -> List[JiraIssue]:
|
| 114 |
"""Fetch issues for a specific sprint"""
|
| 115 |
try:
|
|
|
|
| 110 |
logger.error(f"Error fetching issues for project {project_key}: {str(e)}")
|
| 111 |
raise
|
| 112 |
|
| 113 |
+
def get_issues_assigned_to_user(self, account_id: str, max_results: int = 100) -> List[JiraIssue]:
|
| 114 |
+
"""Fetch all issues assigned to a specific user"""
|
| 115 |
+
try:
|
| 116 |
+
jql = f"assignee = '{account_id}' ORDER BY updated DESC"
|
| 117 |
+
issues = self._search_jql(
|
| 118 |
+
jql,
|
| 119 |
+
max_results=max_results,
|
| 120 |
+
fields=['summary', 'status', 'priority', 'assignee', 'reporter', 'created',
|
| 121 |
+
'updated', 'resolutiondate', 'customfield_10016', 'description',
|
| 122 |
+
'labels', 'components', 'issuetype', 'timespent', 'timeestimate']
|
| 123 |
+
)
|
| 124 |
+
return [self._parse_issue_dict(issue) for issue in issues]
|
| 125 |
+
except Exception as e:
|
| 126 |
+
logger.error(f"Error fetching issues for user {account_id}: {str(e)}")
|
| 127 |
+
return []
|
| 128 |
+
|
| 129 |
def get_sprint_issues(self, sprint_id: int) -> List[JiraIssue]:
|
| 130 |
"""Fetch issues for a specific sprint"""
|
| 131 |
try:
|