Class: PartyFoul::ExceptionHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/party_foul/exception_handler.rb

Direct Known Subclasses

RacklessExceptionHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exception, env) ⇒ ExceptionHandler

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

Parameters:

  • (Exception, Hash)


19
20
21
22
23
24
25
26
27
# 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

Instance Attribute Details

#rendered_issueObject

Returns the value of attribute rendered_issue.



4
5
6
# File 'lib/party_foul/exception_handler.rb', line 4

def rendered_issue
  @rendered_issue
end

Class Method Details

.handle(exception, env) ⇒ Object

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.

Parameters:

  • (Exception, Hash)


11
12
13
# File 'lib/party_foul/exception_handler.rb', line 11

def self.handle(exception, env)
  PartyFoul.processor.handle(exception, clean_env(env))
end

Instance Method Details

#create_issueObject

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



45
46
47
48
49
# 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_issueObject

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



40
41
42
# File 'lib/party_foul/exception_handler.rb', line 40

def find_issue
  find_first_issue('open') || find_first_issue('closed')
end

#runObject

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.



31
32
33
34
35
36
37
# 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) ⇒ Object

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’.

Parameters:

  • (Sawyer::Resource)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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