Merge pull request #1359 from Codium-ai/tr/is_bot_user

Refactor `is_bot_user` function to improve actor type handling
This commit is contained in:
Tal
2024-11-14 08:29:05 +02:00
committed by GitHub

View File

@ -98,11 +98,14 @@ async def _perform_commands_bitbucket(commands_conf: str, agent: PRAgent, api_ur
def is_bot_user(data) -> bool: def is_bot_user(data) -> bool:
try: try:
if data["data"]["actor"]["type"] != "user": actor = data.get("data", {}).get("actor", {})
get_logger().info(f"BitBucket actor type is not 'user': {data['data']['actor']['type']}") # 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 return True
except Exception as e: 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 return False
@ -161,16 +164,18 @@ async def handle_github_webhooks(background_tasks: BackgroundTasks, request: Req
return "OK" return "OK"
# Get the username of the sender # Get the username of the sender
try: actor = data.get("data", {}).get("actor", {})
username = data["data"]["actor"]["username"] if actor:
except KeyError:
try: try:
username = data["data"]["actor"]["display_name"] username = actor["username"]
except KeyError: except KeyError:
username = data["data"]["actor"]["nickname"] try:
log_context["sender"] = username username = actor["display_name"]
except KeyError:
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 log_context["sender_id"] = sender_id
jwt_parts = input_jwt.split(".") jwt_parts = input_jwt.split(".")
claim_part = jwt_parts[1] claim_part = jwt_parts[1]