diff --git a/app/models/work_package/exports/formatters/id.rb b/app/models/work_package/exports/formatters/id.rb new file mode 100644 index 00000000000..ae35619272e --- /dev/null +++ b/app/models/work_package/exports/formatters/id.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) the OpenProject GmbH +# +# 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-2013 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 COPYRIGHT and LICENSE files for more details. +#++ + +module WorkPackage::Exports + module Formatters + # Exports the user-facing work package identifier: the semantic + # identifier (e.g. "PROJ-42") in semantic mode, the numeric database + # ID in classic mode. + class Id < ::Exports::Formatters::Default + def self.apply?(attribute, export_format) + attribute.to_sym == :id && export_format == :csv + end + + protected + + def retrieve_value(object) + object.display_id + end + end + end +end diff --git a/config/initializers/export_formats.rb b/config/initializers/export_formats.rb index 20a50c2cd6d..f0f1db2e6d7 100644 --- a/config/initializers/export_formats.rb +++ b/config/initializers/export_formats.rb @@ -47,6 +47,7 @@ Rails.application.configure do |application| formatter WorkPackage, WorkPackage::Exports::Formatters::PDF::Days formatter WorkPackage, WorkPackage::Exports::Formatters::XLS::DoneRatio formatter WorkPackage, WorkPackage::Exports::Formatters::PDF::Hours + formatter WorkPackage, WorkPackage::Exports::Formatters::Id formatter WorkPackage, WorkPackage::Exports::Formatters::ProjectPhase formatter WorkPackage, WorkPackage::Exports::Formatters::SpentUnits diff --git a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb index adb53e70255..ce6f54cfb64 100644 --- a/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb +++ b/modules/xls_export/spec/models/xls_export/work_package/exporter/xls_integration_spec.rb @@ -246,6 +246,25 @@ RSpec.describe XlsExport::WorkPackage::Exporter::XLS do end end + context "with semantic work package identifiers", + with_settings: { work_packages_identifier: "semantic" } do + let(:project) { create(:project, identifier: "XLSPROJ") } + let(:work_package) do + create(:work_package, + project:, + type: project.types.first) + end + let(:work_packages) { [work_package] } + let(:column_names) { %w[id subject] } + + it "exports the semantic identifier in the ID column" do + expect(sheet.rows.size).to eq(1 + 1) + + expect(work_package.identifier).to match(/\AXLSPROJ-\d+\z/) + expect(sheet.rows[1][0]).to eq work_package.identifier + end + end + context "with underscore in subject" do let(:work_package) do create(:work_package, diff --git a/spec/models/work_package/exporter/csv_integration_spec.rb b/spec/models/work_package/exporter/csv_integration_spec.rb index f0aee2b6bd7..ed3163301c7 100644 --- a/spec/models/work_package/exporter/csv_integration_spec.rb +++ b/spec/models/work_package/exporter/csv_integration_spec.rb @@ -134,6 +134,30 @@ RSpec.describe WorkPackage::Exports::CSV, "integration" do end end + context "with semantic work package identifiers", + with_settings: { work_packages_identifier: "semantic" } do + let(:semantic_project) do + create(:project, + identifier: "CSVPROJ", + member_with_permissions: { user => %i[view_work_packages] }) + end + let!(:work_package) { create(:work_package, project: semantic_project) } + let(:options) { {} } + let(:query) do + create(:query, project: semantic_project, user:, column_names: %i(id subject)) + end + + it "exports the semantic identifier in the ID column" do + headers, values = CSV.parse instance.export!.content + pairs = headers.zip(values).to_h + + expect(work_package.identifier).to match(/\ACSVPROJ-\d+\z/) + # the leading ID header is downcased by the exporter to avoid SYLK detection + expect(pairs["#{byte_order_mark}id"]).to eq work_package.identifier + expect(pairs["Subject"]).to eq work_package.subject + end + end + context "with multiple work packages" do shared_let(:wp1) { create(:work_package, project:, done_ratio: 25, subject: "WP1", type: type_a, id: 1) } shared_let(:wp2) { create(:work_package, project:, done_ratio: 0, subject: "WP2", type: type_a, id: 2) }