From 37d98dbe3d5268527f1f543b43fa669029d49ae5 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 28 Oct 2026 20:59:18 -0400 Subject: [PATCH] Relate all issues created from the same estimate, across batches - New issues from the estimate preview are now related to each other and to every issue created earlier from the same estimate; older batches that weren't related to each other are linked too - Pairs that already have any relation are skipped - New rake task redmine_qbo_calendar:relate_estimate_issues relates issues created before this change (safe to re-run) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- config/locales/en.yml | 4 +- .../estimate_issue_builder.rb | 49 ++++++++++++++----- lib/tasks/redmine_qbo_calendar.rake | 24 +++++++++ 3 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 lib/tasks/redmine_qbo_calendar.rake diff --git a/config/locales/en.yml b/config/locales/en.yml index 5d24fb0..e31ca5f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -104,7 +104,7 @@ en: permission_view_customer_appointments: "View customer appointments" text_default_tracker_info: "Pre-selected for each proposed issue. Leave blank to use the project's first tracker." text_estimate_issue_fields_info: "Entered once and applied to every created issue whose tracker uses the field. Fields marked * are required by the selected trackers." - text_estimate_issues_hint: "The subject comes from the estimate line's description and estimated hours from its quantity. Edit anything before creating. Issues created together are related to each other." - text_estimate_issues_template_hint: "The subject comes from the estimate line's description and estimated hours from its quantity. A template supplies the issue's description. Issues created together are related to each other." + text_estimate_issues_hint: "The subject comes from the estimate line's description and estimated hours from its quantity. Edit anything before creating. New issues are related to each other and to issues already created from this estimate." + text_estimate_issues_template_hint: "The subject comes from the estimate line's description and estimated hours from its quantity. A template supplies the issue's description. New issues are related to each other and to issues already created from this estimate." text_estimate_item_codes_info: "QBO item names that can become issues, one per line (e.g. Labor, Alignments). Matches the full item name or any part of a Category:Item name, ignoring case." title_customer_appointments: Customer Appointments \ No newline at end of file diff --git a/lib/redmine_qbo_calendar/estimate_issue_builder.rb b/lib/redmine_qbo_calendar/estimate_issue_builder.rb index d647dff..756f6ed 100644 --- a/lib/redmine_qbo_calendar/estimate_issue_builder.rb +++ b/lib/redmine_qbo_calendar/estimate_issue_builder.rb @@ -16,7 +16,7 @@ module RedmineQboCalendar # - Subject = the line's description; estimated hours = the line's quantity # - Lines already turned into issues are shown but can't be created again # - Creation is all-or-nothing: if any issue fails validation, none are saved - # - Issues created together are all related to each other + # - All issues created from the same estimate are related to each other, across batches class EstimateIssueBuilder include Redmine::I18n @@ -53,6 +53,34 @@ module RedmineQboCalendar codes.map { |c| c.strip.downcase }.any? { |code| name == code || parts.include?(code) } end + # Relates every issue in the list to every other one ("Related to"), skipping pairs that already + # have any relation. Used for issues created from the same estimate, whichever batch they came from. + # Issues loaded fresh have no open journal, so this adds no history entries and sends no + # "issue updated" emails. A failed relation (e.g. cross-project relations disabled) is logged, not fatal. + # Returns the number of relations created. + def self.relate_all(issues) + issues = issues.compact.uniq(&:id) + return 0 if issues.size < 2 + + ids = issues.map(&:id) + already_related = IssueRelation.where(issue_from_id: ids, issue_to_id: ids) + .pluck(:issue_from_id, :issue_to_id) + .map(&:minmax) + .to_set + + issues.combination(2).count do |issue_from, issue_to| + next false if already_related.include?([issue_from.id, issue_to.id].minmax) + + relation = IssueRelation.new(issue_from: issue_from, issue_to: issue_to, + relation_type: IssueRelation::TYPE_RELATES) + next true if relation.save + + Rails.logger.warn "[EstimateIssueBuilder] Could not relate ##{issue_from.id} and ##{issue_to.id}: " \ + "#{relation.errors.full_messages.join(', ')}" + false + end + end + attr_reader :appointment, :errors def initialize(appointment) @@ -157,18 +185,13 @@ module RedmineQboCalendar key.to_s end - # Relates every issue in the batch to every other one ("Related to"), since they're work on the - # same estimate done at the same time. New issues have no open journal, so this adds no history - # entries and sends no "issue updated" emails. A failed relation is logged, not fatal. - def relate_issues(issues) - issues.combination(2).each do |issue_from, issue_to| - relation = IssueRelation.new(issue_from: issue_from, issue_to: issue_to, - relation_type: IssueRelation::TYPE_RELATES) - next if relation.save - - Rails.logger.warn "[EstimateIssueBuilder] Could not relate ##{issue_from.id} and ##{issue_to.id}: " \ - "#{relation.errors.full_messages.join(', ')}" - end + # Relates the new issues to each other and to every issue created earlier from the same estimate. + def relate_issues(new_issues) + earlier = AppointmentEstimateIssue.where(estimate_id: estimate.id) + .where.not(issue_id: new_issues.map(&:id)) + .includes(:issue) + .filter_map(&:issue) + self.class.relate_all(earlier + new_issues) end def build_issue(proposal, row, project, author, allowed_trackers, custom_field_values) diff --git a/lib/tasks/redmine_qbo_calendar.rake b/lib/tasks/redmine_qbo_calendar.rake new file mode 100644 index 0000000..141f956 --- /dev/null +++ b/lib/tasks/redmine_qbo_calendar.rake @@ -0,0 +1,24 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +namespace :redmine_qbo_calendar do + desc 'Relate all issues created from the same QBO estimate to each other (one-time catch-up; safe to re-run)' + task relate_estimate_issues: :environment do + total = 0 + estimates = AppointmentEstimateIssue.distinct.pluck(:estimate_id) + + estimates.each do |estimate_id| + issues = AppointmentEstimateIssue.where(estimate_id: estimate_id).includes(:issue).filter_map(&:issue) + total += RedmineQboCalendar::EstimateIssueBuilder.relate_all(issues) + end + + puts "Checked #{estimates.size} estimate(s), created #{total} relation(s)." + end +end