lib/bukin/providers/jenkins.rb in bukin-0.5.0 vs lib/bukin/providers/jenkins.rb in bukin-0.6.0

- old
+ new

@@ -1,38 +1,40 @@ require 'json' # Api for downloading from jenkins class Bukin::Jenkins - - def usable(data) - !!data[:jenkins] - end + attr_reader :data - def url(data) - data[:jenkins] + def initialize(data) + @data = data end - def resolve_info(data) + def resolve_info name = data[:name] version = data[:version] || 'lastSuccessfulBuild' - if version =~ /^build-(.*)$/ - base_path = "#{data[:jenkins]}/job/#{name}/#{$1}" - url = "#{base_path}/api/json" - info = JSON.parse(open(url).read) - - download_info = if data[:file] - info['artifacts'].find do |artifact| - artifact['fileName'] =~ data[:file] - end - else - info['artifacts'].first - end - - data[:version] = data[:display_version] = version - data[:download] = "#{base_path}/artifact/#{download_info['relativePath']}" - else + unless /^build-(.*)$/.match(version) raise Bukin::InstallError, "The plugin #{name} (#{version}) has an improper version format for downloading from Jenkins. It should be in the form of 'build-<number>'" end + + base_path = "#{data[:jenkins]}/job/#{name}/#{$1}" + url = "#{base_path}/api/json" + info = JSON.parse(open(url).read) + + download_info = find_file(info['artifacts'], data[:file]) + data[:version] = version + data[:download] = "#{base_path}/artifact/#{download_info['relativePath']}" data + end + + def find_file(artifacts, file_name) + artifacts.find do |artifact| + if file_name.is_a?(Regexp) + file_name =~ artifact['fileName'] + elsif file_name.is_a?(String) + file_name == artifact['fileName'] + else + true + end + end end end