Skip to content

Commit

Permalink
Merge branch 'feature/debug' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
janroesner committed Oct 28, 2011
2 parents ac68457 + 0360f5c commit 471e999
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 107 deletions.
51 changes: 5 additions & 46 deletions lib/batch_sender.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GitReport

class BatchSender
class BatchSender < GenericSender

# send the given commits chunked
def self.send! option = nil
Expand Down Expand Up @@ -36,6 +36,7 @@ def self.batches option

# sends the commit batch data to the server
def self.send_data! batch, options = nil
grlog(1, 'send_data started')
begin
response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(configuration.host, configuration.port) do |http|
request = Net::HTTP::Post.new(request_path options)
Expand All @@ -45,40 +46,11 @@ def self.send_data! batch, options = nil
http.read_timeout = configuration.timeout
http.request request unless GitReport.global_opts[:dry_run]
end
grlog(1, response ? "send_data responded with #{response.code}" : "send_data had no response")
raise GitReport::ServerError unless (response.code == "200" or response.code == "401") unless GitReport.global_opts[:dry_run]
rescue Exception => e
if e.is_a?(GitReport::ServerError)
puts "A server error occured during data transfer."
if GitReport.global_opts[:trace]
puts "Exception: #{e}\n"
puts "Message: #{JSON.parse(response.body)["message"]}\n"
else
puts "Run with --trace to get more info."
end
grlog(0 ,'send_data! - A server error occured during data transfer.')
grlog(0, "send_data! - Exception: #{e}")
grlog(0, "send_data! - Message: #{JSON.parse(response.body)["message"]}") if response
exit
else
puts "A client error occured during data transfer."
if GitReport.global_opts[:trace]
puts "Exception: #{e}\n"
e.backtrace.each do |line|
puts "#{line}\n"
end
else
puts "Run with --trace to get more info."
end
grlog(0, "send_data! - A client error occures during data transfer.")
grlog(0, "send_data! - Exception: #{e}")
grlog(0, "send_data! - Backtrace:")
e.backtrace.each do |line|
grlog(0, line)
end
exit
end

return false
communicate e, response
exit
end

true
Expand All @@ -93,24 +65,11 @@ def self.body batch
}.to_json
end

# returns configuration object
def self.configuration
@@configuration ||= GitReport.configuration
end

# returns the request path
def self.request_path options
@@path ||= "/v#{configuration.api_version}/projects"
end

# returns the default headers
def self.headers request
request['User-Agent'] = 'gitreport-client-ruby'
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['X-gitreport-Auth-Token'] = configuration.auth_token
end

end

end
4 changes: 2 additions & 2 deletions lib/logger.rb → lib/g_logger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GitReport

class Logger
class GLogger

# logs the given message in case the given level is less or equal the configured log level
def self.log level, message
Expand All @@ -13,7 +13,7 @@ def self.log level, message
# provides method grlog in every class
class << self.superclass
def grlog level, message
::GitReport::Logger.log level, Time.now.to_s + " - " + self.to_s + " #" + message
::GitReport::GLogger.log level, Time.now.to_s + " - " + self.to_s + " #" + message
end
end

Expand Down
44 changes: 44 additions & 0 deletions lib/generic_sender.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class GenericSender

# logs and puts the error according to its conditions and configuration
# TODO refactor me!
def self.communicate exception, response
both, cmd, log = [], [], []
if exception.is_a?(GitReport::ServerError)
@error_type = "server error"
@details = JSON.parse(response.body)["message"] rescue response.body
else
@error_type = "client error"
@details = exception.backtrace
end
both << "A #{@error_type} occured during data transfer."
both << "Exception: #{exception}"
if GitReport.global_opts[:trace]
cmd << @details
else
cmd << "Run with --trace to get more info."
end
log << @details
puts (both + cmd).join("\n")
(both + log).each{ |line| grlog(0, "send_data! #{line}")}
end

# returns local storage
def self.storage
@@storage ||= GitReport::Storage.new(ENV['HOME'], '.gitreport_storage')
end

# returns configuration object
def self.configuration
@@configuration ||= GitReport.configuration
end

# returns the default headers
def self.headers request
request['User-Agent'] = 'gitreport-client-ruby'
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['X-gitreport-Auth-Token'] = configuration.auth_token
end

end
5 changes: 4 additions & 1 deletion lib/gitreport.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'logger'
require 'g_logger'
require 'git'
require 'json'
require 'net/http'
Expand All @@ -11,6 +11,7 @@
require 'history'
require 'configuration'
require 'git_configuration'
require 'generic_sender'
require 'sender'
require 'batch_sender'
require 'storage'
Expand All @@ -19,6 +20,8 @@

module GitReport

@@global_opts ||= nil

class ServerError < StandardError;end

# mattr_reader
Expand Down
10 changes: 5 additions & 5 deletions lib/hook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def self.set_hook!

# returns true if the hook file already has a hook line in
def self.line_exists?
if file_content.match(/bundle\sexec\sgitreport\scommit\s&/)
if file_content.match(/nohup\sbundle\sexec\sgitreport\scommit\s>\s\/dev\/null\s2>\s\/dev\/null\s<\s\/dev\/null\s&/)
return true
end

Expand All @@ -76,15 +76,15 @@ def self.doc
"#!/bin/sh\n" +
"# This is a post-commit hook created by gitreport (http://gitreport.com)\n" +
"#\n" +
"# To remove it issue 'bundle exec unregister' in the projects main directory\n" +
"# To remove it issue 'bundle exec deactivate' in the projects main directory\n" +
"# In case the gitreport gem is not installed anymore, simply remove this hook file\n" +
"#\n" +
"# Be aware of other post commit hooks that my be mentioned here!\n"
end

