From c934523f2d8d36ad66cc2fb22a5f926fc94400d2 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Thu, 14 Nov 2024 08:19:49 +0200 Subject: [PATCH 1/3] Refactor `is_bot_user` function to improve actor type handling and logging --- pr_agent/servers/bitbucket_app.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pr_agent/servers/bitbucket_app.py b/pr_agent/servers/bitbucket_app.py index 8701463df..ab810f5a8 100644 --- a/pr_agent/servers/bitbucket_app.py +++ b/pr_agent/servers/bitbucket_app.py @@ -98,11 +98,14 @@ async def _perform_commands_bitbucket(commands_conf: str, agent: PRAgent, api_ur def is_bot_user(data) -> bool: try: - if data["data"]["actor"]["type"] != "user": - get_logger().info(f"BitBucket actor type is not 'user': {data['data']['actor']['type']}") + actor = data.get("data", {}).get("actor", {}) + # allow actor type: user . if it's "AppUser" or "team" then it is a bot user + allowed_actor_types = {"user"} + if actor and actor["type"].lower() not in allowed_actor_types: + get_logger().info(f"BitBucket actor type is not 'user', skipping: {actor}") return True except Exception as e: - get_logger().error("Failed 'is_bot_user' logic: {e}") + get_logger().error(f"Failed 'is_bot_user' logic: {e}") return False From 2c3aa7b2dc2b9fb6e57ca8b064c0359162cd45b2 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Thu, 14 Nov 2024 08:23:38 +0200 Subject: [PATCH 2/3] Improve actor data extraction logic in Bitbucket app server --- pr_agent/servers/bitbucket_app.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pr_agent/servers/bitbucket_app.py b/pr_agent/servers/bitbucket_app.py index ab810f5a8..98df69a64 100644 --- a/pr_agent/servers/bitbucket_app.py +++ b/pr_agent/servers/bitbucket_app.py @@ -164,14 +164,16 @@ async def inner(): return "OK" # Get the username of the sender - try: - username = data["data"]["actor"]["username"] - except KeyError: + actor = data.get("data", {}).get("actor", {}) + if actor: try: - username = data["data"]["actor"]["display_name"] + username = actor["username"] except KeyError: - username = data["data"]["actor"]["nickname"] - log_context["sender"] = username + try: + username = actor["display_name"] + except KeyError: + username = actor["nickname"] + log_context["sender"] = username sender_id = data["data"]["actor"]["account_id"] log_context["sender_id"] = sender_id From fe27f96bf1472bcc2ebc95376eb9e9d12f19930b Mon Sep 17 00:00:00 2001 From: mrT23 Date: Thu, 14 Nov 2024 08:26:04 +0200 Subject: [PATCH 3/3] Improve robustness of sender_id extraction in Bitbucket app server by using safe dictionary access --- pr_agent/servers/bitbucket_app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pr_agent/servers/bitbucket_app.py b/pr_agent/servers/bitbucket_app.py index 98df69a64..a5f2fccd2 100644 --- a/pr_agent/servers/bitbucket_app.py +++ b/pr_agent/servers/bitbucket_app.py @@ -175,7 +175,7 @@ async def inner(): username = actor["nickname"] log_context["sender"] = username - sender_id = data["data"]["actor"]["account_id"] + sender_id = data.get("data", {}).get("actor", {}).get("account_id", "") log_context["sender_id"] = sender_id jwt_parts = input_jwt.split(".") claim_part = jwt_parts[1]