Skip to content

πŸ™Œ OpenHands: AI-Driven Development

Fix BBDC mentioner resolution to use numeric id, and add πŸ‘€ eye reaction (#14497)

Alona6e1e3d03d38a
diff --git a/enterprise/integrations/bitbucket_data_center/bitbucket_dc_manager.py b/enterprise/integrations/bitbucket_data_center/bitbucket_dc_manager.py
index aceaff8d2c..fc0905c7a4 100644
--- a/enterprise/integrations/bitbucket_data_center/bitbucket_dc_manager.py
+++ b/enterprise/integrations/bitbucket_data_center/bitbucket_dc_manager.py
@@ -92,6 +92,53 @@ class BitbucketDCManager(Manager[BitbucketDCViewType]):
)
return False
+ async def _add_eyes_reaction(
+ self,
+ message: Message,
+ reacting_user_id: str,
+ project_key: str,
+ repo_slug: str,
+ ) -> None:
+ """Best-effort πŸ‘€ acknowledgement on the triggering PR comment.
+
+ Mirrors ``GithubManager._add_reaction``: posted once we've decided
+ the request will be acted on (permission check passed, mentioner
+ resolved), before view construction or conversation creation. Posted
+ as the resolved invoking user (``reacting_user_id`` -- the mentioner's
+ keycloak id, or the installer when the mentioner has no OHE account)
+ so the reaction is attributed to whoever triggered the job.
+
+ Any failure here is logged at INFO and swallowed -- older BBDC
+ installs return 404/400 on the reactions endpoint, and a missing
+ acknowledgement must never block conversation creation.
+ """
+ payload = message.message.get('payload') or {}
+ comment = payload.get('comment') or {}
+ pull_request = payload.get('pullRequest') or {}
+ comment_id = comment.get('id')
+ pr_id = pull_request.get('id')
+ if comment_id is None or pr_id is None:
+ return
+
+ from integrations.bitbucket_data_center.bitbucket_dc_service import (
+ SaaSBitbucketDCService,
+ )
+
+ try:
+ service = SaaSBitbucketDCService(external_auth_id=reacting_user_id)
+ await service.add_comment_reaction(
+ owner=project_key,
+ repo_slug=repo_slug,
+ pr_id=int(pr_id),
+ comment_id=int(comment_id),
+ emoticon='eyes',
+ )
+ except Exception as e:
+ logger.info(
+ f'[Bitbucket DC] Could not add eyes reaction on '
+ f'{project_key}/{repo_slug} PR#{pr_id} comment#{comment_id}: {e}'
+ )
+
async def receive_message(self, message: Message) -> None:
self._confirm_incoming_source_type(message)
if not self.is_job_requested(message):
@@ -119,39 +166,57 @@ class BitbucketDCManager(Manager[BitbucketDCViewType]):
# Mirror the GitHub resolver pattern: the job runs as the user who
# @-mentioned us, not the webhook installer. Look up the mentioner
- # in Keycloak by their BBDC slug; the installer keycloak_user_id is
- # carried alongside so the permission check and webhook lifecycle
- # calls can keep using the installer's elevated token.
+ # in Keycloak by their numeric BBDC user id; the installer
+ # keycloak_user_id is carried alongside so the permission check and
+ # webhook lifecycle calls can keep using the installer's elevated
+ # token.
+ #
+ # NB: Keycloak's `bitbucket_data_center_id` attribute is populated by
+ # the SSO id-mapper from the BBDC OIDC `sub` claim, which is the
+ # numeric user id -- NOT the slug/username. So we must look up by
+ # `actor['id']` (numeric), not by the slug; looking up by slug never
+ # matches and silently falls back to the installer. The slug is kept
+ # only for human-readable logging.
payload = message.message.get('payload') or {}
actor = payload.get('actor') or {}
mentioner_slug = extract_actor_slug(actor)
+ mentioner_idp_id = str(actor.get('id') or '')
mentioner_keycloak_id: str | None = None
- if mentioner_slug:
+ if mentioner_idp_id:
try:
mentioner_keycloak_id = (
await self.token_manager.get_user_id_from_idp_user_id(
- mentioner_slug, ProviderType.BITBUCKET_DATA_CENTER
+ mentioner_idp_id, ProviderType.BITBUCKET_DATA_CENTER
)
)
except Exception as e:
logger.warning(
f'[Bitbucket DC] Keycloak lookup for mentioner '
- f'{mentioner_slug!r} failed: {e}'
+ f'{mentioner_slug!r} (id {mentioner_idp_id}) failed: {e}'
)
if not mentioner_keycloak_id:
logger.info(
- f'[Bitbucket DC] Mentioner {mentioner_slug!r} has no OHE '
- f'account; falling back to installer for this job'
+ f'[Bitbucket DC] Mentioner {mentioner_slug!r} (id '
+ f'{mentioner_idp_id}) has no OHE account; falling back to '
+ f'installer for this job'
)
mentioner_keycloak_id = installer_user_id
elif mentioner_keycloak_id != installer_user_id:
logger.info(
f'[Bitbucket DC] Running job as mentioner {mentioner_slug!r} '
- f'(keycloak {mentioner_keycloak_id}) instead of installer '
- f'({installer_user_id})'
+ f'(id {mentioner_idp_id}, keycloak {mentioner_keycloak_id}) '
+ f'instead of installer ({installer_user_id})'
)
+ # Acknowledge receipt with a πŸ‘€ reaction on the triggering comment,
+ # mirroring the GitHub manager. Posted as the resolved invoking user.
+ # Best-effort: failures (e.g. legacy BBDC without the reactions
+ # endpoint) must not block conversation creation.
+ await self._add_eyes_reaction(
+ message, mentioner_keycloak_id, project_key, repo_slug
+ )
+
bitbucket_view = await BitbucketDCFactory.create_bitbucket_dc_view_from_payload(
message,
keycloak_user_id=mentioner_keycloak_id,
diff --git a/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_manager.py b/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_manager.py
index ace45914bc..fa9fe207ae 100644
--- a/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_manager.py
+++ b/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_manager.py
@@ -127,6 +127,13 @@ async def test_receive_message_runs_job_as_mentioner_when_linked_in_keycloak():
view = captured['view']
assert view.user_info.keycloak_user_id == 'kc-alice'
assert view.installer_keycloak_user_id == 'kc-installer'
+ # Regression guard for the slug-vs-numeric-id bug: the mentioner must be
+ # resolved by their NUMERIC BBDC id (actor['id'] == 1001), which is what
+ # Keycloak's `bitbucket_data_center_id` attribute stores (the OIDC `sub`
+ # claim) -- NOT the slug 'alice'. Looking up by slug never matched and
+ # silently fell back to the webhook installer.
+ token_manager.get_user_id_from_idp_user_id.assert_awaited_once()
+ assert token_manager.get_user_id_from_idp_user_id.await_args.args[0] == '1001'
@pytest.mark.asyncio
diff --git a/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_service.py b/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_service.py
index 3402bcc80d..047b7f9918 100644
--- a/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_service.py
+++ b/enterprise/tests/unit/integrations/bitbucket_data_center/test_bitbucket_dc_service.py
@@ -9,6 +9,8 @@ from integrations.bitbucket_data_center.bitbucket_dc_service import (
from pydantic import SecretStr
from server.auth.token_manager import TokenManager
+from openhands.app_server.integrations.service_types import RequestMethod
+
@pytest.fixture
def service():
@@ -130,3 +132,33 @@ class TestGetLatestToken:
assert token.get_secret_value() == expected_token
mock_get_idp_token.assert_called_once()
mock_load_offline.assert_not_called()
+
+
+async def test_add_comment_reaction_uses_comment_likes_put():
+ """BBDC reactions live in the comment-likes plugin, not core /rest/api/1.0.
+
+ Regression guard: the call must be a PUT to
+ ``/rest/comment-likes/latest/.../comments/{id}/reactions/{emoticon}`` with
+ the bare emoticon name (``eyes``). The original endpoint
+ (POST /rest/api/1.0/.../reactions with ``:eyes:``) returns 404/400.
+ """
+ service = SaaSBitbucketDCService()
+ service.BASE_URL = 'https://bb.example.com/rest/api/1.0'
+
+ with patch.object(service, '_make_request', new_callable=AsyncMock) as mock_request:
+ await service.add_comment_reaction(
+ owner='PROJ',
+ repo_slug='myrepo',
+ pr_id=7,
+ comment_id=99,
+ emoticon='eyes',
+ )
+
+ mock_request.assert_awaited_once()
+ args, kwargs = mock_request.await_args
+ assert args[0] == (
+ 'https://bb.example.com/rest/comment-likes/latest/projects/PROJ'
+ '/repos/myrepo/pull-requests/7/comments/99/reactions/eyes'
+ )
+ assert kwargs['method'] == RequestMethod.PUT
diff --git a/openhands/app_server/integrations/bitbucket_data_center/service/resolver.py b/openhands/app_server/integrations/bitbucket_data_center/service/resolver.py
index dcb89500f3..4bfd6a29a8 100644
--- a/openhands/app_server/integrations/bitbucket_data_center/service/resolver.py
+++ b/openhands/app_server/integrations/bitbucket_data_center/service/resolver.py
@@ -141,6 +141,36 @@ class BitbucketDCResolverMixin(BitbucketDCMixinBase):
await self._make_request(url, params=payload, method=RequestMethod.POST)
+ async def add_comment_reaction(
+ self,
+ owner: str,
+ repo_slug: str,
+ pr_id: int,
+ comment_id: int,
+ emoticon: str,
+ ) -> None:
+ """Post a reaction emoticon on a Bitbucket Data Center PR comment.
+
+ BBDC comment reactions live in the *comment-likes* plugin, NOT the
+ core ``/rest/api/1.0`` API. The call is::
+
+ PUT /rest/comment-likes/latest/projects/{owner}/repos/{repo}
+ /pull-requests/{pr}/comments/{id}/reactions/{emoticon}
+
+ where ``emoticon`` is the bare shortcut name (e.g. ``eyes``, ``+1``,
+ ``heart``) -- no surrounding colons (``:eyes:`` returns 400 "No such
+ emoticon"). Callers should treat failures as non-fatal: older BBDC
+ versions return 404 on this endpoint and a missing reaction must not
+ block event processing.
+ """
+ server_url = self.BASE_URL.rsplit('/rest/api/1.0', 1)[0]
+ url = (
+ f'{server_url}/rest/comment-likes/latest/projects/{owner}'
+ f'/repos/{repo_slug}/pull-requests/{pr_id}/comments/{comment_id}'
+ f'/reactions/{emoticon}'
+ )
+ await self._make_request(url, method=RequestMethod.PUT)
+
async def user_has_write_access(self, owner: str, repo_slug: str) -> bool:
"""Self-permission analog of :meth:`user_has_write_access_for`.