Skip to content

Commit

Permalink
optimizing code
Browse files Browse the repository at this point in the history
  • Loading branch information
BigOleHealz committed Oct 31, 2024
1 parent ea8fb88 commit 1b7ab96
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
14 changes: 6 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
17 changes: 12 additions & 5 deletions services/jira/jira_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion services/webhook_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 1b7ab96

Please sign in to comment.