diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action/custom.rb | 51 | ||||
-rw-r--r-- | lib/gitlab_access_status.rb | 3 | ||||
-rw-r--r-- | lib/gitlab_shell.rb | 5 | ||||
-rw-r--r-- | lib/http_codes.rb | 8 | ||||
-rw-r--r-- | lib/http_helper.rb | 3 |
5 files changed, 34 insertions, 36 deletions
diff --git a/lib/action/custom.rb b/lib/action/custom.rb index 328a12f..a2f3d59 100644 --- a/lib/action/custom.rb +++ b/lib/action/custom.rb @@ -22,20 +22,15 @@ module Action def execute validate! - result = process_api_endpoints - - if result && HTTP_SUCCESS_CODES.include?(result.code) - result - else - raise_unsuccessful!(result) - end + inform_client(info_message) if info_message + process_api_endpoints! end private attr_reader :gl_id, :payload - def process_api_endpoints + def process_api_endpoints! output = '' resp = nil @@ -46,7 +41,14 @@ module Action json = { 'data' => data_with_gl_id, 'output' => output } resp = post(url, {}, headers: DEFAULT_HEADERS, options: { json: json }) - return resp unless HTTP_SUCCESS_CODES.include?(resp.code) + + # Net::HTTPSuccess is the parent of Net::HTTPOK, Net::HTTPCreated etc. + case resp + when Net::HTTPSuccess, Net::HTTPMultipleChoices + true + else + raise_unsuccessful!(resp) + end begin body = JSON.parse(resp.body) @@ -54,8 +56,6 @@ module Action raise UnsuccessfulError, 'Response was not valid JSON' end - inform_client(body['message']) if body['message'] - print_flush(body['result']) # In the context of the git push sequence of events, it's necessary to read @@ -78,6 +78,10 @@ module Action data['api_endpoints'] end + def info_message + data['info_message'] + end + def config @config ||= GitlabConfig.new end @@ -97,7 +101,11 @@ module Action end def inform_client(str) - $stderr.puts(str) + $stderr.puts(format_gitlab_output(str)) + end + + def format_gitlab_output(str) + str.split("\n").map { |line| "> GitLab: #{line}" }.join("\n") end def validate! @@ -120,16 +128,17 @@ module Action end def raise_unsuccessful!(result) - message = begin - body = JSON.parse(result.body) - message = body['message'] - message = Base64.decode64(body['result']) if !message && body['result'] && !body['result'].empty? - message ? message : NO_MESSAGE_TEXT - rescue JSON::ParserError - NO_MESSAGE_TEXT - end + message = "#{exception_message_for(result.body)} (#{result.code})" + raise UnsuccessfulError, format_gitlab_output(message) + end + + def exception_message_for(body) + body = JSON.parse(body) + return body['message'] unless body['message'].to_s.empty? - raise UnsuccessfulError, "#{message} (#{result.code})" + body['result'].to_s.empty? ? NO_MESSAGE_TEXT : Base64.decode64(body['result']) + rescue JSON::ParserError + NO_MESSAGE_TEXT end end end diff --git a/lib/gitlab_access_status.rb b/lib/gitlab_access_status.rb index 8483863..dd6562e 100644 --- a/lib/gitlab_access_status.rb +++ b/lib/gitlab_access_status.rb @@ -1,8 +1,7 @@ require 'json' -require_relative 'http_codes' class GitAccessStatus - include HTTPCodes + HTTP_MULTIPLE_CHOICES = '300'.freeze attr_reader :message, :gl_repository, :gl_id, :gl_username, :gitaly, :git_protocol, :git_config_options, :payload diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb index 79af861..57c70f5 100644 --- a/lib/gitlab_shell.rb +++ b/lib/gitlab_shell.rb @@ -95,8 +95,9 @@ class GitlabShell # rubocop:disable Metrics/ClassLength $stderr.puts "GitLab: Invalid repository path" false rescue Action::Custom::BaseError => ex - $logger.warn('Custom action error', command: origin_cmd, user: log_username) - $stderr.puts "GitLab: #{ex.message}" + $logger.warn('Custom action error', exception: ex.class, message: ex.message, + command: origin_cmd, user: log_username) + $stderr.puts ex.message false end diff --git a/lib/http_codes.rb b/lib/http_codes.rb deleted file mode 100644 index 5f79095..0000000 --- a/lib/http_codes.rb +++ /dev/null @@ -1,8 +0,0 @@ -module HTTPCodes - HTTP_SUCCESS = '200'.freeze - HTTP_CREATED = '201'.freeze - HTTP_MULTIPLE_CHOICES = '300'.freeze - HTTP_UNAUTHORIZED = '401'.freeze - HTTP_NOT_FOUND = '404'.freeze - HTTP_SUCCESS_CODES = [HTTP_SUCCESS, HTTP_CREATED, HTTP_MULTIPLE_CHOICES].freeze -end diff --git a/lib/http_helper.rb b/lib/http_helper.rb index ac18f49..b2a6211 100644 --- a/lib/http_helper.rb +++ b/lib/http_helper.rb @@ -1,10 +1,7 @@ -require_relative 'http_codes' require_relative 'httpunix' require_relative 'gitlab_logger' module HTTPHelper - include HTTPCodes - READ_TIMEOUT = 300 CONTENT_TYPE_JSON = 'application/json'.freeze |