diffend-monitor 0.2.27 → 0.2.33

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