Class: Match::Storage::GitLab::Client

Inherits:
Object
  • Object
show all
Defined in:
match/lib/match/storage/gitlab/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_v4_url:, project_id:, job_token: nil, private_token: nil) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
# File 'match/lib/match/storage/gitlab/client.rb', line 11

def initialize(api_v4_url:, project_id:, job_token: nil, private_token: nil)
  @job_token      = job_token
  @private_token  = private_token
  @api_v4_url     = api_v4_url
  @project_id     = project_id

  UI.important("JOB_TOKEN and PRIVATE_TOKEN both defined, using JOB_TOKEN to execute this job.") if @job_token && @private_token
end

Instance Method Details

#authentication_keyObject



24
25
26
27
28
29
30
# File 'match/lib/match/storage/gitlab/client.rb', line 24

def authentication_key
  if @job_token
    return "JOB-TOKEN"
  elsif @private_token
    return "PRIVATE-TOKEN"
  end
end

#authentication_valueObject



32
33
34
35
36
37
38
# File 'match/lib/match/storage/gitlab/client.rb', line 32

def authentication_value
  if @job_token
    return @job_token
  elsif @private_token
    return @private_token
  end
end

#base_urlObject



20
21
22
# File 'match/lib/match/storage/gitlab/client.rb', line 20

def base_url
  return "#{@api_v4_url}/projects/#{CGI.escape(@project_id)}/secure_files"
end

#execute_request(url, request, target_file = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'match/lib/match/storage/gitlab/client.rb', line 78

def execute_request(url, request, target_file = nil)
  request[authentication_key] = authentication_value

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = url.instance_of?(URI::HTTPS)

  begin
    response = http.request(request)
  rescue Errno::ECONNREFUSED, SocketError => message
    UI.user_error!("GitLab connection error: #{message}")
  end

  unless response.kind_of?(Net::HTTPSuccess)
    handle_response_error(response, target_file)
  end

  response
end

#filesObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'match/lib/match/storage/gitlab/client.rb', line 40

def files
  @files ||= begin
    url = URI.parse(base_url)
    # 100 is maximum number of Secure files available on Gitlab https://docs.gitlab.com/ee/api/secure_files.html
    url.query = [url.query, "per_page=100"].compact.join('&')

    request = Net::HTTP::Get.new(url.request_uri)

    res = execute_request(url, request)

    data = []

    JSON.parse(res.body).each do |file|
      data << SecureFile.new(client: self, file: file)
    end

    data
  end
end

#find_file_by_name(name) ⇒ Object



60
61
62
# File 'match/lib/match/storage/gitlab/client.rb', line 60

def find_file_by_name(name)
  files.select { |secure_file| secure_file.file.name == name }.first
end

#handle_response_error(response, target_file = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'match/lib/match/storage/gitlab/client.rb', line 97

def handle_response_error(response, target_file = nil)
  error_prefix      = "GitLab storage error:"
  file_debug_string = "File: #{target_file}" unless target_file.nil?
  api_debug_string  = "API: #{@api_v4_url}"
  debug_info        = "(#{[file_debug_string, api_debug_string].compact.join(', ')})"

  begin
    parsed = JSON.parse(response.body)
  rescue JSON::ParserError
  end

  if parsed && parsed["message"] && (parsed["message"]["name"] == ["has already been taken"])
    error_handler = :error
    error = "#{target_file} already exists in GitLab project #{@project_id}, file not uploaded"
  else
    error_handler = :user_error!
    error = "#{response.code}: #{response.body}"
  end
  error_message = [error_prefix, error, debug_info].join(' ')

  UI.send(error_handler, error_message)
end

#prompt_for_access_tokenObject



120
121
122
123
124
# File 'match/lib/match/storage/gitlab/client.rb', line 120

def prompt_for_access_token
  unless authentication_key
    @private_token = UI.input("Please supply a GitLab personal or project access token: ")
  end
end

#upload_file(current_file, target_file) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'match/lib/match/storage/gitlab/client.rb', line 64

def upload_file(current_file, target_file)
  url = URI.parse(base_url)

  File.open(current_file) do |file|
    request = Net::HTTP::Post::Multipart.new(
      url.path,
      "file" => UploadIO.new(file, "application/octet-stream"),
      "name" => target_file
    )

    execute_request(url, request, target_file)
  end
end