2018-08-16 06:07:43 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Issues
|
|
|
|
class ReferencedMergeRequestsService < Issues::BaseService
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-16 06:07:43 -04:00
|
|
|
def execute(issue)
|
2018-08-16 08:23:02 -04:00
|
|
|
referenced = referenced_merge_requests(issue)
|
|
|
|
closed_by = closed_by_merge_requests(issue)
|
|
|
|
preloader = ActiveRecord::Associations::Preloader.new
|
|
|
|
|
|
|
|
preloader.preload(referenced + closed_by,
|
|
|
|
head_pipeline: { project: [:route, { namespace: :route }] })
|
|
|
|
|
|
|
|
[sort_by_iid(referenced), sort_by_iid(closed_by)]
|
2018-08-16 06:07:43 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-16 06:07:43 -04:00
|
|
|
|
|
|
|
def referenced_merge_requests(issue)
|
2018-08-21 07:56:01 -04:00
|
|
|
merge_requests = extract_merge_requests(issue)
|
2018-08-16 06:07:43 -04:00
|
|
|
|
|
|
|
cross_project_filter = -> (merge_requests) do
|
|
|
|
merge_requests.select { |mr| mr.target_project == project }
|
|
|
|
end
|
|
|
|
|
|
|
|
Ability.merge_requests_readable_by_user(
|
|
|
|
merge_requests,
|
|
|
|
current_user,
|
|
|
|
filters: {
|
|
|
|
read_cross_project: cross_project_filter
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-16 06:07:43 -04:00
|
|
|
def closed_by_merge_requests(issue)
|
|
|
|
return [] unless issue.open?
|
|
|
|
|
2018-08-21 07:56:01 -04:00
|
|
|
merge_requests = extract_merge_requests(issue, filter: :system).select(&:open?)
|
2018-08-16 06:07:43 -04:00
|
|
|
|
|
|
|
return [] if merge_requests.empty?
|
|
|
|
|
|
|
|
ids = MergeRequestsClosingIssues.where(merge_request_id: merge_requests.map(&:id), issue_id: issue.id).pluck(:merge_request_id)
|
|
|
|
merge_requests.select { |mr| mr.id.in?(ids) }
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-16 06:07:43 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-21 07:56:01 -04:00
|
|
|
def extract_merge_requests(issue, filter: nil)
|
2018-08-16 07:08:00 -04:00
|
|
|
ext = issue.all_references(current_user)
|
2018-08-21 07:56:01 -04:00
|
|
|
notes = issue_notes(issue)
|
|
|
|
notes = notes.select(&filter) if filter
|
2018-08-16 07:08:00 -04:00
|
|
|
|
2018-08-21 07:56:01 -04:00
|
|
|
notes.each do |note|
|
2018-08-16 07:08:00 -04:00
|
|
|
note.all_references(current_user, extractor: ext)
|
|
|
|
end
|
|
|
|
|
|
|
|
ext.merge_requests
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-21 07:56:01 -04:00
|
|
|
def issue_notes(issue)
|
|
|
|
@issue_notes ||= {}
|
|
|
|
@issue_notes[issue] ||= issue.notes.includes(:author)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-21 07:56:01 -04:00
|
|
|
|
2018-08-16 06:07:43 -04:00
|
|
|
def sort_by_iid(merge_requests)
|
|
|
|
Gitlab::IssuableSorter.sort(project, merge_requests) { |mr| mr.iid.to_s }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|