-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[FND-209] Build service & job to generate project specific variants for form configs #24658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
351d5df
Implement service to generate a variant from a type + project custom …
klaustopher a36f0c0
Create Job to build project specific variants
klaustopher 5be84be
Add TODO about the move to a project specific variant
klaustopher 0fba536
Use the EmptyContract to also allow this on archived projects and rol…
klaustopher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
app/services/work_package_types/build_variant_from_project_service.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
114
app/workers/work_package_types/build_project_variants_job.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
| 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 | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
SwitchVariantServicevalidatesProjects::ManageTypesContract, ending up withunless project.active? || project.being_archived?inUserPermissibleService#allowed_in_single_project?.As the
BuildVariantFromProjectServiceandProjects::Types::SwitchVariantServiceare 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
ProjectTypequery to handle only active (and being archived) projects, leaving archived project without customized form configuration, can we?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually we pass the
EmptyContractin cases like this. I think this should also be the correct pattern here. Adding a spec and putting everything in one transaction