class KPM::NexusFacade::MavenCentralApiCalls
def search_for_artifacts(coordinates)
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) metadata_url = build_metadata_url(artifact) metadata_versions = [] begin metadata_response = Net::HTTP.get(URI(metadata_url)) if metadata_response.nil? || metadata_response.strip.empty? logger.debug { "Empty metadata response for #{artifact[:artifact_id]}" } else begin metadata_xml = REXML::Document.new(metadata_response) metadata_xml.elements.each('//versioning/versions/version') do |version_node| metadata_versions << 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 + metadata_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