diffend-monitor 0.2.28 → 0.2.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,222 +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
- array = `ps -p #{Process.pid} -o command=`.strip.split(' ')
167
- array.shift if array.first.end_with?('bin/ruby')
168
- name = array.shift.split('/').last.strip
169
- command = "#{name} #{array.join(' ')}"
170
-
171
- { 'name' => command, 'title' => '' }
172
- else
173
- { 'name' => ARGV.join(' '), 'title' => $PROGRAM_NAME }
174
- end
175
- end
176
-
177
- # Build host ips, except localhost and loopback
178
- #
179
- # @return [Array<String>]
180
- def build_host_ips
181
- Socket.ip_address_list.map do |ip|
182
- next if ip.ipv4_loopback? || ip.ipv6_loopback? || ip.ipv6_linklocal?
183
-
184
- ip.ip_address
185
- end.compact
186
- end
187
-
188
- # Build host tags
189
- #
190
- # @return [Array]
191
- def build_host_tags
192
- tags = []
193
-
194
- if ENV.key?('GITHUB_ACTIONS')
195
- tags << 'ci'
196
- tags << 'ci-github'
197
- end
198
-
199
- if ENV.key?('CIRCLECI')
200
- tags << 'ci'
201
- tags << 'ci-circle'
202
- end
203
-
204
- tags
205
- end
206
-
207
- # Provides diffend command endpoint url
208
- #
209
- # @param command [String] either install or update
210
- # @param project_id [String] diffend project_id
211
- #
212
- # @return [String] diffend endpoint
213
- def commands_url(command, project_id)
214
- return ENV['DIFFEND_COMMAND_URL'] if ENV.key?('DIFFEND_COMMAND_URL')
215
-
216
- "https://my.diffend.io/api/projects/#{project_id}/bundle/#{command}"
217
- end
218
- end
219
- end
220
- end
221
- end
222
- end