Make auto-assign safe: after commit, workflow-aware, with history

- Runs after the time entry is committed, on a fresh copy of the issue:
  a problem updating the issue no longer rolls back the logged time or
  interferes with the issue edit form saving the same issue
- Only assigns the time logger when they can be assigned in the project
- Only changes status when the workflow allows it for that user
  (checked after assigning, since assignee rules can allow more)
- Records the change in the issue history under the time logger, with
  a short note, so watchers see it
- Settings for the starting and target statuses; defaults are each
  tracker's default status and a status named "In Progress", so
  renaming "New" no longer breaks it
- Update failures are logged instead of raised; declares Redmine 6.1+

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
Claude committed 2026-10-28 23:32:20 -04:00
1 parent fd66d8c169
commit af3a769a6b
5 files changed
+100 -20

No files matched your search

@@ -0,0 +1,17 @@
<div class="box tabular">
<p>
<label for="settings_starting_status_id"><%= l(:label_auto_assign_starting_status) %></label>
<%= select_tag 'settings[starting_status_id]',
options_from_collection_for_select(IssueStatus.sorted, :id, :name, settings['starting_status_id'].to_i),
include_blank: l(:label_auto_assign_tracker_default), id: 'settings_starting_status_id' %>
<em class="info"><%= l(:text_auto_assign_starting_status) %></em>
</p>
<p>
<label for="settings_in_progress_status_id"><%= l(:label_auto_assign_in_progress_status) %></label>
<%= select_tag 'settings[in_progress_status_id]',
options_from_collection_for_select(IssueStatus.sorted, :id, :name, settings['in_progress_status_id'].to_i),
include_blank: l(:label_auto_assign_named_in_progress), id: 'settings_in_progress_status_id' %>
<em class="info"><%= l(:text_auto_assign_in_progress_status) %></em>
</p>
</div>
+8
View File
@@ -0,0 +1,8 @@
en:
label_auto_assign_starting_status: "Move issues from"
label_auto_assign_in_progress_status: "Move issues to"
label_auto_assign_tracker_default: "(each tracker's default status)"
label_auto_assign_named_in_progress: "(the status named In Progress)"
text_auto_assign_starting_status: "When time is logged on an issue at this status, it's moved on. Default: each tracker's default status, the one new issues start with."
text_auto_assign_in_progress_status: "The status issues move to when time is first logged. Only applied when the workflow allows the change for the person logging time."
text_auto_assign_journal_note: "Updated automatically when time was logged."
+5
View File
@@ -18,6 +18,11 @@ Redmine::Plugin.register :redmine_auto_assign do
version '2026.3.0'
url 'https://github.com/rickbarrette/redmine_auto_assign'
author_url 'https://barrettefabrication.com'
requires_redmine version_or_higher: '6.1.0'
# Blank settings: start from each tracker's default status, move to a status named "In Progress"
settings default: { 'starting_status_id' => '', 'in_progress_status_id' => '' },
partial: 'settings/auto_assign_settings'
end
AutoAssign.setup
+19
View File
@@ -12,4 +12,23 @@ module AutoAssign
def self.setup
TimeEntry.prepend AutoAssign::Patches::TimeEntryPatch
end
def self.settings
Setting.plugin_redmine_auto_assign || {}
rescue StandardError
{}
end
# The status an issue must be at to be moved: the one chosen in settings, else the tracker's default
# status (what new issues start as), so renaming "New" doesn't break it
def self.starting_status_for(issue)
chosen = settings['starting_status_id'].to_i
(IssueStatus.find_by(id: chosen) if chosen.positive?) || issue.tracker&.default_status
end
# The status to move to: the one chosen in settings, else a status named "In Progress"
def self.in_progress_status
chosen = settings['in_progress_status_id'].to_i
(IssueStatus.find_by(id: chosen) if chosen.positive?) || IssueStatus.find_by(name: 'In Progress')
end
end
+51 -20
View File
@@ -10,45 +10,76 @@
module AutoAssign
module Patches
# When someone logs time on an issue:
# - if nobody is assigned, assign the issue to them (if they can be assigned in that project)
# - if the issue is still at its starting status, move it to the "in progress" status
# (only when the workflow allows that change for them)
# The change is recorded in the issue's history under the person who logged the time.
module TimeEntryPatch
extend ActiveSupport::Concern
prepended do
after_create :assign_issue
# After the time entry is committed, so a problem updating the issue can never undo the
# logged time, and so we never touch the issue object an issue edit form is still saving.
after_commit :assign_issue, on: :create
end
def assign_issue
return unless issue
return unless issue_id && user
log "assign_issue triggered for TimeEntry ##{id}"
# A fresh copy, not the (possibly half-edited) issue this time entry was built with
target = Issue.find_by(id: issue_id)
return unless target
# Only assign if nobody is assigned yet
if issue.assigned_to.blank?
issue.assigned_to = user
log "Assigned Issue ##{issue.id} to #{user.id}"
target.init_journal(user, ::I18n.t(:text_auto_assign_journal_note))
assign_to_time_logger(target)
move_to_in_progress(target)
unless target.changed?
log "No changes needed for Issue ##{target.id}"
return
end
# Only move status if still "New"
if issue.status&.name == "New"
in_progress = IssueStatus.find_by(name: "In Progress")
if in_progress
issue.status = in_progress
log "Moved Issue ##{issue.id} to In Progress"
end
end
if issue.changed?
issue.save!
if target.save
log "Updated Issue ##{target.id} after time entry ##{id}"
else
log "No changes needed for Issue ##{issue.id}"
log "Could not update Issue ##{target.id}: #{target.errors.full_messages.join(', ')}"
end
rescue StandardError => e
# The time entry is already saved; an error page here would wrongly suggest it wasn't
log "Could not update Issue ##{issue_id}: #{e.class}: #{e.message}"
end
private
def assign_to_time_logger(target)
return if target.assigned_to.present?
if target.assignable_users.include?(user)
target.assigned_to = user
log "Assigning Issue ##{target.id} to user ##{user.id}"
else
log "Not assigning Issue ##{target.id}: user ##{user.id} can't be assigned in this project"
end
end
def move_to_in_progress(target)
from = AutoAssign.starting_status_for(target)
to = AutoAssign.in_progress_status
return unless from && to && target.status == from && from != to
# Checked after assigning, since workflows can allow more transitions for the assignee
if target.new_statuses_allowed_to(user).include?(to)
target.status = to
log "Moving Issue ##{target.id} to #{to.name}"
else
log "Not moving Issue ##{target.id} to #{to.name}: the workflow doesn't allow it for user ##{user.id}"
end
end
def log(msg)
Rails.logger.info "[AutoAssign] #{msg}"
end
end
end
end
end