Commit 6aa839eb authored by Patrick Bajao's avatar Patrick Bajao

Add from_fork scope

In https://gitlab.com/gitlab-org/gitlab/-/merge_requests/22418,
a change has been made that required the need to disable the
`CodeReuse/ActiveRecord` cop on `merge_requests_for_forks`.

To be able to enable it again, added `from_fork` scope and use
that instead.
parent 6bcc37b3
...@@ -263,8 +263,9 @@ class MergeRequest < ApplicationRecord ...@@ -263,8 +263,9 @@ class MergeRequest < ApplicationRecord
scope :by_milestone, ->(milestone) { where(milestone_id: milestone) } scope :by_milestone, ->(milestone) { where(milestone_id: milestone) }
scope :of_projects, ->(ids) { where(target_project_id: ids) } scope :of_projects, ->(ids) { where(target_project_id: ids) }
scope :from_project, ->(project) { where(source_project_id: project.id) } scope :from_project, ->(project) { where(source_project_id: project.id) }
scope :from_fork, -> { where('source_project_id <> target_project_id') }
scope :from_and_to_forks, ->(project) do scope :from_and_to_forks, ->(project) do
where('source_project_id <> target_project_id AND (source_project_id = ? OR target_project_id = ?)', project.id, project.id) from_fork.where('source_project_id = ? OR target_project_id = ?', project.id, project.id)
end end
scope :merged, -> { with_state(:merged) } scope :merged, -> { with_state(:merged) }
scope :closed_and_merged, -> { with_states(:closed, :merged) } scope :closed_and_merged, -> { with_states(:closed, :merged) }
......
...@@ -294,14 +294,14 @@ module MergeRequests ...@@ -294,14 +294,14 @@ module MergeRequests
@source_merge_requests ||= merge_requests_for(@push.branch_name) @source_merge_requests ||= merge_requests_for(@push.branch_name)
end end
# rubocop: disable CodeReuse/ActiveRecord
def merge_requests_for_forks def merge_requests_for_forks
@merge_requests_for_forks ||= @merge_requests_for_forks ||=
MergeRequest.opened MergeRequest
.where(source_branch: @push.branch_name, source_project: @project) .opened
.where.not(target_project: @project) .from_project(project)
.from_source_branches(@push.branch_name)
.from_fork
end end
# rubocop: enable CodeReuse/ActiveRecord
end end
end end
......
...@@ -4951,4 +4951,15 @@ RSpec.describe MergeRequest, factory_default: :keep do ...@@ -4951,4 +4951,15 @@ RSpec.describe MergeRequest, factory_default: :keep do
it { is_expected.to eq(true) } it { is_expected.to eq(true) }
end end
end end
describe '.from_fork' do
let!(:project) { create(:project, :repository) }
let!(:forked_project) { fork_project(project) }
let!(:fork_mr) { create(:merge_request, source_project: forked_project, target_project: project) }
let!(:regular_mr) { create(:merge_request, source_project: project) }
it 'returns merge requests from forks only' do
expect(described_class.from_fork).to eq([fork_mr])
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment