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