class PartyFoul::ExceptionHandler

Attributes

rendered_issue[RW]

Public Class Methods

handle(exception, env) click to toggle source

This handler will pass the exception and env from Rack off to a processor. The default PartyFoul processor will work synchronously. Processor adapters can be written to push this logic to a background job if desired.

@param [Exception, Hash]

# File lib/party_foul/exception_handler.rb, line 11
def self.handle(exception, env)
  PartyFoul.processor.handle(exception, clean_env(env))
end
new(exception, env) click to toggle source

Makes an attempt to determine what framework is being used and will use the proper IssueRenderer.

@param [Exception, Hash]

# File lib/party_foul/exception_handler.rb, line 19
def initialize(exception, env)
  renderer_klass = if defined?(Rails)
                     PartyFoul::IssueRenderers::Rails
                   else
                     PartyFoul::IssueRenderers::Rack
                   end

  self.rendered_issue = renderer_klass.new(exception, env)
end

Private Class Methods

clean_env(env) click to toggle source
# File lib/party_foul/exception_handler.rb, line 76
def self.clean_env(env)
  env.select do |key, value|
    begin
      Marshal.dump(value)
    rescue TypeError
      true
    rescue
      false
    end
  end
end

Public Instance Methods

create_issue() click to toggle source

Will create a new issue and a comment with the proper details. All issues are labeled as 'bug'.

# File lib/party_foul/exception_handler.rb, line 45
def create_issue
  self.sha = PartyFoul.github.references(PartyFoul.repo_path, "heads/#{PartyFoul.branch}").object.sha
  issue = PartyFoul.github.create_issue(PartyFoul.repo_path, rendered_issue.title, rendered_issue.body, labels: ['bug'] + rendered_issue.labels)
  PartyFoul.github.add_comment(PartyFoul.repo_path, issue[:number], rendered_issue.comment)
end
find_issue() click to toggle source

Hits the GitHub API to find the matching issue using the fingerprint.

# File lib/party_foul/exception_handler.rb, line 40
def find_issue
  find_first_issue('open') || find_first_issue('closed')
end
run() click to toggle source

Begins to process the exception for GitHub Issues. Makes an attempt to find the issue. If found will update the issue. If not found will create a new issue.

# File lib/party_foul/exception_handler.rb, line 31
def run
  if issue = find_issue
    update_issue(issue)
  else
    create_issue
  end
end
update_issue(issue) click to toggle source

Updates the given issue. If the issue is labeled as 'wontfix' nothing is done. If the issue is closed the issue is reopened and labeled as 'regression'.

@param [Sawyer::Resource]

# File lib/party_foul/exception_handler.rb, line 54
def update_issue(issue)
  label_names = issue.key?(:labels) ? issue[:labels].map {|label| label[:name] } : []

  unless label_names.include?('wontfix')
    body = rendered_issue.update_body(issue[:body])
    params = {state: 'open'}

    if issue[:state] == 'closed'
      params[:labels] = (['bug', 'regression'] + label_names).uniq
    end

    self.sha = PartyFoul.github.references(PartyFoul.repo_path, "heads/#{PartyFoul.branch}").object.sha
    PartyFoul.github.update_issue(PartyFoul.repo_path, issue[:number], issue.title, body, params)

    unless comment_limit_met?(issue[:body])
      PartyFoul.github.add_comment(PartyFoul.repo_path, issue[:number], rendered_issue.comment)
    end
  end
end

Private Instance Methods

comment_limit_met?(body) click to toggle source
# File lib/party_foul/exception_handler.rb, line 101
def comment_limit_met?(body)
  !!PartyFoul.comment_limit && PartyFoul.comment_limit <= occurrence_count(body)
end
find_first_issue(state) click to toggle source
# File lib/party_foul/exception_handler.rb, line 105
def find_first_issue(state)
  PartyFoul.github.search_issues("#{fingerprint} repo:#{PartyFoul.repo_path} state:#{state}").items.first
end
fingerprint() click to toggle source
# File lib/party_foul/exception_handler.rb, line 88
def fingerprint
  rendered_issue.fingerprint
end
occurrence_count(body) click to toggle source
# File lib/party_foul/exception_handler.rb, line 96
def occurrence_count(body)
  result = body.match(/<th>Count<\/th><td>(\d+)<\/td>/)
  result.nil? ? 0 : result[1].to_i
end
sha=(sha) click to toggle source
# File lib/party_foul/exception_handler.rb, line 92
def sha=(sha)
  rendered_issue.sha = sha
end