Class: DPL::Provider::Bintray

Inherits:
DPL::Provider show all
Defined in:
lib/dpl/provider/bintray.rb

Defined Under Namespace

Classes: Artifact, RequestDetails

Instance Attribute Summary collapse

Attributes inherited from DPL::Provider

#context, #options

Instance Method Summary collapse

Methods inherited from DPL::Provider

apt_get, #check_app, #cleanup, #commit_msg, context, #create_key, #default_text_charset, #default_text_charset?, #detect_encoding?, #encoding_for, #error, experimental, new, npm_g, #option, pip, requires, #run, #setup_git_credentials, #setup_git_ssh, #sha, shell, #uncleanup, #user_agent, #warn

Constructor Details

#initialize(*args) ⇒ Bintray

Returns a new instance of Bintray.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dpl/provider/bintray.rb', line 25

def initialize(*args)
  super(*args)
  @test_mode = false
  @user = options[:user]
  @key = options[:key]
  @url = options[:url]
  @file = options[:file]
  @passphrase = options[:passphrase]
  @dry_run = options[:dry_run]

  if @user.nil?
    abort("The 'user' argument is required")
  end
  if @key.nil?
    abort("The 'key' argument is required")
  end
  if @file.nil?
    abort("The 'file' argument is required")
  end
  if @url.nil?
    @url = 'https://api.bintray.com'
  end
  if @dry_run.nil?
    @dry_run = false
  end
end

Instance Attribute Details

#descriptorObject

Returns the value of attribute descriptor.



23
24
25
# File 'lib/dpl/provider/bintray.rb', line 23

def descriptor
  @descriptor
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



22
23
24
# File 'lib/dpl/provider/bintray.rb', line 22

def dry_run
  @dry_run
end

#fileObject (readonly)

Returns the value of attribute file.



19
20
21
# File 'lib/dpl/provider/bintray.rb', line 19

def file
  @file
end

#keyObject (readonly)

Returns the value of attribute key.



18
19
20
# File 'lib/dpl/provider/bintray.rb', line 18

def key
  @key
end

#passphraseObject (readonly)

Returns the value of attribute passphrase.



20
21
22
# File 'lib/dpl/provider/bintray.rb', line 20

def passphrase
  @passphrase
end

#test_modeObject

Returns the value of attribute test_mode.



16
17
18
# File 'lib/dpl/provider/bintray.rb', line 16

def test_mode
  @test_mode
end

#urlObject (readonly)

Returns the value of attribute url.



21
22
23
# File 'lib/dpl/provider/bintray.rb', line 21

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



17
18
19
# File 'lib/dpl/provider/bintray.rb', line 17

def user
  @user
end

Instance Method Details

#add_if_matches(map, path, include_pattern, exclude_pattern, upload_pattern, matrix_params) ⇒ Object



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/dpl/provider/bintray.rb', line 401

def add_if_matches(map, path, include_pattern, exclude_pattern, upload_pattern, matrix_params)
  res = path.match(/#{include_pattern}/)

  # If the file matches the include pattern and it is not a directory.
  # In case test_mode is set, we do not check if the file exists.
  if !res.nil? && (test_mode || File.file?(path))
    # If the file does not match the exclude pattern.
    if exclude_pattern.nil? || exclude_pattern.empty? || !path.match(/#{exclude_pattern}/)
      # Using the capturing groups in the include pattern, replace the $1, $2, ...
      # in the upload pattern.
      groups = res.captures
      replaced_upload_pattern = upload_pattern
      for i in 0..groups.size-1
        replaced_upload_pattern = replaced_upload_pattern.gsub("$#{i+1}", groups[i])
      end
      map[path] = Artifact.new(path, replaced_upload_pattern, matrix_params)
    end
  end
end

#add_package_attributesObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/dpl/provider/bintray.rb', line 225

def add_package_attributes
  package = descriptor["package"]
  repo = package["repo"]
  subject = package["subject"]
  package_name = package["name"]
  attributes = package["attributes"]
  path = nil
  if !attributes.nil?
    log "Adding attributes for package '#{package_name}'..."
    path = "/packages/#{subject}/#{repo}/#{package_name}/attributes"
    if !dry_run
      res = post_request(path, attributes)
      log_bintray_response(res)
    end
  end
  RequestDetails.new(path, attributes)
end

#add_to_map(to_map, from_map, key) ⇒ Object

Copies a key from one map to another, if the key exists there.



452
453
454
455
456
# File 'lib/dpl/provider/bintray.rb', line 452

def add_to_map(to_map, from_map, key)
  if !from_map[key].nil?
    to_map[key] = from_map[key]
  end
end

#add_version_attributesObject



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/dpl/provider/bintray.rb', line 279

def add_version_attributes
  package = descriptor["package"]
  package_name = package["name"]
  subject = package["subject"]
  version = descriptor["version"]
  version_name = version["name"]
  repo = package["repo"]
  attributes = version["attributes"]
  path = nil
  if !attributes.nil?
    log "Adding attributes for version '#{version_name}'..."
    path = "/packages/#{subject}/#{repo}/#{package_name}/versions/#{version_name}/attributes"
    if !dry_run
      res = post_request(path, attributes)
      log_bintray_response(res)
    end
  end
  RequestDetails.new(path, attributes)
end

#check_and_create_packageObject



299
300
301
302
303
# File 'lib/dpl/provider/bintray.rb', line 299

def check_and_create_package
  if !package_exists?
    create_package
  end
end

#check_and_create_versionObject



305
306
307
308
309
# File 'lib/dpl/provider/bintray.rb', line 305

def check_and_create_version
  if !version_exists?
    create_version
  end
end

#check_authObject



9
10
# File 'lib/dpl/provider/bintray.rb', line 9

def check_auth
end

#create_packageObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/dpl/provider/bintray.rb', line 189

def create_package
  package = descriptor["package"]
  repo = package["repo"]
  body = {}

  add_to_map(body, package, "name")
  add_to_map(body, package, "desc")
  add_to_map(body, package, "licenses")
  add_to_map(body, package, "labels")
  add_to_map(body, package, "vcs_url")
  add_to_map(body, package, "website_url")
  add_to_map(body, package, "issue_tracker_url")
  add_to_map(body, package, "public_download_numbers")
  add_to_map(body, package, "public_stats")

  subject = package["subject"]
  package_name = package["name"]
  log "Creating package '#{package_name}'..."

  path = "/packages/#{subject}/#{repo}"
  if !dry_run
    res = post_request(path, body)
    log_bintray_response(res)
    code = res.code.to_i
  else
    code = 200
  end

  if !test_mode
    if code == 201 || code == 200
      add_package_attributes
    end
  end
  RequestDetails.new(path, body)
end

#create_versionObject



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/dpl/provider/bintray.rb', line 243

def create_version
  package = descriptor["package"]
  version = descriptor["version"]
  repo = package["repo"]
  body = {}

  add_to_map(body, version, "name")
  add_to_map(body, version, "desc")
  add_to_map(body, version, "released")
  add_to_map(body, version, "vcs_tag")
  add_to_map(body, version, "github_release_notes_file")
  add_to_map(body, version, "github_use_tag_release_notes")
  add_to_map(body, version, "attributes")

  package_name = package["name"]
  subject = package["subject"]
  version_name = version["name"]
  log "Creating version '#{version_name}'..."

  path = "/packages/#{subject}/#{repo}/#{package_name}/versions"
  if !dry_run
    res = post_request(path, body)
    log_bintray_response(res)
    code = res.code.to_i
  else
    code = 200
  end

  if !test_mode
    if code == 201 || code == 200
      add_version_attributes
    end
  end
  RequestDetails.new(path, body)
end

#deployObject



442
443
444
445
446
447
448
449
# File 'lib/dpl/provider/bintray.rb', line 442

def deploy
  read_descriptor
  check_and_create_package
  check_and_create_version
  upload_files
  gpg_sign_version
  publish_version
end

#files_to_uploadObject

Returns a map containing Artifact objects. The map contains the files to be uploaded to Bintray.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/dpl/provider/bintray.rb', line 423

def files_to_upload
  upload_files = Hash.new()
  files = descriptor["files"]
  if files.nil?
    return upload_files
  end

  files.each { |patterns|
    fill_files_map(
        upload_files,
        patterns["includePattern"],
        patterns["excludePattern"],
        patterns["uploadPattern"],
        patterns["matrixParams"])
  }

  return upload_files
end

#fill_files_map(map, include_pattern, exclude_pattern, upload_pattern, matrix_params) ⇒ Object

Fills a map with Artifact objects which match the include pattern and do not match the exclude pattern. The artifacts are files collected from the file system.



388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/dpl/provider/bintray.rb', line 388

def fill_files_map(map, include_pattern, exclude_pattern, upload_pattern, matrix_params)
  # Get the root path from which to start collecting the files.
  root_path = root_path(include_pattern)
  if root_path.nil?
    return
  end

  # Start scanning the root path recursively.
  Find.find(root_path) do |path|
    add_if_matches(map, path, include_pattern, exclude_pattern, upload_pattern, matrix_params)
  end
end

#gpg_sign_versionObject



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/dpl/provider/bintray.rb', line 339

def gpg_sign_version
  version = descriptor["version"]
  gpg_sign = version["gpgSign"]
  if gpg_sign
    package = descriptor["package"]
    repo = package["repo"]
    package_name = package["name"]
    subject = package["subject"]
    version_name = version["name"]

    body = nil
    if !passphrase.nil?
      log "Signing version with no passphrase..."
      body = {}
      body["passphrase"] = passphrase
    else
      log "Signing version with passphrase..."
    end

    path = "/gpg/#{subject}/#{repo}/#{package_name}/versions/#{version_name}"
    if !dry_run
      res = post_request(path, body)
      log_bintray_response(res)
    end
    RequestDetails.new(path, body)
  end
end

#head_request(path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dpl/provider/bintray.rb', line 61

def head_request(path)
  url = URI.parse(self.url)
  req = Net::HTTP::Head.new(path)
  req.basic_auth user, key

  sock = Net::HTTP.new(url.host, url.port)
  sock.use_ssl = true
  res = sock.start {|http| http.request(req) }

  return res
end

#log(msg) ⇒ Object



471
472
473
# File 'lib/dpl/provider/bintray.rb', line 471

def log(msg)
  puts "[Bintray Upload] #{msg}"
end

#log_bintray_response(res) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/dpl/provider/bintray.rb', line 458

def log_bintray_response(res)
  msg = ''
  if !res.body.nil?
    begin
      response = JSON.parse(res.body)
      msg = response["message"]
    rescue
    end
  end

  log "Bintray response: #{res.code.to_i} #{res.message}. #{msg}"
end

#needs_key?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/dpl/provider/bintray.rb', line 12

def needs_key?
  false
end

#package_exists?Boolean

Returns:

  • (Boolean)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dpl/provider/bintray.rb', line 138

def package_exists?
  path = package_exists_path
  if !dry_run
    res = head_request(path)
    code = res.code.to_i
  else
    code = 404
  end

  if code == 404
    return false
  end
  if code == 201 || code == 200
    return true
  end
  name = descriptor["package"]["name"]
  abort("Unexpected HTTP response code #{code} returned from Bintray while checking if package '#{name}' exists. " +
            "Response message: #{res.message}")
end

#package_exists_pathObject



130
131
132
133
134
135
136
# File 'lib/dpl/provider/bintray.rb', line 130

def package_exists_path
  package = descriptor["package"]
  subject = package["subject"]
  name = package["name"]
  repo = package["repo"]
  return "/packages/#{subject}/#{repo}/#{name}"
end

#post_request(path, body) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dpl/provider/bintray.rb', line 73

def post_request(path, body)
  req = Net::HTTP::Post.new(path)
  req.add_field('Content-Type', 'application/json')
  req.basic_auth user, key
  if !body.nil?
    req.body = body.to_json
  end

  url = URI.parse(self.url)
  sock = Net::HTTP.new(url.host, url.port)
  sock.use_ssl = true
  res = sock.start {|http| http.request(req) }
  return res
end

#publish_versionObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/dpl/provider/bintray.rb', line 319

def publish_version
  publish = descriptor["publish"]
  if publish
    package = descriptor["package"]
    version = descriptor["version"]
    repo = package["repo"]
    package_name = package["name"]
    subject = package["subject"]
    version_name = version["name"]

    log "Publishing version '#{version_name}' of package '#{package_name}'..."
    path = "/content/#{subject}/#{repo}/#{package_name}/#{version_name}/publish"
    if !dry_run
      res = post_request(path, nil)
      log_bintray_response(res)
    end
  end
  RequestDetails.new(path, nil)
end

#put_file_request(local_file_path, upload_path, matrix_params) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dpl/provider/bintray.rb', line 88

def put_file_request(local_file_path, upload_path, matrix_params)
  url = URI.parse(self.url)

  file = File.open(local_file_path, 'rb')
  data = file.read()
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true

  params = ''
  if !matrix_params.nil?
    matrix_params.each do |key, val|
      params << ";#{key}=#{val}"
    end
    upload_path << params
  end

  request = Net::HTTP::Put.new("#{upload_path}")
  request.basic_auth user, key
  request.body = data

  return http.request(request)
end

#read_descriptorObject



52
53
54
55
# File 'lib/dpl/provider/bintray.rb', line 52

def read_descriptor
  log "Reading descriptor file: #{file}"
  @descriptor = JSON.parse(File.read(file))
end

#root_path(str) ⇒ Object

Get the root path from which to start collecting files to be uploaded to Bintray.



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/dpl/provider/bintray.rb', line 369

def root_path(str)
  index = str.index('(')
  path = nil
  if index.nil? || str.start_with?('(')
    path = str
  else
    path = str[0, index]
  end

  if !test_mode && !File.exist?(path)
    log "Warning: Path: #{path} does not exist."
    return nil
  end
  return path
end

#upload_file(artifact) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dpl/provider/bintray.rb', line 111

def upload_file(artifact)
  log "Uploading file '#{artifact.local_path}' to #{artifact.upload_path}"

  if dry_run
    return
  end

  package = descriptor["package"]
  version = descriptor["version"]
  package_name = package["name"]
  subject = package["subject"]
  repo = package["repo"]
  version_name = version["name"]

  path = "/content/#{subject}/#{repo}/#{package_name}/#{version_name}/#{artifact.upload_path}"
  res = put_file_request(artifact.local_path, path, artifact.matrix_params)
  log_bintray_response(res)
end

#upload_filesObject



311
312
313
314
315
316
317
# File 'lib/dpl/provider/bintray.rb', line 311

def upload_files
  files = files_to_upload

  files.each do |key, artifact|
    upload_file(artifact)
  end
end

#version_exists?Boolean

Returns:

  • (Boolean)


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/dpl/provider/bintray.rb', line 169

def version_exists?
  path = version_exists_path
  if !dry_run
    res = head_request(path)
    code = res.code.to_i
  else
    code = 404
  end

  if code == 404
    return false
  end
  if code == 201 || code == 200
    return true
  end
  version_name = descriptor["version"]["name"]
  abort("Unexpected HTTP response code #{code} returned from Bintray while checking if version '#{version_name}' exists. " +
            "Response message: #{res.message}")
end

#version_exists_pathObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/dpl/provider/bintray.rb', line 158

def version_exists_path
  package = descriptor["package"]
  version = descriptor["version"]
  package_name = package["name"]
  subject = package["subject"]
  repo = package["repo"]
  version_name = version["name"]

  return "/packages/#{subject}/#{repo}/#{package_name}/versions/#{version_name}"
end