Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Allow for seeding BCFs

Wieland Lindenthalb515f87444c2
diff --git a/app/seeders/demo_data/work_package_seeder.rb b/app/seeders/demo_data/work_package_seeder.rb
index 23930b0a090..acc6440a8f0 100644
--- a/app/seeders/demo_data/work_package_seeder.rb
+++ b/app/seeders/demo_data/work_package_seeder.rb
@@ -58,10 +58,17 @@ module DemoData
work_packages_data.each do |attributes|
print '.'
- create_work_package attributes
+ create_or_update_work_package(attributes)
end
end
+ # Decides what to do with work package seed data.
+ # The default here is to create the work package.
+ # Modules may patch this method.
+ def create_or_update_work_package(attributes)
+ create_work_package(attributes)
+ end
+
def create_work_package(attributes)
wp_attr = base_work_package_attributes attributes
diff --git a/modules/bim_seeder/app/seeders/bim_seeder/demo_data/bcf_xml_seeder.rb b/modules/bim_seeder/app/seeders/bim_seeder/demo_data/bcf_xml_seeder.rb
new file mode 100644
index 00000000000..26d34dfdb79
--- /dev/null
+++ b/modules/bim_seeder/app/seeders/bim_seeder/demo_data/bcf_xml_seeder.rb
@@ -0,0 +1,61 @@
+#-- encoding: UTF-8
+
+#-- copyright
+
+# OpenProject is a project management system.
+# Copyright (C) 2012-2019 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+module BimSeeder
+ module DemoData
+ class BcfXmlSeeder < ::Seeder
+ attr_reader :project, :key
+
+ def initialize(project, key)
+ @project = project
+ @key = key
+ end
+
+ def seed_data!
+ filename = project_data_for(key, 'bcf_xml_file')
+ return unless filename.present?
+
+ user = User.admin.first
+
+ print ' ↳ Import BCF XML file'
+
+ import_options = {
+ invalid_people_action: 'anonymize',
+ unknown_mails_action: 'anonymize',
+ non_members_action: 'anonymize'
+ }
+
+ bcf_xml_file = File.new(File.join(Rails.root, 'modules/bim_seeder/files', filename))
+ importer = ::OpenProject::Bcf::BcfXml::Importer.new(bcf_xml_file, project, current_user: user)
+ importer.import!(import_options).flatten
+ end
+ end
+ end
+end
diff --git a/modules/bim_seeder/lib/open_project/bim_seeder/engine.rb b/modules/bim_seeder/lib/open_project/bim_seeder/engine.rb
index 84c2af82899..a5e7cb00576 100644
--- a/modules/bim_seeder/lib/open_project/bim_seeder/engine.rb
+++ b/modules/bim_seeder/lib/open_project/bim_seeder/engine.rb
@@ -14,5 +14,7 @@ module OpenProject::BimSeeder
patches [:RootSeeder]
patch_with_namespace :DemoData, :QueryBuilder
+ patch_with_namespace :DemoData, :ProjectSeeder
+ patch_with_namespace :DemoData, :WorkPackageSeeder
end
end
diff --git a/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb b/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb
new file mode 100644
index 00000000000..8e23e066ff7
--- /dev/null
+++ b/modules/bim_seeder/lib/open_project/bim_seeder/patches/project_seeder_patch.rb
@@ -0,0 +1,11 @@
+module OpenProject::BimSeeder::Patches::ProjectSeederPatch
+ def self.included(base) # :nodoc:
+ base.prepend InstanceMethods
+ end
+
+ module InstanceMethods
+ def project_data_seeders(project, key)
+ [::BimSeeder::DemoData::BcfXmlSeeder.new(project, key)] + super(project, key)
+ end
+ end
+end
diff --git a/modules/bim_seeder/lib/open_project/bim_seeder/patches/work_package_seeder_patch.rb b/modules/bim_seeder/lib/open_project/bim_seeder/patches/work_package_seeder_patch.rb
new file mode 100644
index 00000000000..2b97e62e0d5
--- /dev/null
+++ b/modules/bim_seeder/lib/open_project/bim_seeder/patches/work_package_seeder_patch.rb
@@ -0,0 +1,54 @@
+module OpenProject::BimSeeder::Patches::WorkPackageSeederPatch
+ def self.included(base) # :nodoc:
+ base.prepend InstanceMethods
+ end
+
+ module InstanceMethods
+ def create_or_update_work_package(attributes)
+ uuid = attributes[:bcf_issue_uuid]
+ if uuid
+ puts "Would do something with #{attributes[:bcf_issue_id]}"
+
+ start_date = calculate_start_date(attributes[:start])
+ due_date = calculate_due_date(start_date, attributes[:duration])
+
+
+
+ work_package = find_bcf_issue(uuid)
+
+ work_package.update_columns(created_at: Time.now,
+ author_id: user.id,
+ assigned_to_id: find_assignee_id(attributes[:assigned_to]),
+ start_date: start_date,
+ due_date: due_date)
+
+ update_parent(work_package, attributes)
+ else
+ create_work_package(attributes)
+ end
+ end
+
+ def update_parent(work_package, attributes)
+ if attributes[:parent]
+ parent = WorkPackage.find_by(subject: attributes[:parent])
+ if parent.present?
+ work_package.parent = parent
+ work_package.save!
+ end
+ end
+ end
+
+ def find_bcf_issue(uuid)
+ WorkPackage
+ .joins(:bcf_issue)
+ .where(project_id: project.id, 'bcf_issues.uuid': uuid)
+ .references(:bcf_issue).first
+ end
+
+ def find_assignee_id(name)
+ return nil unless name.present?
+
+ Principal.find_by(lastname: name).try(:id)
+ end
+ end
+end