Created
July 25, 2010 22:19
-
-
Save zerowidth/489939 to your computer and use it in GitHub Desktop.
git post-receive hook for pivotal tracker
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 | |
# git post-receive hook that posts the log messages to pivotal tracker | |
# for automatic updates of stories. | |
# See http://www.pivotaltracker.com/help/api?version=v3#scm_post_commit | |
# for more information | |
require "cgi" | |
begin | |
require "patron" | |
rescue LoadError | |
begin | |
require "rubygems" | |
require "patron" | |
rescue LoadError | |
puts "pivotal tracker notification requires the patron gem" | |
exit 0 | |
end | |
end | |
ZERO = "0" * 40 | |
if ENV["PIVOTAL_TRACKER_TOKEN"] | |
api_token = ENV["PIVOTAL_TRACKER_TOKEN"] | |
else | |
puts "set PIVOTAL_TRACKER_TOKEN in the environment to enable notification" | |
exit 0 | |
end | |
STDIN.readlines.each do |line| | |
oldrev, newrev, refname = line.strip.split(" ") | |
next if newrev == ZERO # deleting something | |
# brand new branch | |
range = oldrev == ZERO ? newrev : "#{oldrev}..#{newrev}" | |
revisions = %x(git rev-list --pretty="%h%n%an%n%s%n%b%n" #{range}).split("\n\n\n") | |
puts "notifying pivotal tracker of changes..." | |
patron = Patron::Session.new | |
patron.base_url = "http://www.pivotaltracker.com" | |
patron.headers["X-TrackerToken"] = api_token | |
patron.headers["Content-type"] = "application/xml" | |
revisions.each do |revision| | |
revision = revision.split("\n", 4) | |
revision[0] # "commit HASH" | |
sha1 = revision[1] # abbreviated sha1 | |
author = revision[2] # author name | |
message = revision[3] # commit message subject and body, newline-separated | |
xml = "<source_commit>" | |
xml << "<commit_id>" << sha1 << "</commit_id>" | |
xml << "<author>" << CGI::escapeHTML(author) << "</author>" | |
xml << "<message>" << CGI::escapeHTML(message) << "</message>" | |
# xml << "<url>" << ... << "</url>" | |
xml << "</source_commit>" | |
r = patron.post("/services/v3/source_commits", xml) | |
puts r.status_line unless r.status < 400 | |
end | |
end | |
exit 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment