Use MergeRequest.by_commit_shas to find which MRs to update on push
Currently, when you push to a branch, we try to update related merge requests. A related merge request is:
- One where the source branch and source project match the push.
- One where the target branch and target project match the push, and:
- The push was a force push.
- The push contains a commit SHA in the most recent version of the MR (i.e. the same commit made its way into the target branch outside of this MR).
The first is much more likely than either of the second two. We currently do the last item (comparing commit SHAs) by:
- Loading all MRs that could match.
- Looking at their commit SHAs one by one.
Instead, we could:
- Do one query for where source branch and source project match.
- Do another query for where target branch and target project match.
- If it's a force push, that's all we need.
- If it's not, we can create a method like
MergeRequest.by_commit_sha
, but that applies to multiple SHAs. Then we add a single condition for a single query to load the relevant MRs, without having to load any commits into memory from the DB.
Edited by Sean McGivern