Skip to content

Commit cbb6f00

Browse files
author
root
committed
Improve Hook logs
1 parent a5312e0 commit cbb6f00

6 files changed

+91
-21
lines changed

Diff for: contrib/hooks/post-receive/lib/git_hosting_config.rb

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ def debug_mode?
7070
end
7171

7272

73+
def loglevel
74+
if debug_mode?
75+
'debug'
76+
else
77+
'info'
78+
end
79+
end
80+
81+
7382
def post_data
7483
post_data = {}
7584
post_data['clear_time'] = clear_time

Diff for: contrib/hooks/post-receive/lib/git_hosting_custom_hook.rb

+26-15
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ class CustomHook
33

44
attr_reader :repo_path
55
attr_reader :refs
6+
attr_reader :git_config
67

78

89
def initialize(repo_path, refs)
9-
@repo_path = repo_path
10-
@refs = refs
10+
@repo_path = repo_path
11+
@refs = refs
12+
@git_config = Config.new
1113
end
1214

1315

1416
def exec
1517
## Execute extra hooks
1618
extra_hooks = get_extra_hooks
1719
if !extra_hooks.empty?
18-
logger("Calling additional post-receive hooks...")
20+
logger.info("Calling additional post-receive hooks...")
1921
call_extra_hooks(extra_hooks)
22+
logger.info('')
2023
end
2124
end
2225

@@ -26,18 +29,26 @@ def exec
2629

2730
def get_extra_hooks
2831
# Get global extra hooks
29-
logger('Looking for additional global post-receive hooks...')
32+
logger.debug('Looking for additional global post-receive hooks...')
3033
global_extra_hooks = get_executables('hooks/post-receive.d')
31-
logger(' - No global hooks found') if global_extra_hooks.empty?
34+
if global_extra_hooks.empty?
35+
logger.debug(' - No global hooks found')
36+
else
37+
logger.debug(" - Global hooks found : #{global_extra_hooks}")
38+
end
3239

33-
logger('')
40+
logger.debug('')
3441

3542
# Get local extra hooks
36-
logger('Looking for additional local post-receive hooks...')
43+
logger.debug('Looking for additional local post-receive hooks...')
3744
local_extra_hooks = get_executables('hooks/post-receive.local.d')
38-
logger(' - No local hooks found') if local_extra_hooks.empty?
45+
if local_extra_hooks.empty?
46+
logger.debug(' - No local hooks found')
47+
else
48+
logger.debug(" - Local hooks found : #{local_extra_hooks}")
49+
end
3950

40-
logger('')
51+
logger.debug('')
4152

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

6980
IO.popen("#{extra_hook}", "w+") do |pipe|
7081
begin
7182
pipe.puts refs
7283
pipe.close_write
73-
logger("#{pipe.read}")
84+
logger.info("#{pipe.read}")
7485
rescue => e
75-
logger("Error while executing hook #{extra_hook}")
76-
logger("#{e.message}")
86+
logger.error("Error while executing hook #{extra_hook}")
87+
logger.error("#{e.message}")
7788
end
7889
end
7990
end
8091
end
8192

8293

83-
def logger(message)
84-
puts "#{message}\n"
94+
def logger
95+
@logger ||= GitHosting::HookLogger.new(loglevel: git_config.loglevel)
8596
end
8697

8798
end
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module GitHosting
2+
class HookLogger
3+
4+
attr_reader :loglevel
5+
6+
7+
def initialize(opts = {})
8+
@loglevel = opts.delete(:loglevel){ 'info' }
9+
end
10+
11+
12+
def debug(message)
13+
write(message) if loglevel == 'debug'
14+
end
15+
16+
17+
def info(message)
18+
write(message)
19+
end
20+
21+
22+
def error(message)
23+
write(message)
24+
end
25+
26+
27+
private
28+
29+
30+
def write(message)
31+
$stdout.sync = true
32+
$stdout.puts "\e[1G#{message}"
33+
$stdout.flush
34+
end
35+
36+
end
37+
end

Diff for: contrib/hooks/post-receive/lib/git_hosting_post_receive.rb

+11-6
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ def exec
2424

2525

2626
def notify_redmine
27-
logger('')
28-
logger("Notifying Redmine about changes to this repository : '#{git_config.repository_name}' ...")
27+
logger.info('')
28+
logger.info("Notifying Redmine about changes to this repository : '#{git_config.repository_name}' ...")
29+
logger.info('')
2930

3031
opts = {}
3132
opts[:params] = http_post_data
@@ -35,14 +36,18 @@ def notify_redmine
3536
http.request(request) do |response|
3637
if response.code.to_i == 200
3738
response.read_body do |body_frag|
38-
logger(body_frag)
39+
logger.info(body_frag)
3940
end
41+
else
42+
logger.error(" - Error while notifying Redmine ! (status code: #{response.code})")
4043
end
4144
end
4245
rescue => e
43-
logger("HTTP_ERROR : #{e.message}")
46+
logger.error("HTTP_ERROR : #{e.message}")
4447
end
4548
end
49+
50+
logger.info('')
4651
end
4752

4853

@@ -61,8 +66,8 @@ def parsed_refs
6166
end
6267

6368

64-
def logger(message)
65-
puts "#{message}\n"
69+
def logger
70+
@logger ||= GitHosting::HookLogger.new(loglevel: git_config.loglevel)
6671
end
6772

6873
end

Diff for: contrib/hooks/post-receive/redmine_gitolite.rb

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
repo_path = Dir.pwd
88

99
require_relative 'lib/git_hosting/http_helper'
10+
require_relative 'lib/git_hosting/hook_logger'
1011
require_relative 'lib/git_hosting/config'
1112
require_relative 'lib/git_hosting/post_receive'
1213
require_relative 'lib/git_hosting/custom_hook'

Diff for: lib/load_gitolite_hooks.rb

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
executable false
4343
end
4444

45+
gitolite_hook do
46+
name 'git_hosting_hook_logger.rb'
47+
source 'post-receive/lib/git_hosting_hook_logger.rb'
48+
destination 'lib/git_hosting/hook_logger.rb'
49+
executable false
50+
end
51+
4552
gitolite_hook do
4653
name 'git_hosting_post_receive.rb'
4754
source 'post-receive/lib/git_hosting_post_receive.rb'

0 commit comments

Comments
 (0)