Module: Diffend::Voting::Versions::Remote

Defined in:
lib/diffend/voting/versions/remote.rb

Overview

Module responsible for fetching safe/malicious votes for current or current/new versions of gems

Class Method Summary collapse

Class Method Details

.build_diffend(project_id) ⇒ Hash

Build diffend information

Parameters:

  • project_id (String, nil)

    diffend project_id

Returns:

  • (Hash)


77
78
79
80
81
82
83
84
85
# File 'lib/diffend/voting/versions/remote.rb', line 77

def build_diffend(project_id)
  {
    'api_version' => API_VERSION,
    'environment' => build_diffend_environment,
    'project_id' => project_id,
    'type' => PLATFORM_TYPE,
    'version' => Diffend::VERSION
  }.freeze
end

.build_diffend_environmentString

Build diffend environment information

Returns:

  • (String)


90
91
92
# File 'lib/diffend/voting/versions/remote.rb', line 90

def build_diffend_environment
  ENV['DIFFEND_ENV'] || 'development'
end

.build_hostHash

Build host information

Returns:

  • (Hash)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/diffend/voting/versions/remote.rb', line 142

def build_host
  uname = Etc.uname

  {
    'command' => build_host_command,
    'ips' => build_host_ips,
    'name' => uname[:nodename],
    'system' => {
      'machine' => uname[:machine],
      'name' => uname[:sysname],
      'release' => uname[:release],
      'version' => uname[:version]
    },
    'tags' => build_host_tags,
    'user' => Etc.getpwuid(Process.uid).name,
    'pid' => Process.pid
  }.freeze
end

.build_host_commandHash

Build host command information

Returns:

  • (Hash)


164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/diffend/voting/versions/remote.rb', line 164

def build_host_command
  if File.exist?($PROGRAM_NAME)
    array = `ps -p #{Process.pid} -o command=`.strip.split(' ')
    array.shift if array.first.end_with?('bin/ruby')
    name = array.shift.split('/').last.strip
    command = "#{name} #{array.join(' ')}"

    { 'name' => command, 'title' => '' }
  else
    { 'name' => ARGV.join(' '), 'title' => $PROGRAM_NAME }
  end
end

.build_host_ipsArray<String>

Build host ips, except localhost and loopback

Returns:

  • (Array<String>)


180
181
182
183
184
185
186
# File 'lib/diffend/voting/versions/remote.rb', line 180

def build_host_ips
  Socket.ip_address_list.map do |ip|
    next if ip.ipv4_loopback? || ip.ipv6_loopback? || ip.ipv6_linklocal?

    ip.ip_address
  end.compact
end

.build_host_tagsArray

Build host tags

Returns:

  • (Array)


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/diffend/voting/versions/remote.rb', line 191

def build_host_tags
  tags = []

  if ENV.key?('GITHUB_ACTIONS')
    tags << 'ci'
    tags << 'ci-github'
  end

  if ENV.key?('CIRCLECI')
    tags << 'ci'
    tags << 'ci-circle'
  end

  tags
end

.build_platformHash

Build platform information

Returns:

  • (Hash)


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/diffend/voting/versions/remote.rb', line 97

def build_platform
  {
    'bundler' => {
      'version' => Bundler::VERSION
    },
    'environment' => build_platform_environment,
    'ruby' => build_platform_ruby,
    'rubygems' => {
      'specification_version' => Gem::Specification::CURRENT_SPECIFICATION_VERSION,
      'version' => Gem::VERSION
    }
  }.freeze
end

.build_platform_environmentString

Build platform environment information

Returns:

  • (String)


135
136
137
# File 'lib/diffend/voting/versions/remote.rb', line 135

def build_platform_environment
  ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
end

.build_platform_rubyHash

Build platform ruby information

Returns:

  • (Hash)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/diffend/voting/versions/remote.rb', line 114

def build_platform_ruby
  if defined?(JRUBY_VERSION)
    revision = JRUBY_REVISION.to_s
    version = JRUBY_VERSION
  else
    revision = RUBY_REVISION.to_s
    version = RUBY_ENGINE_VERSION
  end

  {
    'engine' => RUBY_ENGINE,
    'patchlevel' => RUBY_PATCHLEVEL,
    'release_date' => RUBY_RELEASE_DATE,
    'revision' => revision,
    'version' => version
  }
end

.build_request_object(command, config, payload) ⇒ Diffend::RequestObject

Parameters:

  • command (String)

    either install or update

  • config (OpenStruct)

    diffend config

  • payload (Hash)

Returns:



47
48
49
50
51
52
53
54
# File 'lib/diffend/voting/versions/remote.rb', line 47

def build_request_object(command, config, payload)
  Diffend::RequestObject.new(
    config: config,
    url: commands_url(command, config.project_id),
    payload: payload,
    request_method: :post
  )
end

.call(command, config, definition) ⇒ Object

Parameters:

  • command (String)

    either install or update

  • definition (Bundler::Definition)

    definition for your source

  • config (OpenStruct)

    diffend config



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/diffend/voting/versions/remote.rb', line 24

def call(command, config, definition)
  payload = payload(command, config.project_id, definition)

  response = Diffend::Request.call(
    build_request_object(command, config, payload)
  )

  JSON.parse(response.body)
rescue StandardError => e
  Diffend::HandleErrors::Report.call(
    exception: e,
    payload: payload || {},
    config: config,
    message: :unhandled_exception,
    report: true
  )
end

.commands_url(command, project_id) ⇒ String

Provides diffend command endpoint url

Parameters:

  • command (String)

    either install or update

  • project_id (String)

    diffend project_id

Returns:

  • (String)

    diffend endpoint



213
214
215
216
217
# File 'lib/diffend/voting/versions/remote.rb', line 213

def commands_url(command, project_id)
  return ENV['DIFFEND_COMMAND_URL'] if ENV.key?('DIFFEND_COMMAND_URL')

  "https://my.diffend.io/api/projects/#{project_id}/bundle/#{command}"
end

.payload(command, project_id, definition) ⇒ Hash

Build diffend, host, packages, and platform specific information

Parameters:

  • command (String)

    either install or update

  • project_id (String)

    diffend project_id

  • definition (Bundler::Definition)

    definition for your source

Returns:

  • (Hash)

    payload for diffend endpoint



63
64
65
66
67
68
69
70
# File 'lib/diffend/voting/versions/remote.rb', line 63

def payload(command, project_id, definition)
  {
    'diffend' => build_diffend(project_id),
    'host' => build_host,
    'packages' => Local.call(command, definition),
    'platform' => build_platform
  }.freeze
end