forked from errbit/errbit
/
issue_tracker.rb
101 lines (86 loc) · 3.25 KB
/
issue_tracker.rb
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
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 :issue_tracker_type, :type => String, :default => 'lighthouseapp'
def create_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 = "[#{ err.environment }][#{ err.where }] #{err.message.to_s.truncate(100)}"
ticket.body = ""
ticket.body += "[See this exception on Errbit](#{ app_err_url err.app, err } \"See this exception on Errbit\")"
ticket.body += "\n"
if notice = err.notices.first
ticket.body += "# #{notice.message} #"
ticket.body += "\n"
ticket.body += "## Summary ##"
ticket.body += "\n"
if notice.request['url'].present?
ticket.body += "### URL ###"
ticket.body += "\n"
ticket.body += "[#{notice.request['url']}](#{notice.request['url']})"
ticket.body += "\n"
end
ticket.body += "### Where ###"
ticket.body += "\n"
ticket.body += notice.err.where
ticket.body += "\n"
ticket.body += "### Occured ###"
ticket.body += "\n"
ticket.body += notice.created_at.to_s(:micro)
ticket.body += "\n"
ticket.body += "### Similar ###"
ticket.body += "\n"
ticket.body += (notice.err.notices.count - 1).to_s
ticket.body += "\n"
ticket.body += "## Params ##"
ticket.body += "\n"
ticket.body += "<code>#{pretty_hash(notice.params)}</code>"
ticket.body += "\n"
ticket.body += "## Session ##"
ticket.body += "\n"
ticket.body += "<code>#{pretty_hash(notice.session)}</code>"
ticket.body += "\n"
ticket.body += "## Backtrace ##"
ticket.body += "\n"
ticket.body += "<code>"
for line in notice.backtrace
ticket.body += "#{line['number']}: #{line['file'].sub(/^\[PROJECT_ROOT\]/, '')} -> **#{line['method']}**"
ticket.body += "\n"
end
ticket.body += "</code>"
ticket.body += "\n"
ticket.body += "## Environment ##"
ticket.body += "\n"
for key, val in notice.env_vars
ticket.body += "#{key}: #{val}"
end
ticket.body += "\n"
end
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
protected
def check_params
blank_flags = %w( api_token project_id account ).map {|m| self[m].blank? }
if blank_flags.any? && !blank_flags.all?
message = if issue_tracker_type == 'lighthouseapp'
"You must specify your Lighthouseapp account, api token and project id"
else
"You must specify your Redmine url, api token and project id"
end
errors.add(:base, message)
end
end
end