Skip to content

Commit cd8ba4f

Browse files
authored
Merge pull request #1359 from Codium-ai/tr/is_bot_user
Refactor `is_bot_user` function to improve actor type handling
2 parents b07f96d + fe27f96 commit cd8ba4f

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

pr_agent/servers/bitbucket_app.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,14 @@ async def _perform_commands_bitbucket(commands_conf: str, agent: PRAgent, api_ur
9898

9999
def is_bot_user(data) -> bool:
100100
try:
101-
if data["data"]["actor"]["type"] != "user":
102-
get_logger().info(f"BitBucket actor type is not 'user': {data['data']['actor']['type']}")
101+
actor = data.get("data", {}).get("actor", {})
102+
# allow actor type: user . if it's "AppUser" or "team" then it is a bot user
103+
allowed_actor_types = {"user"}
104+
if actor and actor["type"].lower() not in allowed_actor_types:
105+
get_logger().info(f"BitBucket actor type is not 'user', skipping: {actor}")
103106
return True
104107
except Exception as e:
105-
get_logger().error("Failed 'is_bot_user' logic: {e}")
108+
get_logger().error(f"Failed 'is_bot_user' logic: {e}")
106109
return False
107110

108111

@@ -161,16 +164,18 @@ async def inner():
161164
return "OK"
162165

163166
# Get the username of the sender
164-
try:
165-
username = data["data"]["actor"]["username"]
166-
except KeyError:
167+
actor = data.get("data", {}).get("actor", {})
168+
if actor:
167169
try:
168-
username = data["data"]["actor"]["display_name"]
170+
username = actor["username"]
169171
except KeyError:
170-
username = data["data"]["actor"]["nickname"]
171-
log_context["sender"] = username
172+
try:
173+
username = actor["display_name"]
174+
except KeyError:
175+
username = actor["nickname"]
176+
log_context["sender"] = username
172177

173-
sender_id = data["data"]["actor"]["account_id"]
178+
sender_id = data.get("data", {}).get("actor", {}).get("account_id", "")
174179
log_context["sender_id"] = sender_id
175180
jwt_parts = input_jwt.split(".")
176181
claim_part = jwt_parts[1]

0 commit comments

Comments
 (0)