class WssAgent::Specifications

Public Class Methods

check_policies(options = {}) click to toggle source

checking dependencies that they conforms with company policy.

@param (see Specifications#specs)

# File lib/wss_agent/specifications.rb, line 100
def check_policies(options = {})
  wss_client = WssAgent::Client.new
  result = wss_client.check_policies(
    WssAgent::Specifications.list(options),
    options
  )
  if result.success?
    WssAgent.logger.debug result.data
    puts result.message
  else
    WssAgent.logger.debug "check policies errors occur: #{result.status}, message: #{result.message}, data: #{result.data}"
    ap "error: #{result.status}/#{result.data}", color: { string: :red }
  end

  result
end
check_policies_for_update(options = {}) click to toggle source

Check policies before update

@param (see Specifications#specs)

# File lib/wss_agent/specifications.rb, line 84
def check_policies_for_update(options = {})
  return Struct.new(:status).new(:ok) unless check_policy?(options)
  wss_client = WssAgent::Client.new
  policy_results = wss_client.check_policies(
    WssAgent::Specifications.list(options), options
  )
  if policy_results.success? && policy_results.policy_violations?
    puts policy_results.message
    return Struct.new(:status, :msg).new(:reject, policy_results.message)
  end
  Struct.new(:status).new(:ok)
end
gem_dependencies(list, gem_dependencies, options = {}) click to toggle source

Get all dependencies includes development

@param [Array<Spec>] array for gems @param [Array<Dependencies>] @param [Hash] @options options [String] :excludes list gems to exclude

@return [Array<Spec>] list

# File lib/wss_agent/specifications.rb, line 125
def gem_dependencies(list, gem_dependencies, options = {})
  gem_dependencies.each do |gd|
    if options['excludes'] && options['excludes'].to_s.split(',').include?(gd.name)
      next
    end

    gs = gd.matching_specs.first
    if gs
      unless list[gs.name]
        list[gs.name] = gs
        unless gs.dependencies.empty?
          list = gem_dependencies(list, gs.dependencies, options)
        end
      end
    else
      unless list[gd.name]
        list[gd.name] = Gem::Specification.new(
          gd.name,
          gd.requirements_list.last.scan(/[\d\.\w]+/).first
        )
        rm_dep = remote_dependencies(gd.name, gd.requirements_list.last)
        unless rm_dep.empty?
          list = gem_dependencies(list, rm_dep, options)
        end
      end
    end
  end

  list
end
list(options = {}) click to toggle source

Display list dependencies

@param (see Specifications#specs)

# File lib/wss_agent/specifications.rb, line 39
def list(options = {})
  new(specs(options)).call
end
new(gem_specs) click to toggle source
# File lib/wss_agent/specifications.rb, line 179
def initialize(gem_specs)
  @gem_specs = gem_specs
end
remote_dependencies(gem_name, _version) click to toggle source

Load dependencies from rubygems

@param gem_name [String] name gem @params version [String] version gem

@return [Array<Gem::Dependency>] list gem dependencies

# File lib/wss_agent/specifications.rb, line 162
def remote_dependencies(gem_name, _version)
  conn = Faraday.new(url: 'https://rubygems.org') do |h|
    h.headers[:content_type] = 'application/x-www-form-urlencoded'
    h.request :url_encoded
    h.adapter :excon
  end
  response = conn.get("/api/v1/gems/#{gem_name}.json")
  dep_list = MultiJson.load(response.body)
  dep_list['dependencies'].values.flatten.map do |j|
    Gem::Dependency.new(
      j['name'],
      Gem::Requirement.new(j['requirements'].split(','))
    )
  end
end
specs(options = {}) click to toggle source

Get dependencies

@param [Hash] @option options [Boolean] 'all' if true then get all dependencies (include development dependencies) @option options [String] 'excludes' list gem name which need to exclude from end list

# File lib/wss_agent/specifications.rb, line 12
def specs(options = {})
  list_gems = Bundler::Definition.build(
    Bundler.default_gemfile,
    Bundler.default_lockfile,
    false
  )
  list_gems = list_gems.resolve.materialize(
    list_gems.send(
      Bundler.settings[:cache_all_platforms] ? :dependencies :  :requested_dependencies
    )
  )
  if options['all']
    # get all gems
    list = {}
    list_gems.each { |j| list[j.name] = j }
    list_gems.each { |j|
      list = gem_dependencies(list, j.dependencies, options)
    }

    list_gems = list.values
  end
  list_gems
end
update(options = {}) click to toggle source

Send gem list to server

@param (see Specifications#specs)

# File lib/wss_agent/specifications.rb, line 60
def update(options = {})
  result = check_policies_for_update(options)
  if !force_update?(options) && (result.status == :reject)
    return Struct.new(:status) do
      alias_method :success?, :status
    end.new(false)
  end

  wss_client = WssAgent::Client.new
  result = wss_client.update(WssAgent::Specifications.list(options))
  if result.success?
    WssAgent.logger.debug result.data
    puts result.message
  else
    WssAgent.logger.debug "synchronization errors occur: status: #{result.status}, message: #{result.message}, data: #{result.data}"
    ap "error: #{result.status}/#{result.data}", color: { string: :red }
  end

  result
end

Private Class Methods

check_policy?(options = {}) click to toggle source
# File lib/wss_agent/specifications.rb, line 43
def check_policy?(options = {})
  options['force'] ||
    WssAgent::Configure['check_policies'] ||
    WssAgent::Configure['force_check_all_dependencies']
end
force_update?(options = {}) click to toggle source
# File lib/wss_agent/specifications.rb, line 50
def force_update?(options = {})
  options['force-update'] ||
    WssAgent::Configure['force_update']
end

Public Instance Methods

call() click to toggle source
# File lib/wss_agent/specifications.rb, line 183
def call
  @gem_specs.map do |spec|
    next if spec.name == WssAgent::NAME
    gem_item(spec)
  end.compact
end
gem_item(spec) click to toggle source
# File lib/wss_agent/specifications.rb, line 190
def gem_item(spec)
  {
    'groupId' => spec.name,
    'artifactId' => spec.file_name,
    'version' => spec.version.to_s,
    'sha1' => GemSha1.new(spec).sha1,
    'optional' => false,
    'children' => [],
    'exclusions' => []
  }
end