require 'rubygems'
require 'net/http'
require 'builder'
require 'cgi'
# Created by Damian Nicholson
# Product Manager
# hedgehoglab
# damian.nicholson@hedgehoglab.com
#
# NOTE: THIS SCRIPT IT TAILORED TO WORKING WITH SUBVERSION AND FIXX VERSION 1.7
# This script needs to ran as part of the post commit hook which is found in the hooks directory(/path_to_repository/repository/hooks/) of your
# Subersion repository.
# Copy the original template file called "post-commit.tmpl" and rename it to just "post-commit"
# Make this file execuatable (chmod +x post-commit)
# Making sure to keep it in the same directory as the other hook templates.
# Edit the post-commit file you've just made executable making sure to comment out the following lines at the bottom as these are merely examples
# of how your script can be run
# commit-email.pl "$REPOS" "$REV" commit-watchers@example.org
# log-commit.py --repository "$REPOS" --revision "$REV"
# Copy and paste in this line, making sure to change the path to where your script is located
# ruby /path_to_script/postCommitHook.rb $1 $2
# We recommend placing this script in the hooks directory of your repository, you also need to make this file executable (chmod +x postCommitHook.rb)
# Messages are only logged in fixx if they contain the ID of the issue they're referring to - it must be prefixed with a hashtag
# e.g. svn commit -m '#12 CSS Tweaks'
# SVNLOOK - Path to wherever your installation of subversion is
SVNLOOK = "/usr/local/bin/svnlook"
REPOS_PATH = ARGV[0]
REVISION = ARGV[1]
# IMPORTANT - Change the contents of this to suit your configuration
DETAILS = {
# The url of your installation of fixx with port number if necessary
:path => '0.0.0.0',
:port => 8080,
# FIXX AUTHENTICATION
# You can put all your usernames and passwords in this hash though it is not recommended. You may wish to do so
# if you want the Subversion messages to created by the person who commited the revision in Subversion
# We have created a new user called svnbot specifically for this task who feeds all new messages into fixx as the
# logs will show who originally changed the files anyway.
:users => { 'svnbot' => 'password' }
}
# Path, revision and details are all passed in automatically, no need to change params for this function
def postToFixx(path, revision, details)
# Running of svnlook commands, change to suit your respective SCM
commit_author = `#{SVNLOOK} author #{path} -r #{revision}`
commit_log = `#{SVNLOOK} log #{path} -r #{revision}`
commit_changed = `#{SVNLOOK} changed #{path} -r #{revision}`
# Regex to search for #issueId
regex = /#\d+/
# Scans the commit message for #issueId tag
issue_id = commit_log.scan(regex)
if !issue_id.empty?
Net::HTTP.start(details[:path], details[:port]) { |http|
commit_changes = commit_changed.inject([]) do |array, value|
array << value.to_s + "\n"
array
end
# Strips the hash tag
stripped_issue = issue_id.to_s.delete('#').to_i
req = Net::HTTP::Post.new("/api/issues/#{stripped_issue}/comments")
# We use the same user for all messages hence why we're using only first entry
# It is possible to do some subversion username - fixx username comparison here if you opted down the multiple user avenue
req.basic_auth details[:users].keys[0], details[:users].values[0]
req.set_content_type('application/xml')
# Creating the xml to be posted to fixx
xml = <<-END_XML
<comment>
<text>Commited by: #{CGI.escapeHTML(commit_author)} \n Commit message: #{CGI.escapeHTML(commit_log.gsub!(regex, ''))} \n Files changed: \n\n #{commit_changes}</text>
<createdAt>#{DateTime.now.to_s}</createdAt>
</comment>
END_XML
req.body = xml.strip
res = http.request(req)
if res.is_a? Net::HTTPSuccess
# OK
else
return false
end
}
end
end
postToFixx(REPOS_PATH, REVISION, DETAILS)