# returns the line to activate gitreport via post commit hook
def self.line
"\nbundle exec gitreport commit &\n"
"\nnohup bundle exec gitreport commit > /dev/null 2> /dev/null < /dev/null &\n"
end

# removes the hook
Expand All @@ -105,7 +105,7 @@ def self.remove_hook_file!

# returns true if the hook file is ours and was not changed
def self.hook_file_unchanged?
Digest::SHA1.hexdigest(file_content) == "9c69e61ce35b8ce21968343411e6abeb89b237dd"
Digest::SHA1.hexdigest(file_content) == "e4032a91bb8e07e09ea637c803d8763e26e165e7"
end

# removes our hook line from hook file
Expand All @@ -115,7 +115,7 @@ def self.remove_line!

# removes our hook line from given content
def self.clean_up content
content.gsub(/\nbundle\sexec\sgitreport\scommit\s&\n/,'')
content.gsub(/\nnohup\sbundle\sexec\sgitreport\scommit\s>\s\/dev\/null\s2>\s\/dev\/null\s<\s\/dev\/null\s&\n/,'')
end

end
Expand Down
55 changes: 5 additions & 50 deletions lib/sender.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
module GitReport

class Sender
class Sender < GenericSender

# sends or saves the commits
def self.send! options = nil
begin
commits = GitReport::Supplier.commits(options)
rescue Exception => e
puts "A client error occured during data transfer."
if GitReport.global_opts[:trace]
puts "Exception: #{e}\n"
e.backtrace.each do |line|
puts "#{line}\n"
end
else
puts "Run with --trace to get more info."
end
grlog(0, "send! - A client error occures during data transfer.")
grlog(0, "send! - Exception: #{e}")
grlog(0, "send! - Backtrace:")
e.backtrace.each do |line|
grlog(0, line)
end
communicate e, nil
exit
end

Expand All @@ -37,6 +23,7 @@ def self.send! options = nil

# sends the commit data to the server
def self.send_data! commit, options = nil
grlog(1, 'send_data started')
begin
response = Net::HTTP.Proxy(configuration.proxy_host, configuration.proxy_port).start(configuration.host, configuration.port) do |http|
request = Net::HTTP::Post.new(request_path options)
Expand All @@ -46,53 +33,21 @@ def self.send_data! commit, options = nil
http.read_timeout = configuration.timeout
http.request request unless GitReport.global_opts[:dry_run]
end
grlog(1, response ? "send_data responded with #{response.code}" : "send_data had no response")
raise GitReport::ServerError unless (response.code == "201" or response.code == "401") unless GitReport.global_opts[:dry_run]
rescue Exception => e
if e.is_a?(GitReport::ServerError)
puts "A server error occured during data transfer."
if GitReport.global_opts[:trace]
puts "Exception: #{e}\n"
puts "Message: #{JSON.parse(response.body)["message"]}\n"
else
puts "Run with --trace to get more info."
end
else
puts "A client error occured during data transfer."
puts "Exception: #{e}\n"
end
grlog(0 ,'send_data! - A server error occured during data transfer.')
grlog(0, "send_data! - Exception: #{e}")
grlog(0, "send_data! - Message: #{JSON.parse(response.body)["message"]}") if response

communicate e, response
return false
end

true
end

# returns local storage
def self.storage
@@storage ||= GitReport::Storage.new(ENV['HOME'], '.gitreport_storage')
end

# returns configuration object
def self.configuration
@@configuration ||= GitReport.configuration
end

# returns the request path
def self.request_path options
@@path ||= "/v#{configuration.api_version}/commits"
end

# returns the default headers
def self.headers request
request['User-Agent'] = 'gitreport-client-ruby'
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['X-gitreport-Auth-Token'] = configuration.auth_token
end

end

end
6 changes: 3 additions & 3 deletions spec/models/hook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(File.exists?(hook_file)).should be_true
content = File.open(hook_file, 'r').read

content.match(/\nbundle\sexec\sgitreport\scommit\s&\n/).should be_true
content.match(/\nnohup\sbundle\sexec\sgitreport\scommit\s>\s\/dev\/null\s2>\s\/dev\/null\s<\s\/dev\/null\s&\n/).should be_true
end

it 'should insert the gitreport hook into an existing post-commit hook file' do
Expand All @@ -30,7 +30,7 @@

GitReport::Hook.set!
content = File.open(hook_file, 'r').read
content.match(/\nbundle\sexec\sgitreport\scommit\s&\n/).should be_true
content.match(/\nnohup\sbundle\sexec\sgitreport\scommit\s>\s\/dev\/null\s2>\s\/dev\/null\s<\s\/dev\/null\s&\n/).should be_true
end
end

Expand All @@ -57,7 +57,7 @@
(File.exists?(hook_file)).should be_true
content = File.open(hook_file, 'r').read
content.should match(/\ssome\spreexisting\shook\sfile\n/)
content.should_not match(/\nbundle\sexec\sgitreport\scommit\s&\n/)
content.should_not match(/\nnohup\sbundle\sexec\sgitreport\scommit\s>\s\/dev\/null\s2>\s\/dev\/null\s<\s\/dev\/null\s&\n/)
end
end
end
Expand Down

0 comments on commit 471e999

Please sign in to comment.