Commit 92a5becc authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '201745-add-from-fork-scope' into 'master'

Add from_fork scope

See merge request gitlab-org/gitlab!64177
parents a2a6cbb5 6aa839eb
......@@ -263,8 +263,9 @@ class MergeRequest < ApplicationRecord
scope :by_milestone, ->(milestone) { where(milestone_id: milestone) }
scope :of_projects, ->(ids) { where(target_project_id: ids) }
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
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
scope :merged, -> { with_state(:merged) }
scope :closed_and_merged, -> { with_states(:closed, :merged) }
......
......@@ -294,14 +294,14 @@ module MergeRequests
@source_merge_requests ||= merge_requests_for(@push.branch_name)
end
# rubocop: disable CodeReuse/ActiveRecord
def merge_requests_for_forks
@merge_requests_for_forks ||=
MergeRequest.opened
.where(source_branch: @push.branch_name, source_project: @project)
.where.not(target_project: @project)
MergeRequest
.opened
.from_project(project)
.from_source_branches(@push.branch_name)
.from_fork
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
......
......@@ -4951,4 +4951,15 @@ RSpec.describe MergeRequest, factory_default: :keep do
it { is_expected.to eq(true) }
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
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