-
Notifications
You must be signed in to change notification settings - Fork 367
/
bintray.rb
497 lines (425 loc) · 13.9 KB
/
bintray.rb
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
require 'json'
require 'net/http'
require "uri"
require 'find'
module DPL
class Provider
class Bintray < Provider
DEFAULT_URL = 'https://api.bintray.com'
def check_auth
@user ||= option(:user)
@key ||= option(:key)
@file ||= option(:file)
end
def needs_key?
false
end
def url
@url ||= URI.parse(options[:url] || DEFAULT_URL)
end
attr_accessor :test_mode
attr_reader :user, :key, :file
attr_reader :passphrase
attr_reader :dry_run
attr_reader :descriptor
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
def read_descriptor
log "Reading descriptor file: #{file}"
@descriptor = JSON.parse(File.read(file))
end
def descriptor=(json)
@descriptor = JSON.parse(json)
end
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
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
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
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
def package_exists_path
package = descriptor["package"]
subject = package["subject"]
name = package["name"]
repo = package["repo"]
return "/packages/#{subject}/#{repo}/#{name}"
end
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
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
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
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
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
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
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
def check_and_create_package
if !package_exists?
create_package
end
end
def check_and_create_version
if !version_exists?
create_version
end
end
def upload_files
files = files_to_upload
files.each do |key, artifact|
upload_file(artifact)
end
end
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
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
# Get the root path from which to start collecting files to be
# uploaded to Bintray.
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
# 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.
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
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
# Returns a map containing Artifact objects.
# The map contains the files to be uploaded to Bintray.
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
def push_app
read_descriptor
check_and_create_package
check_and_create_version
upload_files
gpg_sign_version
publish_version
end
# Copies a key from one map to another, if the key exists there.
def add_to_map(to_map, from_map, key)
if !from_map[key].nil?
to_map[key] = from_map[key]
end
end
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
def log(msg)
puts "[Bintray Upload] #{msg}"
end
# This class represents an artifact (file) to be uploaded to Bintray.
class Artifact
def initialize(local_path, upload_path, matrix_params)
@local_path = local_path
@upload_path = upload_path
@matrix_params = matrix_params
end
def hash
return @localPath.hash
end
def eql?(other)
@localPath == other.local_path
end
attr_reader :local_path
attr_reader :upload_path
attr_reader :matrix_params
end
# Used to return the path and body of REST requests sent to Bintray.
# Used for testing.
class RequestDetails
def initialize(path, body)
@path = path
@body = body
end
attr_reader :path
attr_reader :body
end
end
end
end