diff --git a/main.py b/main.py index e8031af4..28a10883 100644 --- a/main.py +++ b/main.py @@ -12,6 +12,8 @@ # Local imports from config import ( ENV, + GITHUB_TEST_REPO_NAME, + GITHUB_TEST_REPO_OWNER, GITHUB_WEBHOOK_SECRET, JIRA_WEBHOOK_SECRET, PRODUCT_NAME, @@ -85,11 +87,10 @@ async def handle_webhook(request: Request) -> dict[str, str]: content_type: str = request.headers.get( "Content-Type", "Content-Type not specified" ) - agent: str = "JIRA" - event_name: str = (await request.json()).get("issue_event_type_name", "Event not specified") + event_name: str = (await request.json()).get("webhookEvent", "Event not specified") print("\n" * 3 + "-" * 70) - print(f"Received event: {event_name} from Agent: {agent} with content type: {content_type}") + print(f"Received event: {event_name} from Agent: JIRA with content type: {content_type}") await verify_jira_webhook_signature(request=request, secret=JIRA_WEBHOOK_SECRET) try: @@ -103,16 +104,13 @@ async def handle_webhook(request: Request) -> dict[str, str]: # First try to parse the body as JSON payload = json.loads(s=request_body.decode(encoding=UTF8)) - with open("payload.json", "w") as f: - json.dump(payload, f, indent=4) - - username: str = "BigOleHealz" + username: str = GITHUB_TEST_REPO_NAME payload["action"] = event_name payload.setdefault("installation", {})["id"] = 56165848 + payload["issue"]["fields"]["reporter"]["accountId"] = 17244643 payload["issue"]["fields"]["creator"]["displayName"] = username payload["user"]["displayName"] = username payload["issue"]["fields"]["reporter"]["displayName"] = username - payload["issue"]["fields"]["reporter"]["accountId"] = 17244643 except json.JSONDecodeError: # If JSON parsing fails, treat the body as URL-encoded diff --git a/services/jira/jira_manager.py b/services/jira/jira_manager.py index 36841a9e..db94bbdf 100644 --- a/services/jira/jira_manager.py +++ b/services/jira/jira_manager.py @@ -85,8 +85,13 @@ def map_jira_to_github_event_payload(jira_payload: Dict[str, Any]) -> GitHubEven issue_fields = jira_payload["issue"]["fields"] reporter = issue_fields["reporter"] + issue_id: int = int(jira_payload["issue"]["id"]) + issue_key: str = jira_payload["issue"]["key"] - repo_url: str = f"git@github.com:{jira_payload['user']['displayName']}/{jira_payload['issue']['fields']['project']['name']}.git" + + # repo_url: str = f"git@github.com:{jira_payload['user']['displayName']}/{jira_payload['issue']['fields']['project']['name']}.git" + + repo_url: str = next((label for label in issue_fields["labels"] if label.startswith("https://github.com/")), None) # Build the GitHubLabeledPayload type github_payload: GitHubLabeledPayload = GitHubLabeledPayload( @@ -99,7 +104,7 @@ def map_jira_to_github_event_payload(jira_payload: Dict[str, Any]) -> GitHubEven events_url="", # Optional, map if needed html_url=f"https://bigolehealz.atlassian.net/browse/{jira_payload['issue']['key']}", id=int(jira_payload["issue"]["id"]), - node_id=jira_payload["issue"]["key"], + node_id=issue_key, number=int(jira_payload["issue"]["id"]), title=issue_fields["summary"], user=map_user_info(reporter), @@ -123,7 +128,7 @@ def map_jira_to_github_event_payload(jira_payload: Dict[str, Any]) -> GitHubEven ), label=LabelInfo( id=0, # Placeholder, JIRA doesn't provide label IDs - node_id="", # Placeholder + node_id=issue_key, # Placeholder url="", # Placeholder name=PRODUCT_ID, color="", # No color in JIRA @@ -184,8 +189,10 @@ def map_jira_to_github_event_payload(jira_payload: Dict[str, Any]) -> GitHubEven description=issue_fields["project"].get("description", ""), ), sender=map_user_info(reporter), - installation=InstallationMiniInfo(id=56165848, node_id=issue_fields["project"]["key"]), # Not applicable for JIRA - ) + installation=InstallationMiniInfo( + id=jira_payload['installation']['id'], + node_id=issue_fields["project"]["key"]) + ) # Return as GitHubEventPayload type return cast(GitHubEventPayload, github_payload) diff --git a/services/webhook_handler.py b/services/webhook_handler.py index 70a7de57..d5390fc2 100644 --- a/services/webhook_handler.py +++ b/services/webhook_handler.py @@ -143,7 +143,7 @@ async def handle_webhook_event(event_name: str, payload: GitHubEventPayload) -> print("Edit is not an activated GitAtuo trigger.") return - if event_name in ("issue_created", "issue_updated"): + if event_name.startswith("jira:") and payload["issue"]["fields"]["assignee"]: github_payload: GitHubEventPayload = cast(GitHubEventPayload, map_jira_to_github_event_payload(jira_payload=payload))