Created
June 29, 2009 18:06
-
-
Save evanmcd/137727 to your computer and use it in GitHub Desktop.
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
#!C:\ruby\bin | |
require 'yaml' | |
require 'cgi' | |
require 'net/http' | |
require 'uri' | |
# configure multiple project settings below | |
SVNLOOK = `E:\Subversion\bin\svnlook.exe' | |
LOG_FILE = 'C:\temp\svn-hooks.log' | |
OPTIONS = { | |
# default token | |
:token => 'tokenwashere', | |
# tokens for other members of the team | |
# name is matched against svn user name | |
:users => { 'bob' => "bob's token", 'fred' => "fred's token" }, | |
# full url, please | |
:account => 'http://company.lighthouseapp.com', | |
:project => 00000, # REPLACE | |
#:prefix => /^trunk/ # OPTIONAL | |
} | |
def gather_and_post(repo_path, revision, options) | |
if options[:prefix] | |
commit_dirs_changed = `#{SVNLOOK} dirs-changed #{repo_path} -r #{revision}` | |
return unless commit_dirs_changed.split(/\n/)[0] =~ options[:prefix] | |
end | |
commit_author = `#{SVNLOOK} author #{repo_path} -r #{revision}`.chop | |
commit_log = `#{SVNLOOK} log #{repo_path} -r #{revision}` | |
commit_date = `#{SVNLOOK} date #{repo_path} -r #{revision}` | |
commit_changed = `#{SVNLOOK} changed #{repo_path} -r #{revision}` | |
commit_changes = commit_changed.split("\n").inject([]) do |memo, line| | |
if line.strip =~ /(\w)\s+(.*)/ | |
memo << [$1, $2] | |
end | |
end.to_yaml | |
changeset_xml = <<-END_XML | |
<changeset> | |
<title>#{CGI.escapeHTML("%s committed changeset [%d]" % [commit_author, revision])}</title> | |
<body>#{CGI.escapeHTML(commit_log)}</body> | |
<changes>#{CGI.escapeHTML(commit_changes)}</changes> | |
<revision>#{CGI.escapeHTML(revision.to_s)}</revision> | |
<changed-at type="datetime">#{CGI.escapeHTML(commit_date.split('(').first.strip)}</changed-at> | |
</changeset> | |
END_XML | |
token = options[:users][commit_author] || options[:token] | |
url = URI.parse('%s/projects/%d/changesets.xml' % [options[:account], options[:project]]) | |
req = Net::HTTP::Post.new(url.path) | |
req.basic_auth token, 'x' # to ensure authentication | |
req.body = changeset_xml.strip | |
req.set_content_type('application/xml') | |
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) } | |
case res | |
when Net::HTTPSuccess, Net::HTTPRedirection | |
## all good, we submitted... | |
else | |
res.error! | |
end | |
end | |
begin | |
# feel free to add multiple calls below | |
gather_and_post ARGV[0], ARGV[1], OPTIONS | |
rescue | |
%x{echo "repo:#{ARGV[0]} rev: #{ARGV[1]}" > #{LOG_FILE}} | |
%x{echo "Error: #{$!.to_s.gsub('`',"'")} trace:#{caller}" >> #{LOG_FILE}} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment