|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +module ProjectIdentifiers |
| 32 | + # Brings a single project fully up to date for semantic identifier mode: |
| 33 | + # |
| 34 | + # 1. Fixes the project identifier if it is not in valid semantic format. |
| 35 | + # 2. Rewrites stale WP identifiers whose prefix no longer matches the project. |
| 36 | + # 3. Assigns sequence numbers to WPs that have none yet. |
| 37 | + # 4. Seeds the alias table for all historical project identifier prefixes. |
| 38 | + class ConvertProjectToSemanticService |
| 39 | + def initialize(project) |
| 40 | + @project = project |
| 41 | + end |
| 42 | + |
| 43 | + def call |
| 44 | + ApplicationRecord.transaction do |
| 45 | + fix_identifier_if_needed |
| 46 | + reset_stale_identifiers |
| 47 | + backfill_missing_ids |
| 48 | + seed_alias_table |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + private |
| 53 | + |
| 54 | + attr_reader :project |
| 55 | + |
| 56 | + def fix_identifier_if_needed |
| 57 | + # Pure format check — no DB queries. |
| 58 | + return if ProjectIdentifiers::IdentifierAutofix::ProblematicIdentifiers.valid_format?(project.identifier) |
| 59 | + |
| 60 | + # Serialize all concurrent identifier assignments with a transaction-level |
| 61 | + # advisory lock. The lock is automatically released when the outer |
| 62 | + # ApplicationRecord.transaction commits, so the next job waiting on it |
| 63 | + # always reads a fully up-to-date exclusion set and can never generate a |
| 64 | + # duplicate. Without this, parallel jobs can read the same exclusion set |
| 65 | + # before any of them commits, then all pick the same candidate. |
| 66 | + OpenProject::Mutex.with_advisory_lock( |
| 67 | + Project, "semantic_identifier_generation", transaction: true |
| 68 | + ) do |
| 69 | + assign_semantic_identifier |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + def assign_semantic_identifier |
| 74 | + # Re-instantiate inside the lock so the exclusion set reflects all |
| 75 | + # identifiers committed since this job started. |
| 76 | + detector = ProjectIdentifiers::IdentifierAutofix::ProblematicIdentifiers.new |
| 77 | + generator = ProjectIdentifiers::IdentifierAutofix::ProjectIdentifierSuggestionGenerator |
| 78 | + |
| 79 | + # Prefer restoring the project's last known semantic identifier (from |
| 80 | + # FriendlyId history) so that existing WP identifiers remain valid and |
| 81 | + # aliases need no update. Fall back to generating a fresh suggestion. |
| 82 | + new_identifier = project.previous_semantic_identifier || |
| 83 | + generator.suggest_identifier(project.name, exclude: detector.exclusion_set) |
| 84 | + |
| 85 | + raise "Generated identifier is blank for project #{project.id}" if new_identifier.blank? |
| 86 | + |
| 87 | + project.identifier = new_identifier |
| 88 | + # Bypass validation, because we're technically still in classic mode, so the model would be applying |
| 89 | + # validation for classic identifiers. |
| 90 | + project.save!(validate: false) |
| 91 | + end |
| 92 | + |
| 93 | + def reset_stale_identifiers |
| 94 | + # Fix WPs whose identifier does not exactly match the expected semantic identifier |
| 95 | + # (caused by renames or cross-project moves in classic mode) |
| 96 | + WorkPackage.where(project:).non_semantic_of(project).update_all(identifier: nil, sequence_number: nil) |
| 97 | + end |
| 98 | + |
| 99 | + def backfill_missing_ids |
| 100 | + WorkPackage.where(project:).unsequenced.find_each do |wp| |
| 101 | + seq, identifier = project.allocate_wp_semantic_identifier! |
| 102 | + wp.update_columns(sequence_number: seq, identifier:) |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + def seed_alias_table |
| 107 | + slug_prefixes = project.slugs.pluck(:slug) |
| 108 | + return if slug_prefixes.empty? |
| 109 | + |
| 110 | + WorkPackage.where(project:).semantically_sequenced.in_batches do |batch| |
| 111 | + alias_rows = batch.pluck(:id, :sequence_number) |
| 112 | + .product(slug_prefixes) |
| 113 | + .map { |(wp_id, seq), prefix| { identifier: "#{prefix}-#{seq}", work_package_id: wp_id } } |
| 114 | + WorkPackageSemanticAlias.insert_all(alias_rows, unique_by: :identifier) if alias_rows.any? |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments