Skip to content

Commit

Permalink
Added rake task to reopen issues with open subtasks
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvijge committed Feb 16, 2016
1 parent 2747ec5 commit 43a6797
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 39 deletions.
43 changes: 29 additions & 14 deletions app/models/issue_status_changer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,52 @@ def self.get_closed_status
IssueStatus.select( :id ).where("is_closed=1").collect {|s| s.id}.join(",")
end

def self.get_enabled_trackers
def self.get_enabled_trackers(state = 'open')
state = 'close' if (state != 'close' and state != 'open')
settings = Setting['plugin_redmine_issue_status_changer'] || {}
if settings['status_change'] != nil then
settings['status_change'].keys.join(',')
if settings['status_change'][state] != nil then
settings['status_change'][state].keys.join(',')
else
false
end
end

def self.get_next_state(tracker_id)
def self.get_next_state(tracker_id, state = 'close')
state = 'close' if (state != 'close' and state != 'open')
settings = Setting['plugin_redmine_issue_status_changer'] || {}
settings['new_status'][tracker_id.to_s].to_i
settings['new_status'][state][tracker_id.to_s].to_i
end


def self.close_issues_with_all_subtasks_closed
issue_change_state('close')
end

def self.open_issues_with_open_subtasks
issue_change_state('open')
end

def self.issue_change_state(state)
settings = Setting['plugin_redmine_issue_status_changer'] || {}

status_message = settings['status_message']


enabled_trackers = get_enabled_trackers()
enabled_trackers = get_enabled_trackers(state)
if enabled_trackers then
open_state = get_open_status()
closed_state = get_closed_status()
if state == 'close' then
change_state = get_open_status()
test = "="
status_message = settings['status_message_closed']
elsif state == 'open' then
change_state = get_closed_status()
test = "<"
status_message = settings['status_message_open']
end

# Get all issues that have a done ratio of 100% (meaning all subtask closed) but the status is not closed
Issue.where("done_ratio=100 AND tracker_id IN (#{enabled_trackers}) AND status_id IN (#{open_state})").each do |issue|
Issue.where("done_ratio#{test}100 AND tracker_id IN (#{enabled_trackers}) AND status_id IN (#{change_state}) AND id IN (SELECT parent_id FROM issues)").each do |issue|
i = Issue.find issue.id
new_status = IssueStatus.find get_next_state(i.tracker_id)
new_status = IssueStatus.find get_next_state(i.tracker_id, state)

#puts i.id.to_s + " " + i.subject + ": " + (IssueStatus.find i.status_id).name + " --> " + new_status.name
puts i.id.to_s + " " + i.subject + ": " + (IssueStatus.find i.status_id).name + " --> " + new_status.name
i.init_journal(User.anonymous, status_message)
i.update_attribute :status, new_status
end
Expand Down
74 changes: 54 additions & 20 deletions app/views/settings/_issue_status_change.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,85 @@
<pre><%= l(:issue_status_change_rake_task) %></pre>


<% settings['status_change'] = Hash.new if settings['status_change'] == nil %>
<% settings['new_status'] = Hash.new if settings['new_status'] == nil %>
<% settings['status_message'] = l(:status_message) if settings['status_message'] == nil %>
<% settings['status_change']['close'] = Hash.new if settings['status_change']['close'] == nil %>
<% settings['new_status']['close'] = Hash.new if settings['new_status']['close'] == nil %>

<% settings['status_change']['open'] = Hash.new if settings['status_change']['open'] == nil %>
<% settings['new_status']['open'] = Hash.new if settings['new_status']['open'] == nil %>

<% settings['status_message_closed'] = l(:status_message_closed) if settings['status_message_closed'] == nil %>
<% settings['status_message_open'] = l(:status_message_open) if settings['status_message_open'] == nil %>

<fieldset>
<legend><%= l(:status_message_legend) %></legend>
<p>
<label><%= l(:status_message_label) %></label>
<%= text_field_tag "settings[status_message]", settings[:status_message] %>
<label><%= l(:status_message_label_closed) %></label>
<%= text_field_tag "settings[status_message_closed]", settings[:status_message_closed] %>
</p>
<p>
<label><%= l(:status_message_label_open) %></label>
<%= text_field_tag "settings[status_message_open]", settings[:status_message_open] %>
</p>
</fieldset>

<% Tracker.all.each do |tracker| %>


<fieldset>
<legend><%= tracker.name %></legend>
<p>
<label><%= l(:change_status_label) %></label>
<%= check_box_tag "settings[status_change][#{tracker.id}]", "1", @settings[:status_change][tracker.id.to_s] %>
<label><%= l(:change_closed_label) %></label>
<%= check_box_tag "settings[status_change][close][#{tracker.id}]", "1", @settings[:status_change]['close'][tracker.id.to_s] %>
<br/><br/>
<label id="settings_new_status_<%= tracker.id %>_label"><%= l(:new_status) %></label>
<%= select_tag "settings[new_status][#{tracker.id}", options_for_select(IssueStatus.joins(:workflows).where(:workflows => {:tracker_id => tracker.id}, :is_closed => 1).uniq.map {|s| [s.name, s.id]},@settings[:new_status][tracker.id.to_s]) %>
<label id="settings_new_status_close_<%= tracker.id %>_label"><%= l(:new_status) %></label>
<%= select_tag "settings[new_status][close][#{tracker.id}", options_for_select(IssueStatus.joins(:workflows).where(:workflows => {:tracker_id => tracker.id}, :is_closed => 1).uniq.map {|s| [s.name, s.id]},@settings[:new_status]['close'][tracker.id.to_s]) %>
</p>

