issue_tracker.rb
4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class IssueTracker
include Mongoid::Document
include Mongoid::Timestamps
include HashHelper
include Rails.application.routes.url_helpers
default_url_options[:host] = Errbit::Application.config.action_mailer.default_url_options[:host]
validate :check_params
embedded_in :app, :inverse_of => :issue_tracker
field :account, :type => String
field :api_token, :type => String
field :project_id, :type => String
field :username, :type => String
field :password, :type => String
field :issue_tracker_type, :type => String, :default => 'lighthouseapp'
def create_issue err
case issue_tracker_type
when 'lighthouseapp'
create_lighthouseapp_issue err
when 'redmine'
create_redmine_issue err
when 'pivotal'
create_pivotal_issue err
when 'fogbugz'
create_fogbugz_issue err
end
end
protected
def create_redmine_issue err
token = api_token
acc = account
RedmineClient::Base.configure do
self.token = token
self.site = acc
end
issue = RedmineClient::Issue.new(:project_id => project_id)
issue.subject = issue_title err
issue.description = self.class.redmine_body_template.result(binding)
issue.save!
err.update_attribute :issue_link, "#{RedmineClient::Issue.site.to_s.sub(/#{RedmineClient::Issue.site.path}$/, '')}#{RedmineClient::Issue.element_path(issue.id, :project_id => project_id)}".sub(/\.xml\?project_id=#{project_id}$/, "\?project_id=#{project_id}")
end
def create_pivotal_issue err
PivotalTracker::Client.token = api_token
PivotalTracker::Client.use_ssl = true
project = PivotalTracker::Project.find project_id.to_i
story = project.stories.create :name => issue_title(err), :story_type => 'bug', :description => self.class.pivotal_body_template.result(binding)
err.update_attribute :issue_link, "https://www.pivotaltracker.com/story/show/#{story.id}"
end
def create_lighthouseapp_issue err
Lighthouse.account = account
Lighthouse.token = api_token
# updating lighthouse account
Lighthouse::Ticket.site
ticket = Lighthouse::Ticket.new(:project_id => project_id)
ticket.title = issue_title err
ticket.body = self.class.lighthouseapp_body_template.result(binding)
ticket.tags << "errbit"
ticket.save!
err.update_attribute :issue_link, "#{Lighthouse::Ticket.site.to_s.sub(/#{Lighthouse::Ticket.site.path}$/, '')}#{Lighthouse::Ticket.element_path(ticket.id, :project_id => project_id)}".sub(/\.xml$/, '')
end
def create_fogbugz_issue err
fogbugz = Fogbugz::Interface.new(:email => username, :password => password, :uri => "https://#{account}.fogbugz.com")
fogbugz.authenticate
issue = {}
issue['sTitle'] = issue_title err
issue['sArea'] = project_id
issue['sEvent'] = self.class.fogbugz_body_template.result(binding)
issue['sTags'] = ['errbit'].join(',')
issue['cols'] = ['ixBug'].join(',')
fb_resp = fogbugz.command(:new, issue)
err.update_attribute :issue_link, "https://#{account}.fogbugz.com/default.asp?#{fb_resp['case']['ixBug']}"
end
def issue_title err
"[#{ err.environment }][#{ err.where }] #{err.message.to_s.truncate(100)}"
end
def check_params
blank_flag_fields = %w(project_id)
if(%w(fogbugz).include?(issue_tracker_type))
blank_flag_fields += %w(username password)
else
blank_flag_fields << 'api_token'
end
blank_flag_fields << 'account' if(%w(fogbugz lighthouseapp redmine).include?(issue_tracker_type))
blank_flags = blank_flag_fields.map {|m| self[m].blank? }
if blank_flags.any? && !blank_flags.all?
message = case issue_tracker_type
when 'lighthouseapp'
'You must specify your Lighthouseapp account, api token and project id'
when 'redmine'
'You must specify your Redmine url, api token and project id'
when 'pivotal'
'You must specify your Pivotal Tracker api token and project id'
when 'fogbugz'
'You must specify your FogBugz Area Name, Username, and Password'
end
errors.add(:base, message)
end
end
class << self
def lighthouseapp_body_template
@@lighthouseapp_body_template ||= ERB.new(File.read(Rails.root + "app/views/errs/lighthouseapp_body.txt.erb").gsub(/^\s*/, ''))
end
def redmine_body_template
@@redmine_body_template ||= ERB.new(File.read(Rails.root + "app/views/errs/redmine_body.txt.erb"))
end
def pivotal_body_template
@@pivotal_body_template ||= ERB.new(File.read(Rails.root + "app/views/errs/pivotal_body.txt.erb"))
end
def fogbugz_body_template
@@fogbugz_body_template ||= ERB.new(File.read(Rails.root + "app/views/errs/fogbugz_body.txt.erb"))
end
end
end