mirror of
https://github.com/rickbarrette/redmine_recursive_relations.git
synced 2026-11-17 14:30:36 -05:00
Cross-link only "related to", with permission and ID fixes
- Only cross-links "relates": directional types created invented
relations, and "precedes" rescheduled dates / "duplicates" closed
issues between the targets
- Requires permission to manage relations in both issues' projects
(cross-links are between issues the user isn't editing)
- Parses issue ids the way Redmine does ("#13", spaces), so "#" ids are
no longer dropped
- Uses the relations actually saved to the base issue instead of the
form-only @unsaved_relations, so it behaves the same for the form, a
plain post and the API
- Skips pairs already related, caps at 10 issues per request, and logs
failures instead of raising after the relations were saved
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
2 files changed
+73
-34
No files matched your search
@@ -4,8 +4,11 @@
|
||||
This plugin extends the default issue relation functionality in Redmine. When adding multiple issues as a relation via a comma-separated string (e.g., `12, 13, 15`), Redmine natively links the base issue to each of these targets. This plugin automatically extends this behavior by cross-relating all the newly provided issues with each other.
|
||||
|
||||
## Features
|
||||
- Automatically creates cross-relations for comma-separated issue IDs.
|
||||
- Gracefully handles validation failures (e.g., missing issues, cross-project restrictions).
|
||||
- Automatically creates cross-relations for comma-separated issue IDs (`12, #13, 15` all work).
|
||||
- Only for "Related to": directional types (blocks, precedes, duplicates, copied to) are never cross-linked, since that would invent relations and could reschedule or close issues.
|
||||
- Only cross-links issues Redmine actually related to the base issue, and only where you're allowed to manage relations in both issues' projects.
|
||||
- Cross-links at most 10 issues per request (45 relations), to avoid flooding issue history and notifications.
|
||||
- Gracefully handles validation failures (e.g., missing issues, cross-project restrictions); problems are logged, never shown as an error page.
|
||||
- Lightweight, using standard Rails `ActiveSupport::Concern` to patch the controller without overriding core files.
|
||||
|
||||
---
|
||||
|
||||
@@ -7,70 +7,106 @@
|
||||
#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.
|
||||
|
||||
module RedmineRecursiveRelations
|
||||
module Patches
|
||||
# When several issues are related to an issue at once ("12, #13, 15"), also relates those issues to
|
||||
# each other. Only for symmetric relation types (see CROSS_LINK_TYPES): cross-linking "blocks",
|
||||
# "precedes" or "duplicates" would invent relations nobody asked for, and "precedes" reschedules
|
||||
# dates and "duplicates" closes issues.
|
||||
module IssueRelationsControllerPatch
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# Relation types that are cross-linked. "relates" is the only symmetric type in core Redmine.
|
||||
CROSS_LINK_TYPES = %w[relates].freeze
|
||||
|
||||
# Most issues cross-linked from one request: n issues make n*(n-1)/2 relations, each with a
|
||||
# history entry on both issues (10 issues -> 45 relations)
|
||||
MAX_CROSS_LINKED_ISSUES = 10
|
||||
|
||||
included do
|
||||
# 1. Capture the form data before Redmine modifies it in the core 'create' method
|
||||
# Redmine's create overwrites params[:relation][:issue_to_id] while looping, so keep the original
|
||||
before_action :store_original_relation_params, only: [:create]
|
||||
|
||||
# 2. Run the cross-linking after Redmine has linked the base issue to the targets
|
||||
after_action :create_cross_relations, only: [:create]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def log(msg)
|
||||
Rails.logger.info "[IssueRelationsControllerPatch] #{msg}"
|
||||
Rails.logger.info "[RedmineRecursiveRelations] #{msg}"
|
||||
end
|
||||
|
||||
def store_original_relation_params
|
||||
log "store_original_relation_params"
|
||||
return unless params[:relation]
|
||||
relation = params[:relation]
|
||||
return unless relation.respond_to?(:[])
|
||||
|
||||
# relation_issues_to_id is a core method that parses strings like "12, 13, 15" into an array
|
||||
@original_target_ids = relation_issues_to_id.dup
|
||||
|
||||
# Save the params deeply so they aren't overwritten by the core looping mechanism
|
||||
@original_relation_params = params[:relation].to_unsafe_hash.deep_dup
|
||||
@original_issue_to_ids = relation[:issue_to_id].dup
|
||||
@original_relation_type = relation[:relation_type].presence || IssueRelation::TYPE_RELATES
|
||||
end
|
||||
|
||||
def create_cross_relations
|
||||
log "create_cross_relations"
|
||||
# Ensure we have data and at least 2 issues to cross-relate
|
||||
return if @original_target_ids.blank? || @original_relation_params.blank?
|
||||
return if @original_target_ids.size < 2
|
||||
return unless @issue && @original_issue_to_ids
|
||||
return unless CROSS_LINK_TYPES.include?(@original_relation_type)
|
||||
|
||||
successful_target_ids = @original_target_ids.map(&:to_i)
|
||||
requested_ids = parse_issue_ids(@original_issue_to_ids)
|
||||
return if requested_ids.size < 2
|
||||
|
||||
# Remove any issues that failed standard validation on the base issue
|
||||
# (e.g., they don't exist, or cross-project linking is disabled)
|
||||
if @unsaved_relations.present?
|
||||
failed_ids = @unsaved_relations.map(&:issue_to_id)
|
||||
successful_target_ids -= failed_ids
|
||||
# Only issues Redmine actually related to the base issue (works the same for the form,
|
||||
# a plain form post and the API)
|
||||
targets = Issue.visible.where(id: related_to_base(requested_ids)).order(:id).to_a
|
||||
return if targets.size < 2
|
||||
|
||||
if targets.size > MAX_CROSS_LINKED_ISSUES
|
||||
log "Not cross-linking #{targets.size} issues from ##{@issue.id}: more than #{MAX_CROSS_LINKED_ISSUES}"
|
||||
return
|
||||
end
|
||||
|
||||
# Fetch the valid issues for cross-linking
|
||||
valid_issues = Issue.visible(User.current).where(id: successful_target_ids).to_a
|
||||
cross_link(targets)
|
||||
rescue StandardError => e
|
||||
# The requested relations are already saved; don't turn this into an error page
|
||||
log "Cross-linking from ##{@issue&.id} failed: #{e.class}: #{e.message}"
|
||||
end
|
||||
|
||||
# Generate all unique pairs from the list (e.g., 12->13, 12->15, 13->15)
|
||||
valid_issues.combination(2).each do |issue_from, issue_to|
|
||||
relation = IssueRelation.new
|
||||
# Accepts what Redmine accepts: "12, #13, 15", an array of ids, or a single integer
|
||||
def parse_issue_ids(value)
|
||||
Array(value.is_a?(String) ? value.split(',') : value)
|
||||
.map { |v| v.to_s.strip[/\A#?(\d+)\z/, 1] }
|
||||
.compact.map(&:to_i).uniq
|
||||
end
|
||||
|
||||
def related_to_base(ids)
|
||||
IssueRelation.where(issue_from_id: @issue.id, issue_to_id: ids).pluck(:issue_to_id) +
|
||||
IssueRelation.where(issue_to_id: @issue.id, issue_from_id: ids).pluck(:issue_from_id)
|
||||
end
|
||||
|
||||
def cross_link(issues)
|
||||
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).each do |issue_from, issue_to|
|
||||
next if already_related.include?([issue_from.id, issue_to.id].minmax)
|
||||
|
||||
# Neither issue is the one the user is editing, so they need permission on both projects
|
||||
unless [issue_from, issue_to].all? { |i| User.current.allowed_to?(:manage_issue_relations, i.project) }
|
||||
log "Skipping ##{issue_from.id} - ##{issue_to.id}: no permission to manage relations there"
|
||||
next
|
||||
end
|
||||
|
||||
relation = IssueRelation.new(relation_type: @original_relation_type)
|
||||
relation.issue_from = issue_from
|
||||
relation.safe_attributes = @original_relation_params.merge('issue_to_id' => issue_to.id)
|
||||
relation.issue_to = issue_to
|
||||
relation.init_journals(User.current)
|
||||
|
||||
begin
|
||||
# Standard saves trigger core validations. If an issue creates a circular
|
||||
# dependency (e.g., with 'blocks'), Redmine will safely reject it here silently.
|
||||
relation.save
|
||||
next if relation.save
|
||||
|
||||
log "Could not relate ##{issue_from.id} - ##{issue_to.id}: #{relation.errors.full_messages.join(', ')}"
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
# Suppress errors if a duplicate relation somehow already exists in the database
|
||||
# Created at the same moment by someone else
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user