Post-merge pipeline status isn't showing the correct pipeline

Summary

Post-merge pipeline status isn't showing the pipeline linked to the merge. Instead it is showing the latest pipeline that run on the commit linked to the MR. Soon after the merge, the latest pipeline is the same as the first pipeline that run following the merge. After few minutes, this can change and the MR is updated with wrong pipeline information.

We should always see the pipeline resulting of what happens to the target branch as a consequence of the merge.

Steps to reproduce

1- Use this CI configuration:

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" 
    - if: $CI_COMMIT_BRANCH

branchpipeline: 
  script:
    - cat nonexistentfile.yml 
  except:
    - schedules
    - merge_requests

mrpipeline:
  script:
    - sleep 5
  only:
    - merge_requests 

schedulepipeline: 
  script:
    - sleep 5
  only:
    - schedules

2- Create a pipeline schedule

3- Create an MR that you merged and will show correct post-merge pipeline status

Screenshot_2023-07-13_at_14.05.24

4- After few minutes (you can manually trigger the pipeline schedule if needed), the MR is updated with an unrelated pipeline

Screenshot_2023-07-13_at_15.36.24

Example Project

Reproduced on GitLab.com

What is the current bug behavior?

The Post-merge pipeline status is showing the latest pipeline that run on the commit linked to the MR. This leads to confusion as we expect to see the result of what happens to the target branch as a consequence of the merge.

What is the expected correct behavior?

Documentation:

When a merge request is merged, you can see the post-merge pipeline status of the branch the merge request was merged into.

Implementation issue:

  1. should be able to simply click in the MR on the new pipeline that was spun up as a result of the merge approval.
  2. The results of that pipeline should also be put back into the MR.

Relevant logs and/or screenshots

Possible fixes