diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/action/custom.rb | 2 | ||||
-rw-r--r-- | lib/gitlab_net.rb | 14 | ||||
-rw-r--r-- | lib/http_helper.rb | 1 |
3 files changed, 11 insertions, 6 deletions
diff --git a/lib/action/custom.rb b/lib/action/custom.rb index ba0e650..c781f00 100644 --- a/lib/action/custom.rb +++ b/lib/action/custom.rb @@ -13,7 +13,7 @@ module Action class UnsuccessfulError < BaseError; end NO_MESSAGE_TEXT = 'No message'.freeze - DEFAULT_HEADERS = { 'Content-Type' => 'application/json' }.freeze + DEFAULT_HEADERS = { 'Content-Type' => CONTENT_TYPE_JSON }.freeze def initialize(gl_id, payload) @gl_id = gl_id diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 57ae452..b460f46 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -14,6 +14,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength class NotFound < StandardError; end CHECK_TIMEOUT = 5 + API_INACCESSIBLE_MESSAGE = 'API is not accessible'.freeze def check_access(cmd, gl_repository, repo, who, changes, protocol, env: {}) changes = changes.join("\n") unless changes.is_a?(String) @@ -33,12 +34,15 @@ class GitlabNet # rubocop:disable Metrics/ClassLength url = "#{internal_api_endpoint}/allowed" resp = post(url, params) - case resp.code - when HTTP_SUCCESS, HTTP_MULTIPLE_CHOICES, HTTP_UNAUTHORIZED, HTTP_NOT_FOUND - GitAccessStatus.create_from_json(resp.body, resp.code) - else - GitAccessStatus.new(false, resp.code, 'API is not accessible') + case resp + when Net::HTTPSuccess, Net::HTTPMultipleChoices, Net::HTTPUnauthorized, + Net::HTTPNotFound + if resp.content_type == CONTENT_TYPE_JSON + return GitAccessStatus.create_from_json(resp.body, resp.code) + end end + + GitAccessStatus.new(false, resp.code, API_INACCESSIBLE_MESSAGE) end def discover(who) diff --git a/lib/http_helper.rb b/lib/http_helper.rb index 55c504c..a7e7b23 100644 --- a/lib/http_helper.rb +++ b/lib/http_helper.rb @@ -6,6 +6,7 @@ module HTTPHelper include HTTPCodes READ_TIMEOUT = 300 + CONTENT_TYPE_JSON = 'application/json'.freeze protected |