Skip to content

Commit

Permalink
Improve Hook logs
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Feb 24, 2015
1 parent a5312e0 commit cbb6f00
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 21 deletions.
9 changes: 9 additions & 0 deletions contrib/hooks/post-receive/lib/git_hosting_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ def debug_mode?
end


def loglevel
if debug_mode?
'debug'
else
'info'
end
end


def post_data
post_data = {}
post_data['clear_time'] = clear_time
Expand Down
41 changes: 26 additions & 15 deletions contrib/hooks/post-receive/lib/git_hosting_custom_hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ class CustomHook

attr_reader :repo_path
attr_reader :refs
attr_reader :git_config


def initialize(repo_path, refs)
@repo_path = repo_path
@refs = refs
@repo_path = repo_path
@refs = refs
@git_config = Config.new
end


def exec
## Execute extra hooks
extra_hooks = get_extra_hooks
if !extra_hooks.empty?
logger("Calling additional post-receive hooks...")
logger.info("Calling additional post-receive hooks...")
call_extra_hooks(extra_hooks)
logger.info('')
end
end

Expand All @@ -26,18 +29,26 @@ def exec

def get_extra_hooks
# Get global extra hooks
logger('Looking for additional global post-receive hooks...')
logger.debug('Looking for additional global post-receive hooks...')
global_extra_hooks = get_executables('hooks/post-receive.d')
logger(' - No global hooks found') if global_extra_hooks.empty?
if global_extra_hooks.empty?
logger.debug(' - No global hooks found')
else
logger.debug(" - Global hooks found : #{global_extra_hooks}")
end

logger('')
logger.debug('')

# Get local extra hooks
logger('Looking for additional local post-receive hooks...')
logger.debug('Looking for additional local post-receive hooks...')
local_extra_hooks = get_executables('hooks/post-receive.local.d')
logger(' - No local hooks found') if local_extra_hooks.empty?
if local_extra_hooks.empty?
logger.debug(' - No local hooks found')
else
logger.debug(" - Local hooks found : #{local_extra_hooks}")
end

logger('')
logger.debug('')

global_extra_hooks + local_extra_hooks
end
Expand All @@ -64,24 +75,24 @@ def get_executables(directory)
def call_extra_hooks(extra_hooks)
# Call each exectuble found with the parameters we got
extra_hooks.each do |extra_hook|
logger(" - Executing extra hook '#{extra_hook}'")
logger.info(" - Executing extra hook '#{extra_hook}'")

IO.popen("#{extra_hook}", "w+") do |pipe|
begin
pipe.puts refs
pipe.close_write
logger("#{pipe.read}")
logger.info("#{pipe.read}")
rescue => e
logger("Error while executing hook #{extra_hook}")
logger("#{e.message}")
logger.error("Error while executing hook #{extra_hook}")
logger.error("#{e.message}")
end
end
end
end


def logger(message)
puts "#{message}\n"
def logger
@logger ||= GitHosting::HookLogger.new(loglevel: git_config.loglevel)
end

end
Expand Down
37 changes: 37 additions & 0 deletions contrib/hooks/post-receive/lib/git_hosting_hook_logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module GitHosting
class HookLogger

attr_reader :loglevel


def initialize(opts = {})
@loglevel = opts.delete(:loglevel){ 'info' }
end


def debug(message)
write(message) if loglevel == 'debug'
end


def info(message)
write(message)
end


def error(message)
write(message)
end


private


def write(message)
$stdout.sync = true
$stdout.puts "\e[1G#{message}"
$stdout.flush
end

end
end
17 changes: 11 additions & 6 deletions contrib/hooks/post-receive/lib/git_hosting_post_receive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ def exec


def notify_redmine
logger('')
logger("Notifying Redmine about changes to this repository : '#{git_config.repository_name}' ...")
logger.info('')
logger.info("Notifying Redmine about changes to this repository : '#{git_config.repository_name}' ...")
logger.info('')

opts = {}
opts[:params] = http_post_data
Expand All @@ -35,14 +36,18 @@ def notify_redmine
http.request(request) do |response|
if response.code.to_i == 200
response.read_body do |body_frag|
logger(body_frag)
logger.info(body_frag)
end
else
logger.error(" - Error while notifying Redmine ! (status code: #{response.code})")
end
end
rescue => e
logger("HTTP_ERROR : #{e.message}")
logger.error("HTTP_ERROR : #{e.message}")
end
end

logger.info('')
end


Expand All @@ -61,8 +66,8 @@ def parsed_refs
end


def logger(message)
puts "#{message}\n"
def logger
@logger ||= GitHosting::HookLogger.new(loglevel: git_config.loglevel)
end

end
Expand Down
1 change: 1 addition & 0 deletions contrib/hooks/post-receive/redmine_gitolite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
repo_path = Dir.pwd

require_relative 'lib/git_hosting/http_helper'
require_relative 'lib/git_hosting/hook_logger'
require_relative 'lib/git_hosting/config'
require_relative 'lib/git_hosting/post_receive'
require_relative 'lib/git_hosting/custom_hook'
Expand Down
7 changes: 7 additions & 0 deletions lib/load_gitolite_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
executable false
end

gitolite_hook do
name 'git_hosting_hook_logger.rb'
source 'post-receive/lib/git_hosting_hook_logger.rb'
destination 'lib/git_hosting/hook_logger.rb'
executable false
end

gitolite_hook do
name 'git_hosting_post_receive.rb'
source 'post-receive/lib/git_hosting_post_receive.rb'
Expand Down

0 comments on commit cbb6f00

Please sign in to comment.