Skip to content

Resolve "CI_MERGE_REQUEST_DIFF_BASE_SHA not updating on branch change"

``## What does this MR do and why?

This MR fixes an issue where CI_MERGE_REQUEST_DIFF_BASE_SHA was not updating correctly when the target branch changes in a merge request. The problem occurred because the merge_request_diff_for method was returning the first matching diff instead of the most recent one.

The fix adds a reverse parameter to the merge_request_diff_for method that changes the search order from first-to-last to last-to-first, ensuring the most recent diff is returned when multiple diffs match the same criteria. The Ci::Pipeline#merge_request_diff method now calls merge_request_diff_for with reverse: true to get the latest diff.

This ensures that CI variables like CI_MERGE_REQUEST_DIFF_BASE_SHA reflect the most recent diff's base SHA when the target branch changes or multiple diffs exist for the same commit SHA.``

References

Closes #442031 (closed)

How to set up and validate locally

  1. Create a merge request with multiple diffs (e.g., by changing the target branch)
  2. Verify that CI_MERGE_REQUEST_DIFF_BASE_SHA reflects the most recent diff's base SHA
  3. Test that CI variables are updated correctly when the target branch changes

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Marc Shaw

Merge request reports

Loading