Class: KPM::NexusFacade::MavenCentralApiCalls
- Inherits:
-
NexusApiCallsV2
- Object
- NexusApiCallsV2
- KPM::NexusFacade::MavenCentralApiCalls
- Defined in:
- lib/kpm/nexus_helper/maven_central_api_calls.rb
Constant Summary collapse
- READ_TIMEOUT_DEFAULT =
60- OPEN_TIMEOUT_DEFAULT =
60- BASE_REPO_URL =
'https://repo1.maven.org/maven2'- SEARCH_API =
'https://search.maven.org/solrsearch/select'
Constants inherited from NexusApiCallsV2
NexusApiCallsV2::ERROR_MESSAGE_404
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#logger ⇒ Object
Returns the value of attribute logger.
Attributes inherited from NexusApiCallsV2
Instance Method Summary collapse
- #get_artifact_info(coordinates) ⇒ Object
-
#initialize(configuration, _ssl_verify, logger) ⇒ MavenCentralApiCalls
constructor
A new instance of MavenCentralApiCalls.
- #pull_artifact(coordinates, destination) ⇒ Object
- #search_for_artifacts(coordinates) ⇒ Object
Methods inherited from NexusApiCallsV2
#build_query_params, #get_artifact_info_endpoint, #pull_artifact_endpoint, #search_for_artifact_endpoint
Constructor Details
#initialize(configuration, _ssl_verify, logger) ⇒ MavenCentralApiCalls
Returns a new instance of MavenCentralApiCalls.
20 21 22 23 24 |
# File 'lib/kpm/nexus_helper/maven_central_api_calls.rb', line 20 def initialize(configuration, _ssl_verify, logger) @configuration = configuration @configuration[:url] ||= BASE_REPO_URL @logger = logger end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
14 15 16 |
# File 'lib/kpm/nexus_helper/maven_central_api_calls.rb', line 14 def configuration @configuration end |
#logger ⇒ Object
Returns the value of attribute logger.
15 16 17 |
# File 'lib/kpm/nexus_helper/maven_central_api_calls.rb', line 15 def logger @logger end |
Instance Method Details
#get_artifact_info(coordinates) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/kpm/nexus_helper/maven_central_api_calls.rb', line 101 def get_artifact_info(coordinates) coords = parse_coordinates(coordinates) version = coords[:version] if version.casecmp('latest').zero? version = fetch_latest_version(coords) coords = coords.merge(version: version) end _, versioned_artifact, coords = build_base_path_and_coords([coords[:group_id], coords[:artifact_id], coords[:extension], coords[:classifier], version].compact.join(':')) sha1 = get_sha1([coords[:group_id], coords[:artifact_id], coords[:extension], coords[:classifier], version].compact.join(':')) artifact_xml = '<artifact-resolution><data>' artifact_xml += '<presentLocally>true</presentLocally>' artifact_xml += "<groupId>#{coords[:group_id]}</groupId>" artifact_xml += "<artifactId>#{coords[:artifact_id]}</artifactId>" artifact_xml += "<version>#{coords[:version]}</version>" artifact_xml += "<extension>#{coords[:extension]}</extension>" artifact_xml += "<snapshot>#{!(coords[:version] =~ /-SNAPSHOT$/).nil?}</snapshot>" artifact_xml += "<sha1>#{sha1}</sha1>" artifact_xml += "<repositoryPath>/#{coords[:group_id].gsub('.', '/')}/#{versioned_artifact}</repositoryPath>" artifact_xml += '</data></artifact-resolution>' artifact_xml end |
#pull_artifact(coordinates, destination) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/kpm/nexus_helper/maven_central_api_calls.rb', line 123 def pull_artifact(coordinates, destination) artifact = parse_coordinates(coordinates) version = artifact[:version] version = fetch_latest_version(artifact) if version.casecmp('latest').zero? file_name = build_file_name(artifact[:artifact_id], version, artifact[:classifier], artifact[:extension]) download_url = build_download_url(artifact, version, file_name) dest_path = File.join(File.(destination || '.'), file_name) File.open(dest_path, 'wb') do |io| io.write(Net::HTTP.get(URI(download_url))) end { file_name: file_name, file_path: File.(dest_path), version: version, size: File.size(File.(dest_path)) } end |
#search_for_artifacts(coordinates) ⇒ Object
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 |
# File 'lib/kpm/nexus_helper/maven_central_api_calls.rb', line 26 def search_for_artifacts(coordinates) artifact = parse_coordinates(coordinates) params = { q: "g:\"#{artifact[:group_id]}\" AND a:\"#{artifact[:artifact_id]}\"", rows: 200, wt: 'json', core: 'gav' } query = params.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&') url = "#{SEARCH_API}?#{query}" response = Net::HTTP.get_response(URI(url)) raise "Search failed: #{response.code}" unless response.code.to_i == 200 json = JSON.parse(response.body) docs = json['response']['docs'] search_versions = docs.map { |doc| doc['v'] }.uniq # Apply when the artifact provided if artifact[:artifact_id] # Fetch metadata versions (incase the artifact is not indexed) = (artifact) = [] begin = Net::HTTP.get(URI()) if .nil? || .strip.empty? logger.debug { "Empty metadata response for #{artifact[:artifact_id]}" } else begin = REXML::Document.new() .elements.each('//versioning/versions/version') do |version_node| << version_node.text end rescue REXML::ParseException => e logger.debug { "Malformed XML in metadata for #{artifact[:artifact_id]}: #{e.message}" } end end rescue StandardError => e logger.debug { "Failed to fetch metadata for #{artifact[:artifact_id]}: #{e.message}" } end # Combine versions search_versions = (search_versions + ).uniq.sort_by do |v| begin Gem::Version.new(v) rescue ArgumentError v end end.reverse artifacts_xml = '<searchNGResponse><data>' search_versions.each do |version| artifacts_xml += '<artifact>' artifacts_xml += "<groupId>#{artifact[:group_id]}</groupId>" artifacts_xml += "<artifactId>#{artifact[:artifact_id]}</artifactId>" artifacts_xml += "<version>#{version}</version>" artifacts_xml += '<repositoryId>central</repositoryId>' artifacts_xml += '</artifact>' end else # Incase no artifact_id is provided for plugin search artifacts_xml = '<searchNGResponse><data>' docs.each do |doc| artifacts_xml += '<artifact>' artifacts_xml += "<groupId>#{doc['g']}</groupId>" artifacts_xml += "<artifactId>#{doc['a']}</artifactId>" artifacts_xml += "<version>#{doc['v']}</version>" artifacts_xml += '<repositoryId>central</repositoryId>' artifacts_xml += '</artifact>' end end artifacts_xml += '</data></searchNGResponse>' artifacts_xml end |