5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-16 15:51:41 +00:00
panel/app/jobs/gitlab_notifier_job.rb

143 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class GitlabNotifierJob < ApplicationJob
include ExceptionNotifier::BacktraceCleaner
attr_reader :exception, :options
queue_as :low_priority
def perform(exception, **options)
@exception = exception
@options = options
Rails.logger.info 'Enviando reporte a Gitlab'
i = client.new_issue confidential: true, title: title, description: description
Rails.logger.info "Enviado reporte a Gitlab: #{i['iid']}"
rescue Exception => e
Rails.logger.info 'No entrar en loop'
end
private
# Define si es una excepción de javascript o local
#
# @see BacktraceJob
def javascript?
@javascript ||= options.dig(:data, :javascript_backtrace).present?
end
# @return [String]
def title
@title ||= ''.dup.tap do |t|
t << "[#{exception.class}] " unless javascript?
t << exception.message
end
end
# @return [String]
def description
@description ||= ''.dup.tap do |d|
d << request_section
d << javascript_section
d << javascript_footer
d << backtrace_section
d << data_section
end
end
# @return [String,Nil]
def backtrace
@backtrace ||= exception.backtrace ? clean_backtrace(exception) : nil
end
def env
options[:env]
end
def request
@request ||= ActionDispatch::Request.new(env) if env.present?
end
# @return [GitlabApiClient]
def client
@client ||= GitlabApiClient.new
end
def request_section
return '' unless request
<<~REQUEST
# Request
```
#{request.request_method} #{request.url}#{' '}
#{pp request.filtered_parameters}
```
REQUEST
end
def javascript_section
return '' unless javascript?
options.dig(:data, :params, 'errors')&.map do |error|
<<~JAVASCRIPT
## #{error['type'] || 'NoError'}: #{error['message']}
```
#{Terminal::Table.new headings: error['backtrace'].first.keys, rows: error['backtrace'].map(&:values)}
```
JAVASCRIPT
end&.join
end
def javascript_footer
return '' unless javascript?
<<~JAVASCRIPT
#{options.dig(:data, :params, 'context', 'userAgent')}
<#{options.dig(:data, :params, 'context', 'url')}>
JAVASCRIPT
end
def backtrace_section
return '' if javascript?
return '' unless backtrace
<<~BACKTRACE
## Backtrace
```
#{backtrace.join("\n")}
```
BACKTRACE
end
def data_section
return '' unless options[:data]
<<~DATA
## Data
```
#{pp options[:data]}
```
DATA
end
end