|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'tempfile' |
| 4 | +require 'English' |
| 5 | +require 'puppetfile-resolver/util' |
| 6 | +require 'puppetfile-resolver/spec_searchers/common' |
| 7 | +require 'puppetfile-resolver/spec_searchers/git_configuration' |
| 8 | +require 'puppetfile-resolver/util' |
| 9 | +require 'uri' |
| 10 | + |
| 11 | +module PuppetfileResolver |
| 12 | + module SpecSearchers |
| 13 | + module Git |
| 14 | + module GClone |
| 15 | + # @summary clones the remote url and reads the metadata file |
| 16 | + # @returns [String] the content of the metadata file |
| 17 | + def self.metadata(puppetfile_module, resolver_ui, config) |
| 18 | + repo_url = puppetfile_module.remote |
| 19 | + |
| 20 | + unless PuppetfileResolver::Util.git? |
| 21 | + resolver_ui.debug { 'Git executible not found, unable to use git clone resolution' } |
| 22 | + |
| 23 | + return nil |
| 24 | + end |
| 25 | + return nil if repo_url.nil? |
| 26 | + return nil unless valid_http_url?(repo_url) |
| 27 | + |
| 28 | + metadata_file = 'metadata.json' |
| 29 | + |
| 30 | + ref = puppetfile_module.ref || |
| 31 | + puppetfile_module.tag || |
| 32 | + puppetfile_module.commit || |
| 33 | + puppetfile_module.branch || |
| 34 | + 'HEAD' |
| 35 | + |
| 36 | + resolver_ui.debug { "Querying git repository #{repo_url}" } |
| 37 | + |
| 38 | + clone_and_read_file(repo_url, ref, metadata_file, config) |
| 39 | + end |
| 40 | + |
| 41 | + # @summary clones the git url and reads the file at the given ref |
| 42 | + # a temp directory will be created and then destroyed during |
| 43 | + # the cloning and reading process |
| 44 | + # @param ref [String] the git ref, branch, commit, tag |
| 45 | + # @param file [String] the file you wish to read |
| 46 | + # @returns [String] the content of the file |
| 47 | + def self.clone_and_read_file(url, ref, file, config) |
| 48 | + clone_cmd = ['git', 'clone', '--bare', '--depth=1', '--single-branch'] |
| 49 | + err_msg = '' |
| 50 | + if config.git.proxy |
| 51 | + err_msg += " with proxy #{config.git.proxy}: " |
| 52 | + proxy = "--config \"http.proxy=#{config.git.proxy}\" --config \"https.proxy=#{config.proxy}\"" |
| 53 | + clone_cmd.push(proxy) |
| 54 | + end |
| 55 | + |
| 56 | + Dir.mktmpdir(nil, config.git.clone_dir) do |dir| |
| 57 | + clone_cmd.push("--branch=#{ref}") if ref != 'HEAD' |
| 58 | + clone_cmd.push(url, dir) |
| 59 | + out, err_out, process = ::PuppetfileResolver::Util.run_command(clone_cmd) |
| 60 | + err_msg += out |
| 61 | + raise err_msg unless process.success? |
| 62 | + Dir.chdir(dir) do |
| 63 | + content, err_out, process = ::PuppetfileResolver::Util.run_command(['git', 'show', "#{ref}:#{file}"]) |
| 64 | + raise 'InvalidContent' unless process.success? && content.length > 2 |
| 65 | + return content |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def self.valid_http_url?(url) |
| 71 | + # uri does not work with git urls, return true |
| 72 | + return true if url.start_with?('git@') |
| 73 | + |
| 74 | + uri = URI.parse(url) |
| 75 | + uri.is_a?(URI::HTTP) && !uri.host.nil? |
| 76 | + rescue URI::InvalidURIError |
| 77 | + false |
| 78 | + end |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | +end |
0 commit comments