diff --git a/app/views/settings/_auto_assign_settings.html.erb b/app/views/settings/_auto_assign_settings.html.erb new file mode 100644 index 0000000..ac52f11 --- /dev/null +++ b/app/views/settings/_auto_assign_settings.html.erb @@ -0,0 +1,17 @@ +
+

+ + <%= 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' %> + <%= l(:text_auto_assign_starting_status) %> +

+ +

+ + <%= 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' %> + <%= l(:text_auto_assign_in_progress_status) %> +

+
diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000..13a7afc --- /dev/null +++ b/config/locales/en.yml @@ -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." diff --git a/init.rb b/init.rb index f107b51..e18b4d0 100644 --- a/init.rb +++ b/init.rb @@ -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 \ No newline at end of file diff --git a/lib/auto_assign.rb b/lib/auto_assign.rb index 43d1921..c60c18c 100644 --- a/lib/auto_assign.rb +++ b/lib/auto_assign.rb @@ -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 diff --git a/lib/auto_assign/patches/time_entry_patch.rb b/lib/auto_assign/patches/time_entry_patch.rb index 5cd219f..4a7edc1 100644 --- a/lib/auto_assign/patches/time_entry_patch.rb +++ b/lib/auto_assign/patches/time_entry_patch.rb @@ -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 \ No newline at end of file +end