Skip to content

fix(ci): map BITBUCKET_PR_DESTINATION_COMMIT to base branch SHA for patch coverage#2394

Merged
albabn merged 5 commits into
masterfrom
alba/fix-coverage-fetch-base-branch
Jun 30, 2026
Merged

fix(ci): map BITBUCKET_PR_DESTINATION_COMMIT to base branch SHA for patch coverage#2394
albabn merged 5 commits into
masterfrom
alba/fix-coverage-fetch-base-branch

Conversation

@albabn

@albabn albabn commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

What and why?

Patch coverage never appears for Bitbucket PRs — overall coverage works fine but the "Patch Coverage" column always shows "No data".

Root cause: Bitbucket Pipelines does a single-branch clone and clone: depth: full only affects commit-history depth, not ref scope (BCLOUD-14719, resolved Won't Do). In PR pipelines, destination-branch commits are reachable in history (Bitbucket merges them at build setup), but origin/ is absent as a remote-tracking ref — so git merge-base HEAD fails silently, @report.patch_percentage is never computed, and patch coverage is never uploaded.

GitHub Actions and GitLab CI are unaffected because they provide a base SHA directly (pull_request.base.sha / CI_MERGE_REQUEST_DIFF_BASE_SHA), taking the if (baseSha) code path in getHeadAndBase that never reaches the branch-name logic.

How?

Map BITBUCKET_PR_DESTINATION_COMMIT → GIT_PULL_REQUEST_BASE_BRANCH_SHA in the Bitbucket CI detection block. datadog-ci then takes the same baseSha code path as GitHub and GitLab — calling getMergeBase(git, BITBUCKET_PR_DESTINATION_COMMIT, HEAD) to find the true common ancestor — with no additional git operations needed. Non-PR pipelines are unaffected: BITBUCKET_PR_DESTINATION_COMMIT is only set in PR pipeline contexts, where patch coverage is meaningful.

Review checklist

Two new tests in ci.test.ts cover both cases: PR pipeline with BITBUCKET_PR_DESTINATION_COMMIT set (base SHA populated) and without (base SHA absent, existing behaviour preserved).

Some CI environments (e.g. Bitbucket Pipelines) only clone the current
branch by default, leaving the base branch ref unavailable locally.
This causes git merge-base to fail silently and patch coverage to be
missing from the uploaded event.

Explicitly fetch origin/<baseBranch> before computing the merge-base,
guarded in a try/catch so the fix degrades gracefully on fetch failure.

GitHub Actions and GitLab CI are unaffected — they supply a base SHA
directly and take the earlier code path. Azure/CircleCI/Jenkins hit
this path but already have all refs available, so the fetch is a no-op.
@datadog-prod-us1-4

datadog-prod-us1-4 Bot commented Jun 30, 2026

Copy link
Copy Markdown

Tests

🎉 All green!

🧪 All tests passed

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 1df71b2 | Docs | Datadog PR Page | Give us feedback!

@albabn
albabn marked this pull request as ready for review June 30, 2026 13:43
@albabn
albabn requested review from a team as code owners June 30, 2026 13:43
@albabn
albabn requested a review from henrywalsh June 30, 2026 13:43
@albabn albabn added the software-delivery Related to [coverage, deployment, dora, junit, measure, tag, trace] label Jun 30, 2026

@datadog-prod-us1-4 datadog-prod-us1-4 Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Datadog Autotest: PASS

More details

PR adds a git fetch origin/<baseBranch> call before merge-base when only a branch name is available, fixing patch coverage for Bitbucket Pipelines and similar shallow-clone CIs. The fix is wrapped in try/catch to degrade gracefully when fetch fails, preserving backward compatibility. Five new tests validate all code paths: missing metadata, the baseSha path (unchanged), the baseBranch path (new fetch behavior), and graceful failure modes.

Was this helpful? React 👍 or 👎

Open Bits AI session

🤖 Datadog Autotest · Commit 87d3ea0 · What is Autotest? · Any feedback? Reach out in #autotest

Bitbucket Pipelines merges the destination branch into the source at
build setup, so destination-branch commits are reachable in PR pipeline
history. However origin/<baseBranch> is absent as a remote-tracking
ref, causing git merge-base to fail silently and patch coverage to be
missing.

Rather than fetching the ref (previous approach, reverted), map
BITBUCKET_PR_DESTINATION_COMMIT to GIT_PULL_REQUEST_BASE_BRANCH_SHA in
the CI detection layer. datadog-ci then takes the direct baseSha code
path — same as GitHub Actions (pull_request.base.sha) and GitLab CI
(CI_MERGE_REQUEST_DIFF_BASE_SHA) — with no git operations needed.

Non-PR pipelines are unaffected: BITBUCKET_PR_DESTINATION_COMMIT is
only set in PR pipeline contexts, where patch coverage is meaningful.
@albabn
albabn requested a review from a team as a code owner June 30, 2026 14:08
@albabn albabn changed the title fix(coverage): fetch base branch before merge-base for shallow-clone CIs fix(ci): map BITBUCKET_PR_DESTINATION_COMMIT to base branch SHA for patch coverage Jun 30, 2026
@albabn
albabn merged commit db28094 into master Jun 30, 2026
34 checks passed
@albabn
albabn deleted the alba/fix-coverage-fetch-base-branch branch June 30, 2026 14:23
@TalUsvyatsky TalUsvyatsky mentioned this pull request Jul 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

software-delivery Related to [coverage, deployment, dora, junit, measure, tag, trace]

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants