Created
March 24, 2021 23:36
-
-
Save andriimosin/63c63c4cfa11183a40045b1cfdc843b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faraday' | |
require 'faraday_middleware' | |
require 'JSON' | |
class RollbarItemEraser | |
API_URL = 'https://api.rollbar.com/api/1/item/1049280555/instances'.freeze | |
OCCURANCE_API_URL = 'https://api.rollbar.com/api/1/instance/'.freeze | |
ROLLBAR_READ_TOKEN = 'ROLLBAR_READ_TOKEN'.freeze | |
ROLLBAR_WRITE_TOKEN = 'ROLLBAR_WRITE_TOKEN'.freeze | |
def initialize | |
# Looks like API doesn't understand merged items, have to go one by one: | |
# This array is the result of `get_item_ids_from_counters` method | |
item_ids = [1027734966, 1027804982, 1027808958, 1027816625, 1028153904, | |
1028278891, 1028358583, 1028398298, 1029370976, 1032060476, | |
1036293697, 1036753672, 1036761972, 1036770814, 1036775333, | |
1036797740, 1036953689, 1036953690, 1037052922, 1037084796, | |
1038155021, 1038350300, 1040005429, 1040107901] | |
item_ids.each do |item_id| | |
make_request(item_id) | |
end | |
end | |
# IDs displayed in Rollbar UI are not IDs, they are counters 🤦♂️ | |
# To get actual IDs, we need to make another API call | |
def get_item_ids_from_counters | |
actual_item_ids = [] | |
counters = [762015, 762089, 762092, 762094, 762139, 762176, 762183, | |
762186, 762241, 762404, 762598, 762630, 762632, 762633, | |
762634, 762637, 762660, 762661, 762667, 762671, 762739, | |
762747, 762816, 762824] | |
counters.each do |counter| | |
raw_response = Faraday.get("https://api.rollbar.com/api/1/item_by_counter/#{counter}", | |
{}, | |
"X-Rollbar-Access-Token" => ROLLBAR_READ_TOKEN) | |
parsed_response = JSON.parse(raw_response.body) | |
actual_item_ids << parsed_response['result']['itemId'] | |
end | |
actual_item_ids | |
end | |
private | |
def make_request(item_id) | |
puts "Processing Item ID #{item_id}" | |
page_index = 0 | |
instances = [1] | |
while instances.count > 0 do | |
raw_response = nil | |
page_index += 1 | |
raw_response = Faraday.get("https://api.rollbar.com/api/1/item/#{item_id}/instances?page=#{page_index}", | |
{}, | |
"X-Rollbar-Access-Token" => ROLLBAR_READ_TOKEN) | |
parsed_response = JSON.parse(raw_response.body) | |
instances = parsed_response['result']['instances'] | |
puts "Page index: #{page_index}, items count: #{instances.count}" | |
instances.each do |instance| | |
puts "instance ID: #{instance['id']}" | |
delete_occurance(instance['id']) if occurance_should_be_deleted?(instance['id']) | |
end | |
break if instances.count == 0 | |
end | |
end | |
def delete_occurance(occurance_id) | |
raw_response = Faraday.delete(OCCURANCE_API_URL + occurance_id.to_s, {}, "X-Rollbar-Access-Token" => ROLLBAR_WRITE_TOKEN) | |
result_ok = JSON.parse(raw_response.body)['err'] == 0 | |
puts "Alarma for occurance # #{occurance_id}" unless result_ok | |
puts "Deleted occurance #{occurance_id}" | |
end | |
def occurance_should_be_deleted?(occurance_id) | |
raw_response = Faraday.get(OCCURANCE_API_URL + occurance_id.to_s, {}, "X-Rollbar-Access-Token" => ROLLBAR_READ_TOKEN) | |
parsed_response = JSON.parse(raw_response.body) | |
# We want to proceed only if our custom variables key exist | |
response_request_body = parsed_response.dig('result', 'data', 'variables') | |
return false unless response_request_body | |
# can't parse this JSON, had to check it variables 'string' has the password | |
password_included = response_request_body.include?('password') | |
puts "Password was stored: #{response_request_body}" | |
puts "Deleting occurance #{occurance_id}, password was #{response_request_body}" if password_included | |
password_included | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment