diffend 0.2.29 → 0.2.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,227 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
- require 'etc'
5
-
6
- module Diffend
7
- module Voting
8
- # Module responsible for handling both local and remote gem versions
9
- module Versions
10
- # Module responsible for fetching safe/malicious votes
11
- # for current or current/new versions of gems
12
- module Remote
13
- # API version
14
- API_VERSION = '0.1'
15
- # Platform type ruby
16
- PLATFORM_TYPE = 0
17
-
18
- private_constant :API_VERSION, :PLATFORM_TYPE
19
-
20
- class << self
21
- # @param command [String] either install or update
22
- # @param definition [Bundler::Definition] definition for your source
23
- # @param config [OpenStruct] diffend config
24
- def call(command, config, definition)
25
- payload = payload(command, config.project_id, definition)
26
-
27
- response = Diffend::Request.call(
28
- build_request_object(command, config, payload)
29
- )
30
-
31
- JSON.parse(response.body)
32
- rescue StandardError => e
33
- Diffend::HandleErrors::Report.call(
34
- exception: e,
35
- payload: payload || {},
36
- config: config,
37
- message: :unhandled_exception,
38
- report: true
39
- )
40
- end
41
-
42
- # @param command [String] either install or update
43
- # @param config [OpenStruct] diffend config
44
- # @param payload [Hash]
45
- #
46
- # @return [Diffend::RequestObject]
47
- def build_request_object(command, config, payload)
48
- Diffend::RequestObject.new(
49
- config: config,
50
- url: commands_url(command, config.project_id),
51
- payload: payload,
52
- request_method: :post
53
- )
54
- end
55
-
56
- # Build diffend, host, packages, and platform specific information
57
- #
58
- # @param command [String] either install or update
59
- # @param project_id [String] diffend project_id
60
- # @param definition [Bundler::Definition] definition for your source
61
- #
62
- # @return [Hash] payload for diffend endpoint
63
- def payload(command, project_id, definition)
64
- {
65
- 'diffend' => build_diffend(project_id),
66
- 'host' => build_host,
67
- 'packages' => Local.call(command, definition),
68
- 'platform' => build_platform
69
- }.freeze
70
- end
71
-
72
- # Build diffend information
73
- #
74
- # @param project_id [String, nil] diffend project_id
75
- #
76
- # @return [Hash]
77
- def build_diffend(project_id)
78
- {
79
- 'api_version' => API_VERSION,
80
- 'environment' => build_diffend_environment,
81
- 'project_id' => project_id,
82
- 'type' => PLATFORM_TYPE,
83
- 'version' => Diffend::VERSION
84
- }.freeze
85
- end
86
-
87
- # Build diffend environment information
88
- #
89
- # @return [String]
90
- def build_diffend_environment
91
- ENV['DIFFEND_ENV'] || 'development'
92
- end
93
-
94
- # Build platform information
95
- #
96
- # @return [Hash]
97
- def build_platform
98
- {
99
- 'bundler' => {
100
- 'version' => Bundler::VERSION
101
- },
102
- 'environment' => build_platform_environment,
103
- 'ruby' => build_platform_ruby,
104
- 'rubygems' => {
105
- 'specification_version' => Gem::Specification::CURRENT_SPECIFICATION_VERSION,
106
- 'version' => Gem::VERSION
107
- }
108
- }.freeze
109
- end
110
-
111
- # Build platform ruby information
112
- #
113
- # @return [Hash]
114
- def build_platform_ruby
115
- if defined?(JRUBY_VERSION)
116
- revision = JRUBY_REVISION.to_s
117
- version = JRUBY_VERSION
118
- else
119
- revision = RUBY_REVISION.to_s
120
- version = RUBY_ENGINE_VERSION
121
- end
122
-
123
- {
124
- 'engine' => RUBY_ENGINE,
125
- 'patchlevel' => RUBY_PATCHLEVEL,
126
- 'release_date' => RUBY_RELEASE_DATE,
127
- 'revision' => revision,
128
- 'version' => version
129
- }
130
- end
131
-
132
- # Build platform environment information
133
- #
134
- # @return [String]
135
- def build_platform_environment
136
- ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
137
- end
138
-
139
- # Build host information
140
- #
141
- # @return [Hash]
142
- def build_host
143
- uname = Etc.uname
144
-
145
- {
146
- 'command' => build_host_command,
147
- 'ips' => build_host_ips,
148
- 'name' => uname[:nodename],
149
- 'system' => {
150
- 'machine' => uname[:machine],
151
- 'name' => uname[:sysname],
152
- 'release' => uname[:release],
153
- 'version' => uname[:version]
154
- },
155
- 'tags' => build_host_tags,
156
- 'user' => Etc.getpwuid(Process.uid).name,
157
- 'pid' => Process.pid
158
- }.freeze
159
- end
160
-
161
- # Build host command information
162
- #
163
- # @return [Hash]
164
- def build_host_command
165
- if File.exist?($PROGRAM_NAME)
166
- if defined?(JRUBY_VERSION)
167
- name = $PROGRAM_NAME.split('/').last.strip
168
- command = "#{name} #{ARGV.join(' ')}"
169
- else
170
- array = `ps -p #{Process.pid} -o command=`.strip.split(' ')
171
- array.shift if array.first.end_with?('bin/ruby')
172
- name = array.shift.split('/').last.strip
173
- command = "#{name} #{array.join(' ')}"
174
- end
175
-
176
- { 'name' => command, 'title' => '' }
177
- else
178
- { 'name' => ARGV.join(' '), 'title' => $PROGRAM_NAME }
179
- end
180
- end
181
-
182
- # Build host ips, except localhost and loopback
183
- #
184
- # @return [Array<String>]
185
- def build_host_ips
186
- Socket.ip_address_list.map do |ip|
187
- next if ip.ipv4_loopback? || ip.ipv6_loopback? || ip.ipv6_linklocal?
188
-
189
- ip.ip_address
190
- end.compact
191
- end
192
-
193
- # Build host tags
194
- #
195
- # @return [Array]
196
- def build_host_tags
197
- tags = []
198
-
199
- if ENV.key?('GITHUB_ACTIONS')
200
- tags << 'ci'
201
- tags << 'ci-github'
202
- end
203
-
204
- if ENV.key?('CIRCLECI')
205
- tags << 'ci'
206
- tags << 'ci-circle'
207
- end
208
-
209
- tags
210
- end
211
-
212
- # Provides diffend command endpoint url
213
- #
214
- # @param command [String] either install or update
215
- # @param project_id [String] diffend project_id
216
- #
217
- # @return [String] diffend endpoint
218
- def commands_url(command, project_id)
219
- return ENV['DIFFEND_COMMAND_URL'] if ENV.key?('DIFFEND_COMMAND_URL')
220
-
221
- "https://my.diffend.io/api/projects/#{project_id}/bundle/#{command}"
222
- end
223
- end
224
- end
225
- end
226
- end
227
- end