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)


17
18
19
20
21
22
23
24
25
# File 'lib/party_foul/exception_handler.rb', line 17

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.



2
3
4
# File 'lib/party_foul/exception_handler.rb', line 2

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)


9
10
11
# File 'lib/party_foul/exception_handler.rb', line 9

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



43
44
45
46
47
# File 'lib/party_foul/exception_handler.rb', line 43

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.



38
39
40
# File 'lib/party_foul/exception_handler.rb', line 38

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.



29
30
31
32
33
34
35
# File 'lib/party_foul/exception_handler.rb', line 29

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)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/party_foul/exception_handler.rb', line 52

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