Created
May 1, 2009 21:16
-
-
Save saratpediredla/105261 to your computer and use it in GitHub Desktop.
Mercurial Post Commit Hook for fixx
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 | |
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment