summaryrefslogtreecommitdiff
path: root/app/finders
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-03-04 15:11:19 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-03-04 15:11:19 +0000
commit770adf92515e4311dfb42d89750d32a1e0628913 (patch)
tree574db6e5e92af5c1a0ffe87be345fffa24bb95f7 /app/finders
parentd5d47b45ddddcef0f8fc80a35ca7a8a2a0765fd1 (diff)
downloadgitlab-ce-770adf92515e4311dfb42d89750d32a1e0628913.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/merge_requests/oldest_per_commit_finder.rb32
1 files changed, 29 insertions, 3 deletions
diff --git a/app/finders/merge_requests/oldest_per_commit_finder.rb b/app/finders/merge_requests/oldest_per_commit_finder.rb
index f50db43d7d2..5360f301036 100644
--- a/app/finders/merge_requests/oldest_per_commit_finder.rb
+++ b/app/finders/merge_requests/oldest_per_commit_finder.rb
@@ -15,19 +15,45 @@ module MergeRequests
# Returns a Hash that maps a commit ID to the oldest merge request that
# introduced that commit.
def execute(commits)
+ mapping = {}
+ shas = commits.map(&:id)
+
+ # To include merge requests by the commit SHA, we don't need to go through
+ # any diff rows.
+ #
+ # We can't squeeze all this into a single query, as the diff based data
+ # relies on a GROUP BY. On the other hand, retrieving MRs by their merge
+ # SHAs separately is much easier, and plenty fast.
+ @project
+ .merge_requests
+ .preload_target_project
+ .by_merge_commit_sha(shas)
+ .each do |mr|
+ # Merge SHAs can't be in the merge request itself. It _is_ possible a
+ # newer merge request includes the merge commit, but in that case we
+ # still want the oldest merge request.
+ mapping[mr.merge_commit_sha] = mr
+ end
+
+ remaining = shas - mapping.keys
+
+ return mapping if remaining.empty?
+
id_rows = MergeRequestDiffCommit
- .oldest_merge_request_id_per_commit(@project.id, commits.map(&:id))
+ .oldest_merge_request_id_per_commit(@project.id, remaining)
mrs = MergeRequest
.preload_target_project
.id_in(id_rows.map { |r| r[:merge_request_id] })
.index_by(&:id)
- id_rows.each_with_object({}) do |row, hash|
+ id_rows.each do |row|
if (mr = mrs[row[:merge_request_id]])
- hash[row[:sha]] = mr
+ mapping[row[:sha]] = mr
end
end
+
+ mapping
end
end
end