<p>
<label><%= l(:change_open_label) %></label>
<%= check_box_tag "settings[status_change][open][#{tracker.id}]", "1", @settings[:status_change]['open'][tracker.id.to_s] %>
<br/><br/>
<label id="settings_new_status_open_<%= tracker.id %>_label"><%= l(:new_status) %></label>
<%= select_tag "settings[new_status][open][#{tracker.id}", options_for_select(IssueStatus.joins(:workflows).where(:workflows => {:tracker_id => tracker.id}, :is_closed => 0).uniq.map {|s| [s.name, s.id]},@settings[:new_status]['open'][tracker.id.to_s]) %>
</p>
</fieldset>
<% end %>

<%= javascript_tag do %>
$(document).ready(function() {
$('[id^=settings_status_change_]').click(function() {
$('[id^=settings_status_change_close_]').click(function() {
tracker_id = this.id.substring(this.id.lastIndexOf("_")+1);
if ($('#settings_status_change_'+tracker_id).is(':checked')) {
$('#settings_new_status_'+tracker_id).show();
$('#settings_new_status_'+tracker_id+'_label').show();
if ($('#settings_status_change_close_'+tracker_id).is(':checked')) {
$('#settings_new_status_close_'+tracker_id).show();
$('#settings_new_status_close_'+tracker_id+'_label').show();
}
else {
$('#settings_new_status_'+tracker_id).hide();
$('#settings_new_status_'+tracker_id+'_label').hide();
$('#settings_new_status_close_'+tracker_id).hide();
$('#settings_new_status_close_'+tracker_id+'_label').hide();
}
});
});

$('[id^=settings_status_change_]:not(:checked)').each(function(index) {
$('[id^=settings_status_change_open_]').click(function() {
tracker_id = this.id.substring(this.id.lastIndexOf("_")+1);
$('#settings_new_status_'+tracker_id).hide();
$('#settings_new_status_'+tracker_id+'_label').hide();
if ($('#settings_status_change_open_'+tracker_id).is(':checked')) {
$('#settings_new_status_open_'+tracker_id).show();
$('#settings_new_status_open_'+tracker_id+'_label').show();
}
else {
$('#settings_new_status_open_'+tracker_id).hide();
$('#settings_new_status_open_'+tracker_id+'_label').hide();
}
});

$('[id^=settings_status_change_open_]:not(:checked)').each(function(index) {
tracker_id = this.id.substring(this.id.lastIndexOf("_")+1);
$('#settings_new_status_open_'+tracker_id).hide();
$('#settings_new_status_open_'+tracker_id+'_label').hide();
});

$('[id^=settings_status_change_close_]:not(:checked)').each(function(index) {
tracker_id = this.id.substring(this.id.lastIndexOf("_")+1);
$('#settings_new_status_close_'+tracker_id).hide();
$('#settings_new_status_close_'+tracker_id+'_label').hide();
});
});
<% end %>
11 changes: 7 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ en:
issue_status_change: "Change issue status automatically"
issue_status_change_help: "This plugin does not change the status unless you define a cron job that runs a rake task periodically. This task is:"
issue_status_change_rake_task: "rake redmine:plugins:issue_status_change:close_issues_with_all_subtasks_closed RAILS_ENV=production"
change_status_label: "Change status when all subtasks are done"
new_status: "Closed status"
status_message: "Automatically changing status because all subtasks are solved"
status_message_label: "Journal message when status is changed"
change_closed_label: "Change status when all subtasks are done"
change_open_label: "Change status when all there are new open subtasks"
new_status: "New status"
status_message_closed: "Automatically changing status because all subtasks are solved"
status_message_label_closed: "Journal message when issue is closed"
status_message_open: "Automatically changing status there are still open subtasks"
status_message_label_open: "Journal message when issue is reopened"
status_message_legend: "Journal"
6 changes: 5 additions & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
}, :partial => 'settings/issue_status_change'

settings[:default]["change_status"] = Hash.new
settings[:default]["new_status"] = Hash.new
settings[:default]["new_status"]= Hash.new
settings[:default]["change_status"]["close"] = Hash.new
settings[:default]["new_status"]["close"] = Hash.new
settings[:default]["change_status"]["open"] = Hash.new
settings[:default]["new_status"]["open"] = Hash.new

end
14 changes: 14 additions & 0 deletions lib/tasks/open_issues_with_open_subtasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace :redmine do
namespace :plugins do
namespace :issue_status_change do
desc <<-END_DESC
Bla bla bla
rake redmine:plugins:issue_status_change:open_issues_with_open_subtasks RAILS_ENV=production
END_DESC
task open_issues_with_open_subtasks: :environment do |_t, _args|
IssueStatusChanger.open_issues_with_open_subtasks
end
end
end
end

0 comments on commit 43a6797

Please sign in to comment.