Commit dddffc66 authored by Stan Hu's avatar Stan Hu

Merge branch '35339-refactor-fix-for-pipelines-with-merge-request' into 'master'

Remove merge_request specific code from Relation Factory

See merge request gitlab-org/gitlab!19825
parents dc45d724 c27b088d
......@@ -49,11 +49,12 @@ module Gitlab
].compact
end
# Returns Arel clause `"{table_name}"."project_id" = {project.id}`
# Returns Arel clause `"{table_name}"."project_id" = {project.id}` if project is present
# For example: merge_request has :target_project_id, and we are searching by :iid
# or, if group is present:
# `"{table_name}"."project_id" = {project.id} OR "{table_name}"."group_id" = {group.id}`
def where_clause_base
clause = table[:project_id].eq(project.id)
clause = table[:project_id].eq(project.id) if project
clause = clause.or(table[:group_id].eq(group.id)) if group
clause
......@@ -103,6 +104,10 @@ module Gitlab
klass == Milestone
end
def merge_request?
klass == MergeRequest
end
# If an existing group milestone used the IID
# claim the IID back and set the group milestone to use one available
# This is necessary to fix situations like the following:
......@@ -124,7 +129,7 @@ module Gitlab
# Returns Arel clause for a particular model or `nil`.
def where_clause_for_klass
# no-op
return attrs_to_arel(attributes.slice('iid')) if merge_request?
end
end
end
......
......@@ -329,8 +329,6 @@ module Gitlab
def find_or_create_object!
return relation_class.find_or_create_by(project_id: @project.id) if UNIQUE_RELATIONS.include?(@relation_name)
return find_or_create_merge_request! if @relation_name == :merge_request
# Can't use IDs as validation exists calling `group` or `project` attributes
finder_hash = parsed_relation_hash.tap do |hash|
hash['group'] = @project.group if relation_class.attribute_method?('group_id')
......@@ -340,11 +338,6 @@ module Gitlab
GroupProjectObjectBuilder.build(relation_class, finder_hash)
end
def find_or_create_merge_request!
@project.merge_requests.find_by(iid: parsed_relation_hash['iid']) ||
relation_class.new(parsed_relation_hash)
end
end
end
end
......@@ -2,7 +2,7 @@ require 'spec_helper'
describe Gitlab::ImportExport::GroupProjectObjectBuilder do
let(:project) do
create(:project,
create(:project, :repository,
:builds_disabled,
:issues_disabled,
name: 'project',
......@@ -11,8 +11,8 @@ describe Gitlab::ImportExport::GroupProjectObjectBuilder do
end
context 'labels' do
it 'finds the right group label' do
group_label = create(:group_label, 'name': 'group label', 'group': project.group)
it 'finds the existing group label' do
group_label = create(:group_label, name: 'group label', group: project.group)
expect(described_class.build(Label,
'title' => 'group label',
......@@ -31,8 +31,8 @@ describe Gitlab::ImportExport::GroupProjectObjectBuilder do
end
context 'milestones' do
it 'finds the right group milestone' do
milestone = create(:milestone, 'name' => 'group milestone', 'group' => project.group)
it 'finds the existing group milestone' do
milestone = create(:milestone, name: 'group milestone', group: project.group)
expect(described_class.build(Milestone,
'title' => 'group milestone',
......@@ -49,4 +49,30 @@ describe Gitlab::ImportExport::GroupProjectObjectBuilder do
expect(milestone.persisted?).to be true
end
end
context 'merge_request' do
it 'finds the existing merge_request' do
merge_request = create(:merge_request, title: 'MergeRequest', iid: 7, target_project: project, source_project: project)
expect(described_class.build(MergeRequest,
'title' => 'MergeRequest',
'source_project_id' => project.id,
'target_project_id' => project.id,
'source_branch' => 'SourceBranch',
'iid' => 7,
'target_branch' => 'TargetBranch',
'author_id' => project.creator.id)).to eq(merge_request)
end
it 'creates a new merge_request' do
merge_request = described_class.build(MergeRequest,
'title' => 'MergeRequest',
'iid' => 8,
'source_project_id' => project.id,
'target_project_id' => project.id,
'source_branch' => 'SourceBranch',
'target_branch' => 'TargetBranch',
'author_id' => project.creator.id)
expect(merge_request.persisted?).to be true
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