This repository has been archived by the owner on Jul 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zendesk_redactor.rb
72 lines (50 loc) · 1.72 KB
/
zendesk_redactor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'http'
require 'redis'
require_relative 'config'
class ZendeskRedactor
REDIS_TICKET_ID_PAGE = 100
REDIS_TICKET_ID_SET = 'zendesk_ticket_ids'.freeze
SECONDS_IN_TWO_YEARS = 630_720_00
ZENDESK_API_SEARCH_URL = 'https://pensionwise.zendesk.com/api/v2/search.json'.freeze
ZENDESK_API_DELETE_URL = 'https://pensionwise.zendesk.com/api/v2/tickets/destroy_many.json'.freeze
def initialize(output: STDOUT)
@output = output
end
def run
store_ticket_ids_to_delete!
delete_stored_tickets!
end
private
attr_reader :output
def redis
@redis ||= Redis.new(url: ENV.fetch('REDIS_URL'), ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_NONE })
end
def delete_stored_tickets!
while (ticket_ids = pop_stored_ticket_ids_to_delete).any?
puts "Deleting ticket IDs: #{ticket_ids}"
api.delete("#{ZENDESK_API_DELETE_URL}?ids=#{ticket_ids.join(',')}")
puts 'Done!'
end
end
def pop_stored_ticket_ids_to_delete
redis.spop(REDIS_TICKET_ID_SET, 100)
end
def store_ticket_ids_to_delete!
date = (Time.now - SECONDS_IN_TWO_YEARS).strftime('%Y-%m-%d')
query = URI.encode("type:ticket created<#{date}")
ticket_ids = get_tickets("#{ZENDESK_API_SEARCH_URL}?query=#{query}")
redis.sadd(REDIS_TICKET_ID_SET, ticket_ids) if ticket_ids.any?
end
def api
HTTP.auth("Basic #{ENV.fetch('ZENDESK_AUTH_TOKEN')}")
end
def get_tickets(url, ticket_ids = [])
return [] unless url
output.puts 'Getting ticket IDs'
response = api.get(url).parse
return ticket_ids.flatten unless response['results']
ticket_ids << response['results'].map { |result| result['id'] }
get_tickets(response['next_page'], ticket_ids)
end
end
ZendeskRedactor.new.run