public
Last active

Mercurial Post Commit Hook for fixx

  • Download Gist
hg-fixx-post-commit-hook
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#!/usr/bin/env ruby
 
# Created by Sarat Pediredla
# Partner
# hedgehog lab
#
# NOTE: THIS SCRIPT IT TAILORED TO WORKING WITH MERCURIAL AND FIXX VERSION 1.7
#
# INSTALLATION INSTRUCTIONS :
#
# 1. Add the following lines to .hg/hgrc in your Mercurial repository. Create the hgrc file if it does not exist. Make sure you 'chmod +x' it.
# [hooks]
# commit = hg-fixx-post-commit-hook
#
# 2. Copy and paste the code in this file to a file called hg-fixx-post-commit-hook and put the file anywhere on your path where it can be run from
# the command line.
#
# 3. Configure the options in the file including your fixx URL.
#
# 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. hg commit -m'#12 CSS Tweaks'
 
require 'rubygems'
require 'net/http'
require 'cgi'
require 'yaml'
 
 
HG = `which hg`.strip
REVISION = `#{HG} log -l 1 | sed -e 's/changeset://' -e '2,$d'`.chomp
DETAILS = {
:path => '0.0.0.0',
:port => 8080,
:username => 'username',
:password => 'password'
}
 
def hgCommitTofixx(revision, details)
commit_author = `#{HG} log -r #{revision} --template '{author|person}' | sed q`.chomp
commit_message = `#{HG} log -r #{revision} --template '{desc}' | sed q`.chomp
commit_date = `#{HG} log -r #{revision} --template '{date|isodate}' | sed q`.chomp
commit_files_changed = `#{HG} log -r #{revision} --template '{files}' | sed -n '$p'`
commit_files_add = `#{HG} log -r #{revision} --template '{file_adds}' | sed -n '$p'`
commit_files_del = `#{HG} log -r #{revision} --template '{file_dels}' | sed -n '$p'`
commit_changed = commit_files_changed + " " + commit_files_add + " " + commit_files_del
# Regex to search for #issueId
regex = /#(\d)+/
# Scans the commit message for #issueId tag
issue_id = commit_message.scan(regex)
if !issue_id.empty?
Net::HTTP.start(details[:path], details[:port]) { |http|
commit_changes = commit_changed.split(" ").inject([]) do |array, value|
if !value.to_s.empty?
array << value.to_s.to_yaml + "\n"
end
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")
req.basic_auth details[:username], details[:password]
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_message.gsub!(regex, 'issue@\1'))} \n
Files changed: \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
 
hgCommitTofixx(REVISION, DETAILS)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.