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)


65
66
67
68
69
70
71
72
73
# File 'lib/diffend/voting/versions/remote.rb', line 65

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_environment ⇒ String

Build diffend environment information

Returns:

  • (String)


78
79
80
# File 'lib/diffend/voting/versions/remote.rb', line 78

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

.build_host ⇒ Hash

Build host information

Returns:

  • (Hash)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/diffend/voting/versions/remote.rb', line 130

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_command ⇒ Hash

Build host command information

Returns:

  • (Hash)


152
153
154
155
156
157
# File 'lib/diffend/voting/versions/remote.rb', line 152

def build_host_command
  {
    'name' => $PROGRAM_NAME.split('/').last.strip,
    'options' => ARGV.join(' ')
  }
end

.build_host_ips ⇒ Array<String>

Build host ips, except localhost and loopback

Returns:

  • (Array<String>)


162
163
164
165
166
167
168
# File 'lib/diffend/voting/versions/remote.rb', line 162

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_tags ⇒ Array

Build host tags

Returns:

  • (Array)


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/diffend/voting/versions/remote.rb', line 173

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_platform ⇒ Hash

Build platform information

Returns:

  • (Hash)


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/diffend/voting/versions/remote.rb', line 85

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_environment ⇒ String

Build platform environment information

Returns:

  • (String)


123
124
125
# File 'lib/diffend/voting/versions/remote.rb', line 123

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

.build_platform_ruby ⇒ Hash

Build platform ruby information

Returns:

  • (Hash)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/diffend/voting/versions/remote.rb', line 102

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

.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
41
42
# 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(
    config,
    commands_url(command, config.project_id),
    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



195
196
197
198
199
# File 'lib/diffend/voting/versions/remote.rb', line 195

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



51
52
53
54
55
56
57
58
# File 'lib/diffend/voting/versions/remote.rb', line 51

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