Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions app/services/work_package_types/build_variant_from_project_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# 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 WorkPackageTypes
# Turns a project's per-project custom field deactivations into a variant, so the narrowing a
# project used to express by disabling single fields becomes part of the form configuration
# instead.
#
# The variant inherits every aspect from the given type and excludes only the custom fields the
# project has not enabled. A project that narrows nothing needs no variant, so the given type is
# returned unchanged — callers can assign the result to the project either way.
class BuildVariantFromProjectService < ::BaseServices::BaseCallable
def initialize(user:, type:)
super()
@user = user
@source = type
end

protected

def perform(*)
project = params[:project]

elements = elements_to_exclude(project)
return ServiceResult.success(result: source) if elements.empty?

build_variant(project, elements)
end

private

attr_reader :source, :user

def build_variant(project, elements)
result = nil

Type.transaction do
result = create_variant(project)
raise ActiveRecord::Rollback if result.failure?

variant = result.result
link_aspects_to_source(variant)

exclusion = exclude_elements(variant, elements)
if exclusion.failure?
result = exclusion
raise ActiveRecord::Rollback
end
end

result
end

def create_variant(project)
# TODO: When FND-204 is fully implemented and we can create project specific variants, let's ensure that
# we create a project specific variant here.
CreateService
.new(user:)
.call(name: variant_name(project), parent_id: source.root_id)
end

# A variant is always a child of a root, so a variant built from another variant cannot nest
# under it. The configuration links are what carry the relationship: pointing them at the
# source variant makes its own exclusions accumulate with the ones added below.
def link_aspects_to_source(variant)
return unless source.variant?

Type::ConfigurationLink::ASPECTS.each { |aspect| variant.link!(aspect, source:) }
end

def exclude_elements(variant, elements)
ExcludedElements::AddService
.new(user:, type: variant)
.call(aspect: Type::ConfigurationLink::FORM_CONFIGURATION, elements:)
end

# `source.custom_fields` is the type-level set the form configuration puts on a work package,
# already resolved through the source's own links and exclusions. Whatever of it the project
# has not enabled is exactly what disabling single fields used to hide.
def elements_to_exclude(project)
active_ids = project.all_work_package_custom_fields.pluck(:id)

source.custom_fields
.reject { active_ids.include?(it.id) }
.map(&:attribute_name)
end

# Name of a variant must stay unique per root type
def variant_name(project)
base = "#{source.own_name} - #{project.name}"
taken = Type.where(parent_id: source.root_id).pluck(:name).map(&:downcase)

return base unless taken.include?(base.downcase)

counter = 2
counter += 1 while taken.include?("#{base} (#{counter})".downcase)
"#{base} (#{counter})"
end
end
end
114 changes: 114 additions & 0 deletions app/workers/work_package_types/build_project_variants_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 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 WorkPackageTypes
# Moves every project's per-project custom field deactivations into the form configuration, by
# building a variant per project that narrows anything and resolving the project to it.
#
# Safe to re-run: once a project resolves to its variant, that variant already excludes what the
# project disabled, so BuildVariantFromProjectService hands the variant straight back and nothing
# further happens.
#
# A project that fails is logged and skipped rather than aborting the run, so one broken project
# cannot hold back every project after it.
class BuildProjectVariantsJob < ApplicationJob
include GoodJob::ActiveJobExtensions::Concurrency

good_job_control_concurrency_with(total_limit: 1)

def perform
unless OpenProject::FeatureDecisions.type_variants_active?
raise "expected the type_variants feature to be active"
end

@built = 0
@unchanged = 0
@failed = 0

User.system.run_given do |user|
ProjectType.includes(:project, :type, :variant).find_each do |project_type|
build_variant_for(project_type, user)
end
end

log_summary
end

private

def build_variant_for(project_type, user)
project = project_type.project
type = project_type.effective_type

ApplicationRecord.transaction do
result = BuildVariantFromProjectService.new(user:, type:).call(project:)
rollback(project, type, result) if result.failure?

# The service returns the type it was given when the project narrows nothing, which is the
# signal that no variant is needed here.
next @unchanged += 1 if result.result == type

resolve(project, type, result.result, user)
end
end

def resolve(project, type, variant, user)
result = Projects::Types::SwitchVariantService
.new(user:, model: project, contract_class: EmptyContract)
.call(source: type, target: variant)
Comment on lines +83 to +85

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail on every archived project: SwitchVariantService validates Projects::ManageTypesContract, ending up with unless project.active? || project.being_archived? in UserPermissibleService#allowed_in_single_project?.

As the BuildVariantFromProjectService and Projects::Types::SwitchVariantService are not in the same transaction, the failure of the latter will leave the creation of the former orphan.

I don’t know how we should handle this case. We cannot narrow the ProjectType query to handle only active (and being archived) projects, leaving archived project without customized form configuration, can we?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we pass the EmptyContract in cases like this. I think this should also be the correct pattern here. Adding a spec and putting everything in one transaction


rollback(project, type, result) if result.failure?

@built += 1
end

def rollback(project, type, result)
log_failure(project, type, result)

raise ActiveRecord::Rollback
end

def log_failure(project, type, result)
@failed += 1

Rails.logger.error do
"[#{self.class.name}] Skipped #{type.composite_name} in project #{project.identifier}: " \
"#{result.errors.full_messages.join(', ')}"
end
end

def log_summary
Rails.logger.info do
"[#{self.class.name}] Built #{@built} variant(s), left #{@unchanged} project/type pair(s) " \
"unchanged, skipped #{@failed} after failures."
end
end
end
end
Loading
Loading