Skip to content

Instantly share code, notes, and snippets.

@tobiasmh
Forked from lukaszraczylo/delete-hipchat-history.rb
Last active August 29, 2015 14:17
Show Gist options
  • Save tobiasmh/b3e697b09013d57c565e to your computer and use it in GitHub Desktop.
Save tobiasmh/b3e697b09013d57c565e to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# To make it work:
# - change account details to yours ( obviously ;) )
# - install mechanize gem in version 1.0.0 ( gem install mechanize -v '1.0.0' )
# - look for !!HERE!! comments in code in case you'd like to change default behavior
# - for best results run this script few times as hipchat / ruby openssl don't play together that well and
# there might be an exception thrown from time to time. Script should retry then but hey.. Nobody is perfect.
# .. and finally. Remember that other part should run this script as well to make history clean.
require 'rubygems'
require 'mechanize' # for making everything work
require 'chronic' # for parsing weird timestamps from hipchat
account = {
:email => "[email protected]",
:password => "pswd",
}
agent = Mechanize.new
agent.redirect_ok = true
site = agent.post("https://www.hipchat.com/sign_in", account).uri.host
# Get our user id
member_id = agent.get("https://#{site}/account").
search('img[alt="Your photo"]').first["src"].match(%r{photos/(\d+)})[1]
# Determine our Signup date and the actual date
registered_around = agent.get("https://#{site}/people/show/#{member_id}").
search('li:contains("Signed up")').text.sub(/Signed up: /, '')
registered_on = Date.parse(Chronic.parse(registered_around).to_s)
# !!HERE!!
# You can change behaviour to 'remove only messages since...' by changing registered_on
# variable to specific date, like:
# registered_on = Date.parse("01-01-2014")
puts "Found registration date: #{registered_on}"
intervals = (registered_on..Date.today).map { |d| d.strftime("/%Y/%m/%d") }
# Identify all users ids
members = agent.get("https://#{site}/people").search("ul.members").children.search("a").
map { |m| m.attributes["href"].value.to_s.split('/')[3] }.
reject { |m| m =~ /#{member_id}/ }
# !!HERE!!
# Instead of removing all the private chats, with everyone in your organisation
# you might define single user or user ids just like this:
# members = [ "666" , "777" ]
max_process = 20
# !!HERE!!
# I _don't_ recommend going above 20 as it makes hipchat devops cry and their servers suffer.
# Just leave default '20' and leave script for the night or weekend ;)
processing_pids = {}
# deleting all the history
members.each do |member|
intervals.each do |interval|
# Added some level of verbosity in checks.
puts "Looking for chats with user #{member} on #{interval.gsub(/^\//, '')}"
day_history = agent.get("https://#{site}/history/member/#{member.to_i}#{interval}")
day_history.forms.each do |f|
begin
if f.action =~ /history\/member\/#{member}/
puts ">> QUEUE >> #{processing_pids.size}"
while processing_pids.size < max_process
pid_starting = Process.fork {
puts "Deleting.."
f.submit
}
processing_pids[pid_starting] = "1"
end
end
if processing_pids.size != 0
if pid_done = Process.wait(0, Process::WNOHANG)
if job_finished = processing_pids.delete(pid_done)
puts "Deleted"
end
else
sleep(2)
# raise an exception to get a retry
raise "Force retry"
end
end
rescue
puts "Retrying"
retry
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment