Skip to content

Add Bitbucket PR comment support#206

Draft
Orlando Barrera II (obarrera) wants to merge 4 commits into
mainfrom
orlando/ce-89-add-comment-ability-for-bitbucket-for-python-cli
Draft

Add Bitbucket PR comment support#206
Orlando Barrera II (obarrera) wants to merge 4 commits into
mainfrom
orlando/ce-89-add-comment-ability-for-bitbucket-for-python-cli

Conversation

@obarrera
Copy link
Copy Markdown
Contributor

@obarrera Orlando Barrera II (obarrera) commented May 28, 2026

Summary

  • Adds socketsecurity/core/scm/bitbucket.py with BitbucketConfig + Bitbucket SCM that posts/updates Dependency Overview and Security Issue comments on Bitbucket Cloud PRs, mirroring the GitHub and GitLab modules.
  • Reads BITBUCKET_* Pipelines env vars; supports Bearer (BITBUCKET_TOKEN) or Basic auth (BITBUCKET_USERNAME + BITBUCKET_APP_PASSWORD). BITBUCKET_API_URL lets Server/Data Center users point at a self-hosted REST API.
  • Normalizes Bitbucket's {content: {raw}, user: {nickname, uuid}} payload onto the shared Comment dataclass so the existing Comments helpers, ignore-command flow, and security-comment scrubbing all work unchanged.
  • Substitutes Bitbucket's missing reaction API with a hidden <!-- socket-ignore-processed --> marker that has_thumbsup_reaction looks for on subsequent runs.
  • Wires --scm bitbucket into socketcli.py and updates workflows/bitbucket-pipelines.yml to use it.

Why

The Bitbucket comment implementation was partially completed, but couldn't get the API to accept comments. Most likely culprit: GitHub-style {"body": "..."} form-encoded payloads, where Bitbucket Cloud requires JSON {"content": {"raw": "..."}} with Content-Type: application/json. This PR uses that shape.

Out of scope

  • Azure DevOps — left for a follow-up since it's a separate API surface.
  • Live smoke test on a real Bitbucket workspace — a test instance still needs to be set up.

Test plan

  • uv run pytest tests/unit/test_bitbucket.py — 15 new tests covering env parsing (Bearer + Basic + missing creds), default-branch detection, event-type detection, comment normalization (raw content, deleted, missing fields), POST/PUT payload shape, and create-vs-update dispatch.
  • uv run pytest tests/unit -q --no-cov — 164 passed, 2 pre-existing skips.
  • End-to-end run against a Bitbucket Cloud PR on a workspace with BITBUCKET_TOKEN configured.
  • End-to-end run with BITBUCKET_USERNAME / BITBUCKET_APP_PASSWORD to confirm Basic auth path.
  • Verify ignore-comment flow: post @SocketSecurity ignore npm/foo@1.0.0, re-run, confirm alert filtered + hidden marker appended.
  • (Optional) Bitbucket Server / Data Center run with BITBUCKET_API_URL override.

Adds a Bitbucket SCM implementation mirroring the GitHub/GitLab modules
so socketcli can post and update Dependency Overview and Security Issue
comments on Bitbucket Cloud pull requests. Includes config from
BITBUCKET_* Pipelines env vars (Bearer token or username/app-password
auth), comment shape normalization onto the existing Comment dataclass,
and unit tests covering env parsing, event detection, normalization, and
POST/PUT payload shape.

Refs CE-89

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

🚀 Preview package published!

Install with:

pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socketsecurity==2.2.92.dev1

Docker image: socketdev/cli:pr-206

lelia and others added 2 commits May 28, 2026 12:48
- Fix paginated comment fetch: previous code passed base_url='' for the
  absolute 'next' URL, which CliClient (falsy check) silently mapped to
  Socket's API base. Split the URL into origin + path so the request
  hits Bitbucket.
- Drop content.get('markup') body fallback (markup is the format name,
  not body text); fall back to content.get('html') instead.
- Remove dead hasattr() branch in has_thumbsup_reaction.
- Move PROCESSED_MARKER to top of class with a comment explaining why
  there's no Bearer/Basic auth-fallback retry like GitLab has.
- Document that --scm bitbucket exits 2 without BITBUCKET_TOKEN or
  BITBUCKET_USERNAME+BITBUCKET_APP_PASSWORD set.
- Expand tests: pagination (origin split, no-next, error response,
  no-PR), has_thumbsup_reaction (marker present/absent, no-PR,
  exception swallowing), _mark_comment_processed (append, idempotence,
  swallow update error), check_for_socket_comments classification, and
  _split_absolute_url round-trip.

Signed-off-by: lelia <2418071+lelia@users.noreply.github.com>
Required by the check_version workflow now that main is at 2.2.90. Bumps
__version__ in socketsecurity/__init__.py and pyproject.toml; uv.lock
regenerated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@lelia lelia changed the title Add Bitbucket PR comment support (CE-89) Add Bitbucket PR comment support May 28, 2026
- Cache comment bodies in get_comments_for_pr so has_thumbsup_reaction
  resolves from memory instead of issuing one extra GET per ignore
  comment on every run. _mark_comment_processed refreshes the cache so
  in-run repeat checks also hit it.
- Guard against {"values": null} responses from Bitbucket Cloud — the
  previous .get("values", []) default only fires on missing key, so a
  null value would TypeError on iteration.
- Fail fast in BitbucketConfig.from_env when workspace or repo_slug
  can't be determined, rather than building 404-bound URLs deeper in
  the request flow.
- Drop typing.Tuple in favor of built-in tuple (project targets 3.11+).
- Document BITBUCKET_DEFAULT_BRANCH in the example pipeline since
  Bitbucket Pipelines doesn't export the repo default branch.
- Add tests for cache hits, cache fallback, mark-processed cache
  refresh, null-values pagination, and the new workspace/repo
  validation exit.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants