Skip to content

Instantly share code, notes, and snippets.

@mackato
Created September 26, 2010 02:04
Show Gist options
  • Save mackato/597503 to your computer and use it in GitHub Desktop.
Save mackato/597503 to your computer and use it in GitHub Desktop.
Git post commit hook for JIRA.
#!/usr/bin/env ruby
require 'rubygems'
require 'jira4r'
$KCODE = 'u'
config = {}
log = {}
open("|git config --list | grep 'jira.*=*'").each_line do |line|
config[$1] = $2 if line =~ /jira\.(\w+)=([^\s]+)/
end
if config.keys.size < 4
puts "[ERROR] git-post-commit: JIRA Project config parameters are too few."
exit(1)
end
open("|git log -1 HEAD") do |io|
while (line = io.gets) != nil
case io.lineno
when 1
log[:commit] = line.split(/\s/)[1]
when 2
log[:Author] = $1 if line.match(/Author:\s+(.+)/)
when 3
log[:Date] = $1 if line.match(/Date:\s+(.+)/)
else
line.strip!
if io.lineno == 5 && line.match(/(#{config['project']}\-\d+)\s+\(([\.0-9]+)\)/)
log[:ticket] = $1
log[:time] = $2.to_f
end
(log[:message] ||= "") << line + $/ if line != ""
end
end
log[:message] << "commit: #{log[:commit]}"
end
unless log[:ticket] || log[:time]
puts "[ERROR] git-post-commit: JIRA ticket number or working time is not specified."
exit(1)
end
begin
hours = log[:time].floor
minutes = (log[:time] % 1 * 60).floor
start_date = Time.now - (hours * 60 * 60 + minutes * 60)
time_spent = "#{hours}h #{minutes}m"
jira = Jira4R::JiraTool.new(2, config['url'])
logger = Logger.new(STDERR)
logger.level = Logger::ERROR
jira.logger = logger
jira.login(config['user'], config['pass'])
# - (RemoteWorklog) initialize(author = nil, comment = nil, created = nil,
# groupLevel = nil, id = nil, roleLevelId = nil,
# startDate = nil, timeSpent = nil, timeSpentInSeconds = nil,
# updateAuthor = nil, updated = nil)
worklog = Jira4R::V2::RemoteWorklog.new(nil, log[:message], nil,
nil, nil, nil,
start_date, time_spent)
jira.call_driver("addWorklogAndAutoAdjustRemainingEstimate", log[:ticket], worklog)
rescue Exception => e
puts "[ERROR] git-post-commit: #{e.message}"
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment