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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Add seeder for design
Oliver Günther2d4f3703e8f2
diff --git a/app/models/custom_style.rb b/app/models/custom_style.rbindex 8e0603b2e17..6155a03d988 100644--- a/app/models/custom_style.rb+++ b/app/models/custom_style.rb@@ -35,7 +35,11 @@ class CustomStyle < ApplicationRecordimage = send(name)image&.remove!- update_columns(name => nil, updated_at: Time.zone.now)+ if new_record?+ send(:"#{name}=", nil)+ else+ update_columns(name => nil, updated_at: Time.zone.now)+ endendendenddiff --git a/app/seeders/env_data/custom_design_seeder.rb b/app/seeders/env_data/custom_design_seeder.rbnew file mode 100644index 00000000000..339be90e759--- /dev/null+++ b/app/seeders/env_data/custom_design_seeder.rb@@ -0,0 +1,118 @@+# 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 EnvData+ class CustomDesignSeeder < Seeder+ def seed_data!+ custom_style = CustomStyle.current || CustomStyle.new++ print_status " ↳ Setting custom design colors" do+ seed_colors+ seed_export_color(custom_style)+ end++ print_status " ↳ Setting custom logos" do+ seed_logos(custom_style)+ end++ custom_style.save!+ end++ def applicable?+ Setting.seed_design.present?+ end++ private++ def seed_logos(custom_style)+ CustomStyle.uploaders.each_key do |key|+ data = Setting.seed_design[key.to_s]++ if data.blank?+ custom_style.public_send(:"remove_#{key}")+ elsif data.match?(/^https?:\/\//)+ custom_style.public_send(:"remote_#{key}_url=", data)+ else+ io = Base64StringIO.new(data, key.to_s)+ custom_style.public_send(:"#{key}=", io)+ end+ end+ end++ def seed_colors+ OpenProject::CustomStyles::Design.customizable_variables.each do |variable|+ key = variable.to_s.underscore+ value = Setting.seed_design[key]++ if value.blank?+ DesignColor.where(variable:).delete_all+ else+ design_color = DesignColor.find_or_initialize_by(variable:)+ design_color.hexcode = value+ design_color.save!+ end+ end+ end++ def seed_export_color(custom_style)+ value = Setting.seed_design["export_cover_text_color"]+ custom_style.export_cover_text_color = value.presence+ end+ end++ class Base64StringIO < StringIO+ attr_reader :filename++ def initialize(data_url, base_name)+ metadata, encoded = data_url.split(",")++ if metadata.blank? || encoded.blank? || !metadata.start_with?("data:")+ raise ArgumentError, "Expected data URL, got #{data_url}"+ end++ @filename = "#{base_name}.#{extension(metadata)}"+ bytes = ::Base64.strict_decode64(encoded)++ super(bytes)+ end++ def original_filename+ filename+ end++ def extension(metadata)+ content_type = metadata.match(%r{data:([^;]+)})&.captures&.first+ raise ArgumentError, "Failed to parse content type from metadata: #{metadata}" if content_type.nil?++ mime_type = MIME::Types[content_type].last+ raise ArgumentError, "Unknown mime type: #{content_type}" if mime_type.nil?++ mime_type.preferred_extension+ end+ end+enddiff --git a/app/seeders/env_data_seeder.rb b/app/seeders/env_data_seeder.rbindex bbda615cb62..e446cb1fd80 100644--- a/app/seeders/env_data_seeder.rb+++ b/app/seeders/env_data_seeder.rb@@ -27,7 +27,8 @@class EnvDataSeeder < CompositeSeederdef data_seeder_classes[- EnvData::LdapSeeder+ EnvData::LdapSeeder,+ EnvData::CustomDesignSeeder]enddiff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rbindex 8c7c30a1bd6..25ca799f88d 100644--- a/config/constants/settings/definition.rb+++ b/config/constants/settings/definition.rb@@ -956,6 +956,13 @@ module Settingsformat: :hash,string_values: true},+ seed_design: {+ description: "Seed enterprise-edition theme colors and logos through ENV",+ writable: false,+ default: nil,+ format: :hash,+ string_values: true+ },self_registration: {default: 2},diff --git a/spec/seeders/env_data/custom_design_seeder_spec.rb b/spec/seeders/env_data/custom_design_seeder_spec.rbnew file mode 100644index 00000000000..b57aaed1f74--- /dev/null+++ b/spec/seeders/env_data/custom_design_seeder_spec.rb@@ -0,0 +1,194 @@+# 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.+#++++require "spec_helper"++RSpec.describe EnvData::CustomDesignSeeder, :webmock do+ let(:seed_data) { Source::SeedData.new({}) }+ let(:base64_image) do+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wQACfsD/QqnFgAAAABJRU5ErkJggg=="+ end+ let(:base64_data_url) do+ "data:image/png;base64,#{base64_image}"+ end+ let(:stub) do+ stub_request(:get, "http://test.foobar.com/image.png")+ .to_return(+ status: 200,+ body: Rails.root.join("spec/fixtures/files/image.png").read+ )+ end++ subject(:seeder) { described_class.new(seed_data) }++ before do+ stub+ end++ context "when not provided" do+ it "does nothing" do+ seeder.seed!++ expect(DesignColor.all).to be_empty+ expect(CustomStyle.all).to be_empty+ end+ end++ # rubocop:disable Layout/LineLength+ context "when providing seed variables",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_PRIMARY__BUTTON__COLOR: "#571EFA",+ OPENPROJECT_SEED_DESIGN_ACCENT__COLOR: "#571EFA",+ OPENPROJECT_SEED_DESIGN_HEADER__BG__COLOR: "#FFFFFF",+ OPENPROJECT_SEED_DESIGN_HEADER__ITEM__BG__HOVER__COLOR: "#E5E5E5",+ OPENPROJECT_SEED_DESIGN_MAIN__MENU__BG__COLOR: "#FFFFFF",+ OPENPROJECT_SEED_DESIGN_MAIN__MENU__BG__SELECTED__BACKGROUND: "#571EFA",+ OPENPROJECT_SEED_DESIGN_MAIN__MENU__BG__HOVER__BACKGROUND: "#E5E5E5",+ OPENPROJECT_SEED_DESIGN_TOUCH__ICON: "http://test.foobar.com/image.png",+ OPENPROJECT_SEED_DESIGN_LOGO: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wQACfsD/QqnFgAAAABJRU5ErkJggg=="+ } do+ it "uses those variables" do+ reset(:seed_design)++ seeder.seed!++ expect(DesignColor.find_by(variable: "primary-button-color").hexcode).to eq("#571EFA")+ expect(DesignColor.find_by(variable: "accent-color").hexcode).to eq("#571EFA")+ expect(DesignColor.find_by(variable: "header-bg-color").hexcode).to eq("#FFFFFF")+ expect(DesignColor.find_by(variable: "header-item-bg-hover-color").hexcode).to eq("#E5E5E5")+ expect(DesignColor.find_by(variable: "main-menu-bg-color").hexcode).to eq("#FFFFFF")+ expect(DesignColor.find_by(variable: "main-menu-bg-selected-background").hexcode).to eq("#571EFA")+ expect(DesignColor.find_by(variable: "main-menu-bg-hover-background").hexcode).to eq("#E5E5E5")+ end+ end++ context "when removing logo",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_LOGO: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wQACfsD/QqnFgAAAABJRU5ErkJggg=="+ } do+ it "uses those variables" do+ reset(:seed_design)++ custom_style = CustomStyle.new+ custom_style.favicon = File.open Rails.root.join("spec/fixtures/files/image.png").to_s+ custom_style.save!++ seeder.seed!++ custom_style.reload++ expect(custom_style.favicon.file).to be_nil+ expect(custom_style.logo.file).to be_present+ end+ end++ context "when providing base64 without url",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_LOGO: "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wQACfsD/QqnFgAAAABJRU5ErkJggg=="+ } do+ it "uses those variables" do+ reset(:seed_design)++ expect { seeder.seed! }.to raise_error /Expected data URL/+ end+ end+ # rubocop:enable Layout/LineLength++ context "when providing invalid color",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_PRIMARY__BUTTON__COLOR: "invalid!"+ } do+ it "uses those variables" do+ reset(:seed_design)++ expect { seeder.seed! }.to raise_error /Hexcode is invalid/+ end+ end++ context "when providing export color",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_EXPORT__COVER__TEXT__COLOR: "#444444"+ } do+ it "uses those variables" do+ reset(:seed_design)++ seeder.seed!++ custom_style = CustomStyle.current+ expect(custom_style.export_cover_text_color).to eq "#444444"+ end+ end++ context "when not providing export color",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_PRIMARY__BUTTON__COLOR: "#571EFA"+ } do+ it "unsets that color" do+ reset(:seed_design)++ seeder.seed!++ custom_style = CustomStyle.current+ expect(custom_style.export_cover_text_color).to be_nil+ end+ end++ context "when providing not all colors",+ :settings_reset,+ with_env: {+ OPENPROJECT_SEED_DESIGN_PRIMARY__BUTTON__COLOR: "#571EFA",+ OPENPROJECT_SEED_DESIGN_ACCENT__COLOR: "#571EFA"+ } do+ it "uses those variables" do+ reset(:seed_design)++ DesignColor.create!(variable: "primary-button-color", hexcode: "#571EFA")+ DesignColor.create!(variable: "accent-color", hexcode: "#571EFA")+ DesignColor.create!(variable: "header-bg-color", hexcode: "#FFFFFF")+ DesignColor.create!(variable: "header-item-bg-hover-color", hexcode: "#E5E5E5")+ DesignColor.create!(variable: "main-menu-bg-color", hexcode: "#FFFFFF")+ DesignColor.create!(variable: "main-menu-bg-selected-background", hexcode: "#571EFA")+ DesignColor.create!(variable: "main-menu-bg-hover-background", hexcode: "#E5E5E5")+ expect(DesignColor.count).to eq(7)++ seeder.seed!++ expect(DesignColor.count).to eq(2)+ expect(DesignColor.find_by(variable: "primary-button-color").hexcode).to eq("#571EFA")+ expect(DesignColor.find_by(variable: "accent-color").hexcode).to eq("#571EFA")+ end+ end+end