Project

General

Profile

Feature #1112 » whinemail.rb

Carl Nygard, 2008-04-25 22:28

 
1
#!/usr/bin/env ruby
2

    
3
RAILS_ENV = 'test'
4
REDMINE_ROOT = '/var/www/html/redmine/'
5
# This does _NOT_ work. I looked back 3 revisions and found the correct line
6
#require File.dirname(__FILE__) + '/../config/boot'
7
require REDMINE_ROOT + 'config/environment'
8

    
9
require 'getoptlong'
10
require 'mailer'
11
require 'actionmailer'
12
require 'application_helper'
13

    
14
class Whiner 
15

    
16
  attr_accessor :project, :tracker, :due, :debug
17

    
18
  def initialize
19
    @project = nil
20
    @tracker = nil
21
    @due = Date.today + 3
22
    @debug = false
23
  end
24

    
25
  def due_in_days(days)
26
    @due = Date.today + days
27
  end
28

    
29
  def process
30

    
31
    if(@debug)
32
      puts "Mailer settings [#{ActionMailer::Base.smtp_settings.to_yaml}]"
33
      puts "do deliveries [#{ActionMailer::Base.perform_deliveries.to_s}]"
34
      puts "method [#{ActionMailer::Base.delivery_method.to_s}]"
35
      puts "errors [#{ActionMailer::Base.raise_delivery_errors.to_s}]"
36
    end
37
    if( ActionMailer::Base.delivery_method != :smtp )
38
      puts "Need to reset delivery_method to make it go out"
39
      ActionMailer::Base.delivery_method = :smtp
40
    end
41

    
42
    u = User.find_by_login('admin')
43
    User.current = u
44

    
45
    projects = []
46
    proj = Project.find_by_identifier(@project)
47
    if(! proj) 
48
      projects = Project.find(:all)
49
    else
50
      projects << proj
51
    end
52

    
53
    puts "Scanning [#{projects.size}] projects..." if @debug
54
    projects.each { |p|
55
      t = p.trackers.find_by_name(@tracker)
56

    
57
      puts "Grabbing issues from [#{p.name}/#{p.id}] between #{Date.today} and #{@due}" if @debug
58

    
59
      if(t) 
60
        issues = Issue.find(:all,
61
                            :conditions => [ 'due_date < ? and due_date >= ? and project_id = ? and tracker_id = ?', 
62
                                            @due, Date.today, p.id, t.id]
63
                            )
64
      else
65
        issues = Issue.find(:all,
66
                            :conditions => [ 'due_date < ? and due_date >= ? and project_id = ?', 
67
                                            @due, Date.today, p.id]
68
                            )
69
      end
70

    
71
      puts "Found [#{issues.size}] issues" if @debug
72

    
73
      issues.each { |i| 
74
        puts "Whining about issue [#{i.id}]" if( @debug )
75

    
76
        puts "Delivery failed" unless Mailer.deliver_issue_due(i)
77
      }
78
    }
79

    
80
    return true
81
  end
82
end
83

    
84
opts = GetoptLong.new(
85
      [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
86
      [ '--debug', GetoptLong::NO_ARGUMENT ],
87
      [ '--project', '-p', GetoptLong::REQUIRED_ARGUMENT ],
88
      [ '--tracker', '-t', GetoptLong::REQUIRED_ARGUMENT ],
89
      [ '--days', '-d',  GetoptLong::REQUIRED_ARGUMENT ]
90
    )
91

    
92
def usage
93
  puts "Usage: [--help] [--debug] [--project <projectName>] [--tracker <trackerName>] [--days <daysInFuture>]"
94
  puts " --help  show this help"
95
  puts " --debug show extra debug information"
96
  puts " --project   name of project to process"
97
  puts " --tracker   name of tracker for filtering issues"
98
  puts " --days      how many days in the future to whine about"
99
  exit
100
end
101

    
102
whiner = Whiner.new
103

    
104
opts.each do |opt, arg|
105
  case opt
106
  when '--help'
107
    usage
108
  when '--project'
109
    whiner.project = arg
110
  when '--tracker'
111
    whiner.tracker = arg
112
  when '--days'
113
    whiner.due_in_days(arg.to_i)
114
  when '--debug'
115
    whiner.debug = true
116
  end
117
end
118

    
119
whiner.process
120

    
(1-1/4)