Commit 891e5f48 authored by James Lopez's avatar James Lopez Committed by Douglas Barbosa Alexandre

Update specs to cope with new label types and priorities

Fixed all related specs and also changed the logic to handle edge cases. This includes exporting and exporting of group labels, which will get associated with the new group (if any) or they will become normal project labels otherwise.

Found other issues to do with not being able to import all labels at once in the beginning of the JSON - code was much simpler when we import all labels and milestones associated to a project first, then the associations will find the already created labels instead of creating them from the associations themselves.
parent 2f7260b4
...@@ -738,6 +738,10 @@ class Project < ActiveRecord::Base ...@@ -738,6 +738,10 @@ class Project < ActiveRecord::Base
end end
end end
def all_labels
Label.find_by_project_id(self.id)
end
def find_service(list, name) def find_service(list, name)
list.find { |service| service.to_param == name } list.find { |service| service.to_param == name }
end end
......
module Gitlab module Gitlab
module ImportExport module ImportExport
class AttributeCleaner class AttributeCleaner
ALLOWED_REFERENCES = RelationFactory::PROJECT_REFERENCES + RelationFactory::USER_REFERENCES ALLOWED_REFERENCES = RelationFactory::PROJECT_REFERENCES + RelationFactory::USER_REFERENCES + ['group_id']
def self.clean!(relation_hash:) def self.clean!(relation_hash:)
relation_hash.reject! do |key, _value| relation_hash.reject! do |key, _value|
......
# Model relationships to be included in the project import/export # Model relationships to be included in the project import/export
project_tree: project_tree:
- :labels - labels:
:priorities
- milestones: - milestones:
- :events - :events
- issues: - issues:
...@@ -9,7 +10,8 @@ project_tree: ...@@ -9,7 +10,8 @@ project_tree:
- :author - :author
- :events - :events
- label_links: - label_links:
- :label - label:
:priorities
- milestone: - milestone:
- :events - :events
- snippets: - snippets:
...@@ -26,7 +28,8 @@ project_tree: ...@@ -26,7 +28,8 @@ project_tree:
- :merge_request_diff - :merge_request_diff
- :events - :events
- label_links: - label_links:
- :label - label:
:priorities
- milestone: - milestone:
- :events - :events
- pipelines: - pipelines:
......
...@@ -65,11 +65,17 @@ module Gitlab ...@@ -65,11 +65,17 @@ module Gitlab
# +value+ existing model to be included in the hash # +value+ existing model to be included in the hash
# +parsed_hash+ the original hash # +parsed_hash+ the original hash
def parse_hash(value) def parse_hash(value)
return nil if already_contains_methods?(value)
@attributes_finder.parse(value) do |hash| @attributes_finder.parse(value) do |hash|
{ include: hash_or_merge(value, hash) } { include: hash_or_merge(value, hash) }
end end
end end
def already_contains_methods?(value)
value.is_a?(Hash) && value.values.detect { |val| val[:methods]}
end
# Adds new model configuration to an existing hash with key +current_key+ # Adds new model configuration to an existing hash with key +current_key+
# It may include exceptions or other attribute detail configuration, parsed by +@attributes_finder+ # It may include exceptions or other attribute detail configuration, parsed by +@attributes_finder+
# #
......
...@@ -11,6 +11,7 @@ module Gitlab ...@@ -11,6 +11,7 @@ module Gitlab
merge_access_levels: 'ProtectedBranch::MergeAccessLevel', merge_access_levels: 'ProtectedBranch::MergeAccessLevel',
push_access_levels: 'ProtectedBranch::PushAccessLevel', push_access_levels: 'ProtectedBranch::PushAccessLevel',
labels: :project_labels, labels: :project_labels,
priorities: :label_priorities,
label: :project_label }.freeze label: :project_label }.freeze
USER_REFERENCES = %w[author_id assignee_id updated_by_id user_id].freeze USER_REFERENCES = %w[author_id assignee_id updated_by_id user_id].freeze
...@@ -23,8 +24,6 @@ module Gitlab ...@@ -23,8 +24,6 @@ module Gitlab
EXISTING_OBJECT_CHECK = %i[milestone milestones label labels project_label project_labels project_label group_label].freeze EXISTING_OBJECT_CHECK = %i[milestone milestones label labels project_label project_labels project_label group_label].freeze
FINDER_ATTRIBUTES = %w[title project_id].freeze
def self.create(*args) def self.create(*args)
new(*args).create new(*args).create
end end
...@@ -134,6 +133,7 @@ module Gitlab ...@@ -134,6 +133,7 @@ module Gitlab
def handle_group_label def handle_group_label
# If there's no group, move the label to a project label # If there's no group, move the label to a project label
if @relation_hash['group_id'] if @relation_hash['group_id']
@relation_hash['project_id'] = nil
@relation_name = :group_label @relation_name = :group_label
else else
@relation_hash['type'] = 'ProjectLabel' @relation_hash['type'] = 'ProjectLabel'
...@@ -188,11 +188,9 @@ module Gitlab ...@@ -188,11 +188,9 @@ module Gitlab
# Otherwise always create the record, skipping the extra SELECT clause. # Otherwise always create the record, skipping the extra SELECT clause.
@existing_or_new_object ||= begin @existing_or_new_object ||= begin
if EXISTING_OBJECT_CHECK.include?(@relation_name) if EXISTING_OBJECT_CHECK.include?(@relation_name)
events = parsed_relation_hash.delete('events') attribute_hash = attribute_hash_for(['events', 'priorities'])
unless events.blank? existing_object.assign_attributes(attribute_hash) if attribute_hash.any?
existing_object.assign_attributes(events: events)
end
existing_object existing_object
else else
...@@ -201,14 +199,22 @@ module Gitlab ...@@ -201,14 +199,22 @@ module Gitlab
end end
end end
def attribute_hash_for(attributes)
attributes.inject({}) do |hash, value|
hash[value] = parsed_relation_hash.delete(value) if parsed_relation_hash[value]
hash
end
end
def existing_object def existing_object
@existing_object ||= @existing_object ||=
begin begin
finder_hash = parsed_relation_hash.slice(*FINDER_ATTRIBUTES) finder_attributes = @relation_name == :group_label ? %w[title group_id] : %w[title project_id]
finder_hash = parsed_relation_hash.slice(*finder_attributes)
existing_object = relation_class.find_or_create_by(finder_hash) existing_object = relation_class.find_or_create_by(finder_hash)
# Done in two steps, as MySQL behaves differently than PostgreSQL using # Done in two steps, as MySQL behaves differently than PostgreSQL using
# the +find_or_create_by+ method and does not return the ID the second time. # the +find_or_create_by+ method and does not return the ID the second time.
existing_object.update(parsed_relation_hash) existing_object.update!(parsed_relation_hash)
existing_object existing_object
end end
end end
......
...@@ -38,6 +38,7 @@ label: ...@@ -38,6 +38,7 @@ label:
- label_links - label_links
- issues - issues
- merge_requests - merge_requests
- priorities
milestone: milestone:
- project - project
- issues - issues
...@@ -186,3 +187,5 @@ project: ...@@ -186,3 +187,5 @@ project:
award_emoji: award_emoji:
- awardable - awardable
- user - user
priorities:
- label
\ No newline at end of file
...@@ -2,6 +2,21 @@ ...@@ -2,6 +2,21 @@
"description": "Nisi et repellendus ut enim quo accusamus vel magnam.", "description": "Nisi et repellendus ut enim quo accusamus vel magnam.",
"visibility_level": 10, "visibility_level": 10,
"archived": false, "archived": false,
"labels": [
{
"id": 2,
"title": "test2",
"color": "#428bca",
"project_id": 8,
"created_at": "2016-07-22T08:55:44.161Z",
"updated_at": "2016-07-22T08:55:44.161Z",
"template": false,
"description": "",
"type": "ProjectLabel",
"priorities": [
]
}
],
"issues": [ "issues": [
{ {
"id": 40, "id": 40,
...@@ -64,7 +79,6 @@ ...@@ -64,7 +79,6 @@
"updated_at": "2016-07-22T08:55:44.161Z", "updated_at": "2016-07-22T08:55:44.161Z",
"template": false, "template": false,
"description": "", "description": "",
"priority": null,
"type": "ProjectLabel" "type": "ProjectLabel"
} }
}, },
...@@ -84,9 +98,18 @@ ...@@ -84,9 +98,18 @@
"updated_at": "2016-07-22T08:55:44.161Z", "updated_at": "2016-07-22T08:55:44.161Z",
"template": false, "template": false,
"description": "", "description": "",
"priority": null,
"project_id": null, "project_id": null,
"type": "GroupLabel" "type": "GroupLabel",
"priorities": [
{
"id": 1,
"project_id": 5,
"label_id": 1,
"priority": 1,
"created_at": "2016-10-18T09:35:43.338Z",
"updated_at": "2016-10-18T09:35:43.338Z"
}
]
} }
} }
], ],
...@@ -558,7 +581,6 @@ ...@@ -558,7 +581,6 @@
"updated_at": "2016-07-22T08:55:44.161Z", "updated_at": "2016-07-22T08:55:44.161Z",
"template": false, "template": false,
"description": "", "description": "",
"priority": null,
"type": "ProjectLabel" "type": "ProjectLabel"
} }
} }
...@@ -2249,9 +2271,6 @@ ...@@ -2249,9 +2271,6 @@
} }
] ]
} }
],
"labels": [
], ],
"milestones": [ "milestones": [
{ {
......
...@@ -134,6 +134,12 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do ...@@ -134,6 +134,12 @@ describe Gitlab::ImportExport::ProjectTreeRestorer, services: true do
expect(GroupLabel.count).to eq(1) expect(GroupLabel.count).to eq(1)
end end
it 'has label priorities' do
restored_project_json
expect(GroupLabel.first.priorities).not_to be_empty
end
end end
it 'has a project feature' do it 'has a project feature' do
......
...@@ -114,7 +114,13 @@ describe Gitlab::ImportExport::ProjectTreeSaver, services: true do ...@@ -114,7 +114,13 @@ describe Gitlab::ImportExport::ProjectTreeSaver, services: true do
it 'has project and group labels' do it 'has project and group labels' do
label_types = saved_project_json['issues'].first['label_links'].map { |link| link['label']['type']} label_types = saved_project_json['issues'].first['label_links'].map { |link| link['label']['type']}
expect(label_types).to match(['ProjectLabel', 'GroupLabel']) expect(label_types).to match_array(['ProjectLabel', 'GroupLabel'])
end
it 'has priorities associated to labels' do
priorities = saved_project_json['issues'].first['label_links'].map { |link| link['label']['priorities']}
expect(priorities.flatten).not_to be_empty
end end
it 'saves the correct service type' do it 'saves the correct service type' do
...@@ -154,6 +160,7 @@ describe Gitlab::ImportExport::ProjectTreeSaver, services: true do ...@@ -154,6 +160,7 @@ describe Gitlab::ImportExport::ProjectTreeSaver, services: true do
group_label = create(:group_label, group: group) group_label = create(:group_label, group: group)
create(:label_link, label: project_label, target: issue) create(:label_link, label: project_label, target: issue)
create(:label_link, label: group_label, target: issue) create(:label_link, label: group_label, target: issue)
create(:label_priority, label: group_label, priority: 1)
milestone = create(:milestone, project: project) milestone = create(:milestone, project: project)
merge_request = create(:merge_request, source_project: project, milestone: milestone) merge_request = create(:merge_request, source_project: project, milestone: milestone)
commit_status = create(:commit_status, project: project) commit_status = create(:commit_status, project: project)
......
...@@ -60,7 +60,7 @@ LabelLink: ...@@ -60,7 +60,7 @@ LabelLink:
- target_type - target_type
- created_at - created_at
- updated_at - updated_at
Label: ProjectLabel:
- id - id
- title - title
- color - color
...@@ -331,3 +331,10 @@ AwardEmoji: ...@@ -331,3 +331,10 @@ AwardEmoji:
- awardable_type - awardable_type
- created_at - created_at
- updated_at - updated_at
LabelPriority:
- id
- project_id
- label_id
- priority
- created_at
- updated_at
\ No newline at end of file
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