Class: HackerOne::Client::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/hackerone/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(program = nil) ⇒ Api

Returns a new instance of Api.



47
48
49
# File 'lib/hackerone/client.rb', line 47

def initialize(program = nil)
  @program = program
end

Instance Method Details

#add_report_reference(id, reference) ⇒ Object

Idempotent: Add a report reference to a project

id: the ID of the report state: value for the reference (e.g. issue number or relative path to cross-repo issue)

returns an HackerOne::Client::Report object or raises an error if no report is found.



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hackerone/client.rb', line 101

def add_report_reference(id, reference)
  body = {
    data: {
      type: "issue-tracker-reference-id",
      attributes: {
        reference: reference
      }
    }
  }

  post("reports/#{id}/issue_tracker_reference_id", body)
end

#programObject



51
52
53
# File 'lib/hackerone/client.rb', line 51

def program
  @program || HackerOne::Client.program
end

#report(id) ⇒ Object

Public: retrieve a report

id: the ID of a specific report

returns an HackerOne::Client::Report object or raises an error if no report is found.



147
148
149
# File 'lib/hackerone/client.rb', line 147

def report(id)
  get("reports/#{id}")
end

#reports(since: 3.days.ago) ⇒ Object

Returns all open reports, optionally with a time bound

program: the HackerOne program to search on (configure globally with Hackerone::Client.program=) since (optional): a time bound, don’t include reports earlier than since. Must be a DateTime object.

returns all open reports or an empty array

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hackerone/client.rb', line 61

def reports(since: 3.days.ago)
  raise ArgumentError, "Program cannot be nil" unless program
  response = self.class.hackerone_api_connection.get do |req|
    options = {
      "filter[state][]" => "new",
      "filter[program][]" => program,
      "filter[created_at__gt]" => since.iso8601
    }
    req.url "reports", options
  end

  data = JSON.parse(response.body, :symbolize_names => true)[:data]
  if data.nil?
    raise RuntimeError, "Expected data attribute in response: #{response.body}"
  end

  data.map do |report|
    Report.new(report)
  end
end

#state_change(id, state, message = nil) ⇒ Object

Idempotent: change the state of a report. See STATES for valid values.

id: the ID of the report state: the state in which the report is to be put in

returns an HackerOne::Client::Report object or raises an error if no report is found.

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hackerone/client.rb', line 121

def state_change(id, state, message = nil)
  raise ArgumentError, "state (#{state}) must be one of #{STATES}" unless STATES.include?(state)

  body = {
    data: {
      type: "state-change",
      attributes: {
        state: state
      }
    }
  }

  if message
    body[:message] = message
  elsif STATES_REQUIRING_STATE_CHANGE_MESSAGE.include?(state)
    fail ArgumentError, "State #{state} requires a message. No message was supplied."
  end
  post("reports/#{id}/state_changes", body)
end

#triage(id, reference) ⇒ Object

Idempotent: add the issue reference and put the report into the “triage” state.

id: the ID of the report state: value for the reference (e.g. issue number or relative path to cross-repo issue)

returns an HackerOne::Client::Report object or raises an error if no report is found.



89
90
91
92
# File 'lib/hackerone/client.rb', line 89

def triage(id, reference)
  add_report_reference(id, reference)
  state_change(id, :triaged)
end