Class: ExceptionNotifier::GitlabNotifier
- Inherits:
-
Object
- Object
- ExceptionNotifier::GitlabNotifier
- Defined in:
- lib/gitlab_exception_notification/gitlab_notifier.rb
Instance Method Summary collapse
- #call(exception, options = {}) ⇒ Object
- #create_issue(exception) ⇒ Object
- #exception_notification(env, exception, options = {}) ⇒ Object
- #get_all_issues ⇒ Object
-
#initialize(options) ⇒ GitlabNotifier
constructor
A new instance of GitlabNotifier.
- #issue_description(exception) ⇒ Object
- #issue_exists?(exception) ⇒ Boolean
- #issue_summary(exception) ⇒ Object
-
#issue_title(exception) ⇒ Object
The issue title.
- #md_hash(hash, pre = "") ⇒ Object
- #update_issue(id, exception) ⇒ Object
Constructor Details
#initialize(options) ⇒ GitlabNotifier
Returns a new instance of GitlabNotifier.
13 14 15 16 17 18 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 13 def initialize() p "Booting issue notifications" @client = Gitlab.client(endpoint: 'http://gitlab.42.fr/api/v3', private_token: [:private_token]) @project_id = @client.project_search([:project_name]).first.id @issues = get_all_issues end |
Instance Method Details
#call(exception, options = {}) ⇒ Object
134 135 136 137 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 134 def call(exception, ={}) env = [:env] || {} exception_notification(env, exception, ) end |
#create_issue(exception) ⇒ Object
113 114 115 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 113 def create_issue(exception) @client.create_issue(@project_id, issue_title(exception), {description: issue_description(exception)}) end |
#exception_notification(env, exception, options = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 117 def exception_notification(env, exception, ={}) @env = env @exception = exception @options = .reverse_merge(env['exception_notifier.options'] || {}) @kontroller = env['action_controller.instance'] || MissingController.new @request = ActionDispatch::Request.new(env) @sections = @options[:sections] @data = (env['exception_notifier.exception_data'] || {}).merge([:data] || {}) @sections = @sections + %w(data) unless @data.empty? if issue_id = issue_exists?(exception) update_issue(issue_id, exception) else create_issue(exception) end end |
#get_all_issues ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 28 def get_all_issues page = 1 i = @client.issues(@project_id, per_page: PER_PAGE, page: page, order_by: :updated_at) @issues = i while i.count == PER_PAGE i = @client.issues(@project_id, per_page: PER_PAGE, page: page, order_by: :updated_at) @issues += i page += 1 end return @issues.flatten end |
#issue_description(exception) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 84 def issue_description exception # Get a 'mardowned' backtrace m_backtrace = "```#{SLINE} #{exception.backtrace.join(SLINE)}#{SLINE}```" # Get the concerned file file = exception.backtrace.first description = ["#{exception.} #{@kontroller ? 'in controller ' + @kontroller.controller_name + '#' + @kontroller.action_name : ''}"] description << "File: #{file}" { 'Summary': issue_summary(exception).map { |k, v| "- #{k}: #{v}"}.join(SLINE), 'session id': @request.ssl? ? "[FILTERED]" : (@request.session['session_id'] || (@request.env["rack.session.options"] and @request.env["rack.session.options"][:id])).inspect, 'data': @data, 'backtrace': m_backtrace, 'request headers': md_hash(@request.headers), 'environment': md_hash(@env.reject{|k, v| (REJECT_HEADERS =~ k).nil? }) }.reject{|k, v| v.nil? or v.blank?}.each do |k, v| description << "--------------------------------" description << "#### #{k.to_s.humanize}: " description << v.to_s end description.join("\n\n") end |
#issue_exists?(exception) ⇒ Boolean
20 21 22 23 24 25 26 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 20 def issue_exists?(exception) @issues = get_all_issues rest = @issues.select do |i| i.title == issue_title(exception) and i.description and i.description[exception.backtrace.first] end (rest.count > 0 ? rest.first.id : false) end |
#issue_summary(exception) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 70 def issue_summary exception { 'URL': @request.url, 'HTTP Method': @request.request_method, 'IP address': @request.remote_ip, 'Parameters': md_hash(@request.filtered_parameters, STAB), 'Timestamp': Time.current, 'Server': Socket.gethostname, 'Rails root': (defined?(Rails) && Rails.respond_to?(:root) ? Rails.root : nil), 'Process': $$, 'session data': md_hash(@request.session.to_hash, STAB), } end |
#issue_title(exception) ⇒ Object
The issue title
62 63 64 65 66 67 68 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 62 def issue_title exception title = [] title << "#{@kontroller.controller_name}##{@kontroller.action_name}" if @kontroller title << "(#{exception.class})" title << (exception..length > 120 ? exception.[0..120] + "..." : exception.) title.join(' ') end |
#md_hash(hash, pre = "") ⇒ Object
109 110 111 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 109 def md_hash hash, pre = "" hash.map { |k, v| "#{pre}- **#{k}**: `#{v}`"}.join(SLINE) end |
#update_issue(id, exception) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gitlab_exception_notification/gitlab_notifier.rb', line 40 def update_issue(id, exception) issue = @client.issue(@project_id, id) last = issue.updated_at.to_date if last < 1.hour.ago begin @client.edit_issue(@project_id, id, {state_event: "reopen"}) iss = @client.edit_issue(@project_id, id, {title: "#{issue.title}"}) rescue Exception => e p "An error occured: #{e.inspect}" end else body = ":fire: This issue occured again #{Time.current}. \n#### Summary:\n #{issue_summary(exception).map { |k, v| "- #{k}: #{v}"}.join(SLINE)} " @client.reopen_issue(@project_id, id) @client.create_issue_note @project_id, id, body end end |