Skip to content

Instantly share code, notes, and snippets.

@radar
Created October 9, 2010 06:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save radar/617963 to your computer and use it in GitHub Desktop.
Save radar/617963 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'mechanize'
require 'httparty'
# Ugly code, for an ugly hack.
# Hey, if Lighthouse didn't suck then none of this would happen.
# It's been lotsa fun coding it though, so I suppose I should be thankful for the "opportunity"
def locate_form_by_button(page, value)
page.forms.detect { |f| f.buttons.detect { |b| b.value == value } }
end
class String
def underscore
self.downcase.gsub(" ", "_")
end
end
# The IDs of all the known bastards
bastards = [119236]
# Setup mechanize
agent = Mechanize.new
# Load configuration
config = YAML.load_file(File.join(ENV["HOME"] + "/.lighthouse.yml"))
# Logging in
page = agent.get('https://rails.lighthouseapp.com/login')
# There's two forms, use the login form not the open id form
login_form = page.forms[1]
login_form.email = config["email"]
login_form.password = config["password"]
page = agent.submit(login_form, login_form.buttons.first)
bastards.each do |bastard|
page = agent.get("http://rails.lighthouseapp.com/users/#{bastard}")
pp page.title
# Get all their ticket comments
links = page.links.select { |l| l.href =~ /tickets\/\d+/ }.map(&:href).uniq
links.each do |link|
begin
page = agent.get(link)
# Detect if the spammer has changed the title
id, version = /#ticket-(\d+)-(\d+)/.match(link).to_a
comment = page.search(id)
# Get the changes for this ticket and revert them.
# Because Lighthouse is too fucking stupid to revert (all) the changes of a ticket when you delete it.
changes = comment.search(".ticket-changes li")
update_ticket_form = locate_form_by_button(page, "Update ticket")
update_ticket_action = "http://rails.lighthouseapp.com#{update_ticket_form.action}"
p "Updating: #{update_ticket_action}"
authenticity_token = update_ticket_form.fields.detect { |f| f.name == "authenticity_token" }.value
params = {}
changes.map do |change|
matches = /(.*?) changed from (.*?) to (.*?)$/.match(change)
field = matches[1].split(" ")[1..-1].join(" ").underscore
original_value = matches[2].gsub("\u0093", "").gsub("\u0094", "").gsub("\302\223", "").gsub("\302\224", "")
new_value = matches[3]
params[field] = original_value
end
# If assigned user has changed, switch it back
user_select_box = update_ticket_form.fields.detect { |f| f.name == "ticket[assigned_user_id]" }
assigned_user = params.delete("assigned_user")
if !assigned_user.blank?
params["assigned_user_id"] = user_select_box.options.detect { |o| o.text == assigned_user }.select
end
# If tags changed, switch them back
tag = update_ticket_form.fields.detect { |f| f.name == "ticket[tag]"}
tag.value = params["tag"] if params["tag"]
# If title changed, switch it back
title = update_ticket_form.fields.detect { |f| f.name == "ticket[title]"}
title.value = params["title"] if params["title"]
comment = update_ticket_form.fields.detect { |f| f.name == "ticket[body]" }
comment.value = "Automatic cleanup of spam."
update_ticket_form.submit(update_ticket_form.buttons.first)
# Now we delete the motherfucker.
numbers = /#ticket-(\d+)-(\d+)/.match(link)
version_to_delete_url = "https://rails.lighthouseapp.com#{update_ticket_form.action}/versions/#{numbers[2]}"
page = agent.get(version_to_delete_url)
# begin
delete_form = locate_form_by_button(page, "Yes, delete this ticket comment.")
agent.submit(delete_form, delete_form.buttons.first)
# rescue
# next
# end
rescue Exception => e
p e.message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment