Class: DPL::Provider::Bintray

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

Defined Under Namespace

Classes: Artifact, RequestDetails

Constant Summary collapse

DEFAULT_URL =
'https://api.bintray.com'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Bintray

Returns a new instance of Bintray.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dpl/provider/bintray.rb', line 31

def initialize(*args)
  super(*args)
  @test_mode = false

  @passphrase = options[:passphrase]
  @dry_run = options[:dry_run]

  if @dry_run.nil?
    @dry_run = false
  end
end

Instance Attribute Details

#descriptorObject

Returns the value of attribute descriptor.



29
30
31
# File 'lib/dpl/provider/bintray.rb', line 29

def descriptor
  @descriptor
end

#dry_runObject (readonly)

Returns the value of attribute dry_run.



28
29
30
# File 'lib/dpl/provider/bintray.rb', line 28

def dry_run
  @dry_run
end

#fileObject (readonly)

Returns the value of attribute file.



26
27
28
# File 'lib/dpl/provider/bintray.rb', line 26

def file
  @file
end

#keyObject (readonly)

Returns the value of attribute key.



26
27
28
# File 'lib/dpl/provider/bintray.rb', line 26

def key
  @key
end

#passphraseObject (readonly)

Returns the value of attribute passphrase.



27
28
29
# File 'lib/dpl/provider/bintray.rb', line 27

def passphrase
  @passphrase
end

#test_modeObject

Returns the value of attribute test_mode.



25
26
27
# File 'lib/dpl/provider/bintray.rb', line 25

def test_mode
  @test_mode
end

#userObject (readonly)

Returns the value of attribute user.



26
27
28
# File 'lib/dpl/provider/bintray.rb', line 26

def user
  @user
end

Instance Method Details

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



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/dpl/provider/bintray.rb', line 389

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/dpl/provider/bintray.rb', line 213

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.



440
441
442
443
444
# File 'lib/dpl/provider/bintray.rb', line 440

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



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/dpl/provider/bintray.rb', line 267

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



287
288
289
290
291
# File 'lib/dpl/provider/bintray.rb', line 287

def check_and_create_package
  if !package_exists?
    create_package
  end
end

#check_and_create_versionObject



293
294
295
296
297
# File 'lib/dpl/provider/bintray.rb', line 293

def check_and_create_version
  if !version_exists?
    create_version
  end
end

#check_authObject



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

def check_auth
  @user ||= option(:user)
  @key  ||= option(:key)
  @file ||= option(:file)
end

#create_packageObject



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/dpl/provider/bintray.rb', line 177

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



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/dpl/provider/bintray.rb', line 231

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

#files_to_uploadObject

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



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/dpl/provider/bintray.rb', line 411

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.



376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/dpl/provider/bintray.rb', line 376

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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/dpl/provider/bintray.rb', line 327

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



52
53
54
55
56
57
58
59
60
61
# File 'lib/dpl/provider/bintray.rb', line 52

def head_request(path)
  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



459
460
461
# File 'lib/dpl/provider/bintray.rb', line 459

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

#log_bintray_response(res) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/dpl/provider/bintray.rb', line 446

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)


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

def needs_key?
  false
end

#package_exists?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dpl/provider/bintray.rb', line 126

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



118
119
120
121
122
123
124
# File 'lib/dpl/provider/bintray.rb', line 118

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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dpl/provider/bintray.rb', line 63

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

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

#publish_versionObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/dpl/provider/bintray.rb', line 307

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

#push_appObject



430
431
432
433
434
435
436
437
# File 'lib/dpl/provider/bintray.rb', line 430

def push_app
  read_descriptor
  check_and_create_package
  check_and_create_version
  upload_files
  gpg_sign_version
  publish_version
end

#put_file_request(local_file_path, upload_path, matrix_params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dpl/provider/bintray.rb', line 77

def put_file_request(local_file_path, upload_path, matrix_params)

  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



43
44
45
46
# File 'lib/dpl/provider/bintray.rb', line 43

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.



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/dpl/provider/bintray.rb', line 357

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dpl/provider/bintray.rb', line 99

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



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

def upload_files
  files = files_to_upload

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

#urlObject



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

def url
  @url ||= URI.parse(options[:url] || DEFAULT_URL)
end

#version_exists?Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dpl/provider/bintray.rb', line 157

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



146
147
148
149
150
151
152
153
154
155
# File 'lib/dpl/provider/bintray.rb', line 146

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