Created
April 22, 2010 18:33
-
-
Save subakva/375621 to your computer and use it in GitHub Desktop.
Mechanize script to bulk edit github hooks
This file contains 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
#! /usr/bin/env ruby | |
require 'rubygems' | |
require 'mechanize' | |
require 'logger' | |
projects = %w{ | |
project_name_1 | |
project_name_2 | |
} | |
username = '[Github Username Here]' | |
password = '[Github Password Here]' | |
service_name = 'Email' | |
services = { | |
'Email' => { | |
'service[active]' => '1', | |
'Email[address]' => '[Notification Email Here]' | |
}, | |
'Lighthouse' => { | |
'service[active]' => '1', | |
'Lighthouse[subdomain]' => '[Lighthouse Subdomain Here]', | |
'Lighthouse[project_id]' => '[Lighthouse Project ID Here]', | |
'Lighthouse[token]' => '[Lighthouse API Token Here]', | |
'Lighthouse[private]' => '1', | |
'Lighthouse[active]' => '1' | |
} | |
} | |
postreceive_urls = [ | |
'https://www.pivotaltracker.com/services/v3/github_commits?token=XXXXXXX' | |
] | |
agent = Mechanize.new { |a| | |
# a.log = Logger.new(STDERR) | |
} | |
agent.get('https://github.com/login') do |login_page| | |
my_page = login_page.form_with(:action => '/session') do |form| | |
form.login = username | |
form.password = password | |
end.submit | |
projects.each do |project| | |
# Add post-receive hooks | |
agent.transact do | |
puts "Adding post-receive urls for #{username}-#{project}..." | |
edit_page = agent.get("/#{username}/#{project}/edit") | |
edit_page.forms_with(:action => "/#{username}/#{project}/edit/postreceive_urls").each do |f| | |
postreceive_urls.each do |url| | |
fields = f.fields_with(:value => url) | |
f.add_field!('urls[]', url) if fields.size == 0 | |
end | |
result = f.submit | |
end | |
end | |
# Update service hooks | |
agent.transact do | |
edit_page = agent.get("/#{username}/#{project}/edit") | |
services.each_key do |service_name| | |
service_params = services[service_name] | |
puts "Updating #{service_name} for #{username}-#{project}..." | |
edit_page.forms_with(:action => "/#{username}/#{project}/edit/service").each do |f| | |
if f['service[name]'] == service_name | |
service_params.each { |name, value| f[name] = value } | |
result = f.submit | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment