Welcome to mirror list, hosted at ThFree Co, Russian Federation.

client.rb « harbor « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06142ae2b4097bd00d6fd3579f014e12c4a607b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

module Gitlab
  module Harbor
    class Client
      Error = Class.new(StandardError)
      ConfigError = Class.new(Error)

      attr_reader :integration

      def initialize(integration)
        raise ConfigError, 'Please check your integration configuration.' unless integration

        @integration = integration
      end

      def ping
        options = { headers: headers.merge!('Accept': 'text/plain') }
        response = Gitlab::HTTP.get(url('ping'), options)

        { success: response.success? }
      end

      private

      def url(path)
        Gitlab::Utils.append_path(base_url, path)
      end

      def base_url
        Gitlab::Utils.append_path(integration.url, '/api/v2.0/')
      end

      def headers
        auth = Base64.strict_encode64("#{integration.username}:#{integration.password}")
        {
          'Content-Type': 'application/json',
          'Authorization': "Basic #{auth}"
        }
      end
    end
  end
end