2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
# Given a class, it finds or creates a new object
|
|
|
|
# (initializes in the case of Label) at group or project level.
|
|
|
|
# If it does not exist in the group, it creates it at project level.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# `GroupProjectObjectBuilder.build(Label, label_attributes)`
|
|
|
|
# finds or initializes a label with the given attributes.
|
|
|
|
#
|
|
|
|
# It also adds some logic around Group Labels/Milestones for edge cases.
|
|
|
|
class GroupProjectObjectBuilder
|
|
|
|
def self.build(*args)
|
|
|
|
Project.transaction do
|
|
|
|
new(*args).find
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(klass, attributes)
|
|
|
|
@klass = klass < Label ? Label : klass
|
|
|
|
@attributes = attributes
|
|
|
|
@group = @attributes['group']
|
|
|
|
@project = @attributes['project']
|
|
|
|
end
|
|
|
|
|
|
|
|
def find
|
2019-12-21 20:55:43 +05:30
|
|
|
find_object || klass.create(project_attributes)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
attr_reader :klass, :attributes, :group, :project
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def find_object
|
2019-12-21 20:55:43 +05:30
|
|
|
klass.where(where_clause).first
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def where_clause
|
2019-12-21 20:55:43 +05:30
|
|
|
where_clauses.reduce(:and)
|
|
|
|
end
|
|
|
|
|
|
|
|
def where_clauses
|
|
|
|
[
|
|
|
|
where_clause_base,
|
|
|
|
where_clause_for_title,
|
|
|
|
where_clause_for_klass
|
|
|
|
].compact
|
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
# 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
|
2019-12-21 20:55:43 +05:30
|
|
|
# or, if group is present:
|
|
|
|
# `"{table_name}"."project_id" = {project.id} OR "{table_name}"."group_id" = {group.id}`
|
|
|
|
def where_clause_base
|
2019-12-26 22:10:19 +05:30
|
|
|
clause = table[:project_id].eq(project.id) if project
|
2019-12-21 20:55:43 +05:30
|
|
|
clause = clause.or(table[:group_id].eq(group.id)) if group
|
|
|
|
|
|
|
|
clause
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
# Returns Arel clause `"{table_name}"."title" = '{attributes['title']}'`
|
|
|
|
# if attributes has 'title key, otherwise `nil`.
|
|
|
|
def where_clause_for_title
|
|
|
|
attrs_to_arel(attributes.slice('title'))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns Arel clause:
|
|
|
|
# `"{table_name}"."{attrs.keys[0]}" = '{attrs.values[0]} AND {table_name}"."{attrs.keys[1]}" = '{attrs.values[1]}"`
|
|
|
|
# from the given Hash of attributes.
|
|
|
|
def attrs_to_arel(attrs)
|
|
|
|
attrs.map do |key, value|
|
|
|
|
table[key].eq(value)
|
|
|
|
end.reduce(:and)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def table
|
2019-12-21 20:55:43 +05:30
|
|
|
@table ||= klass.arel_table
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def project_attributes
|
2019-12-21 20:55:43 +05:30
|
|
|
attributes.except('group').tap do |atts|
|
2018-11-08 19:23:39 +05:30
|
|
|
if label?
|
|
|
|
atts['type'] = 'ProjectLabel' # Always create project labels
|
|
|
|
elsif milestone?
|
|
|
|
if atts['group_id'] # Transform new group milestones into project ones
|
|
|
|
atts['iid'] = nil
|
|
|
|
atts.delete('group_id')
|
|
|
|
else
|
|
|
|
claim_iid
|
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
atts['importing'] = true if klass.ancestors.include?(Importable)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def label?
|
2019-12-21 20:55:43 +05:30
|
|
|
klass == Label
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def milestone?
|
2019-12-21 20:55:43 +05:30
|
|
|
klass == Milestone
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def merge_request?
|
|
|
|
klass == MergeRequest
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
# 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:
|
|
|
|
# - Importing into a user namespace project with exported group milestones
|
|
|
|
# where the IID of the Group milestone could conflict with a project one.
|
|
|
|
def claim_iid
|
|
|
|
# The milestone has to be a group milestone, as it's the only case where
|
|
|
|
# we set the IID as the maximum. The rest of them are fixed.
|
2019-12-21 20:55:43 +05:30
|
|
|
milestone = project.milestones.find_by(iid: attributes['iid'])
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
return unless milestone
|
|
|
|
|
|
|
|
milestone.iid = nil
|
|
|
|
milestone.ensure_project_iid!
|
|
|
|
milestone.save!
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Returns Arel clause for a particular model or `nil`.
|
|
|
|
def where_clause_for_klass
|
2019-12-26 22:10:19 +05:30
|
|
|
return attrs_to_arel(attributes.slice('iid')) if merge_request?
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
Gitlab::ImportExport::GroupProjectObjectBuilder.prepend_if_ee('EE::Gitlab::ImportExport::GroupProjectObjectBuilder')
|