diffend-monitor 0.2.31 → 0.2.36

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,10 +7,10 @@ module Diffend
7
7
  class << self
8
8
  # Execute request to Diffend
9
9
  #
10
+ # @param config [Diffend::Config]
11
+ # @param message [Symbol] message that we want to display
10
12
  # @param exception [Exception] expection that was raised
11
13
  # @param payload [Hash] with versions to check
12
- # @param config [OpenStruct] Diffend config
13
- # @param message [Symbol] message that we want to display
14
14
  # @param report [Boolean] if true we will report the issue to diffend
15
15
  # @param raise_exception [Boolean] if true we will raise an exception
16
16
  #
@@ -18,8 +18,11 @@ module Diffend
18
18
  def call(config:, message:, exception: nil, payload: {}, report: false, raise_exception: true)
19
19
  exception_payload = prepare_exception_payload(exception, payload)
20
20
 
21
- Bundler.ui.error(Diffend::HandleErrors::Messages::PAYLOAD_DUMP)
22
- Bundler.ui.error(Diffend::HandleErrors::Messages.const_get(message.to_s.upcase))
21
+ Diffend::HandleErrors::Messages::PAYLOAD_DUMP
22
+ .tap(&config.logger.method(:error))
23
+ Diffend::HandleErrors::Messages
24
+ .const_get(message.to_s.upcase)
25
+ .tap(&config.logger.method(:error))
23
26
 
24
27
  if report
25
28
  Diffend::Request.call(
@@ -30,14 +33,14 @@ module Diffend
30
33
  raise Diffend::Errors::HandledException if raise_exception
31
34
  end
32
35
 
33
- # @param config [OpenStruct] diffend config
36
+ # @param config [Diffend::Config]
34
37
  # @param payload [Hash]
35
38
  #
36
39
  # @return [Diffend::RequestObject]
37
40
  def build_request_object(config, payload)
38
41
  Diffend::RequestObject.new(
39
42
  config: config,
40
- url: errors_url(config.project_id),
43
+ url: config.errors_url,
41
44
  payload: payload,
42
45
  request_method: :post
43
46
  )
@@ -54,17 +57,6 @@ module Diffend
54
57
  .call(exception, payload)
55
58
  .tap(&Diffend::HandleErrors::DisplayToStdout.method(:call))
56
59
  end
57
-
58
- # Provides diffend errors endpoint url
59
- #
60
- # @param project_id [String] diffend project_id
61
- #
62
- # @return [String] diffend endpoint
63
- def errors_url(project_id)
64
- return ENV['DIFFEND_ERROR_URL'] if ENV.key?('DIFFEND_ERROR_URL')
65
-
66
- "https://my.diffend.io/api/projects/#{project_id}/errors"
67
- end
68
60
  end
69
61
  end
70
62
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Verify if we are running latest version of the plugin
5
+ module LatestVersion
6
+ class << self
7
+ # Verify if we are running latest version of the plugin
8
+ #
9
+ # @param config [Diffend::Config]
10
+ def call(config)
11
+ return if config.development?
12
+ return if installed_version == Diffend::VERSION
13
+
14
+ print_message(config, installed_version)
15
+
16
+ exit 2
17
+ end
18
+
19
+ private
20
+
21
+ # @return [String] installed plugin version
22
+ def installed_version
23
+ ::Bundler::Plugin
24
+ .index
25
+ .plugin_path('diffend')
26
+ .basename
27
+ .to_s
28
+ .split('-')
29
+ .last
30
+ end
31
+
32
+ # @param config [Diffend::Config]
33
+ # @param version [Hash] installed version
34
+ def print_message(config, version)
35
+ build_message(version)
36
+ .tap(&config.logger.method(:error))
37
+ end
38
+
39
+ # @param version [Hash] installed version
40
+ #
41
+ # @return [String]
42
+ def build_message(version)
43
+ <<~MSG
44
+ \nYou are running an outdated version (#{version}) of the plugin, which will lead to issues.
45
+ \nPlease upgrade to the latest one (#{Diffend::VERSION}) by executing "rm -rf .bundle/plugin".\n
46
+ MSG
47
+ end
48
+ end
49
+ end
50
+ end
@@ -6,16 +6,15 @@ module Diffend
6
6
  class << self
7
7
  # Build diffend, host, packages, and platform specific information
8
8
  #
9
- # @param command [String] either install or update
10
- # @param project_id [String] diffend project_id
9
+ # @param config [Diffend::Config]
11
10
  # @param definition [Bundler::Definition] definition for your source
12
11
  #
13
12
  # @return [Hash] payload for diffend endpoint
14
- def call(command, project_id, definition)
13
+ def call(config, definition)
15
14
  {
16
- 'diffend' => Diffend.call(project_id),
15
+ 'diffend' => Diffend.call(config),
17
16
  'host' => Host.call,
18
- 'packages' => Packages.call(command, definition),
17
+ 'packages' => Packages.call(config.command, definition),
19
18
  'platform' => Platform.call
20
19
  }.freeze
21
20
  end
@@ -15,14 +15,14 @@ module Diffend
15
15
  class << self
16
16
  # Build diffend information
17
17
  #
18
- # @param project_id [String, nil] diffend project_id
18
+ # @param config [Diffend::Config]
19
19
  #
20
20
  # @return [Hash]
21
- def call(project_id)
21
+ def call(config)
22
22
  {
23
23
  'api_version' => API_VERSION,
24
- 'environment' => ENV['DIFFEND_ENV'],
25
- 'project_id' => project_id,
24
+ 'environment' => config.env,
25
+ 'project_id' => config.project_id,
26
26
  'type' => PLATFORM_TYPE,
27
27
  'version' => ::Diffend::VERSION
28
28
  }.freeze
@@ -47,9 +47,9 @@ module Diffend
47
47
  command = "#{name} #{array.join(' ')}"
48
48
  end
49
49
 
50
- { 'name' => command, 'title' => '' }
50
+ { 'name' => clean(command), 'title' => '' }
51
51
  else
52
- { 'name' => ARGV.join(' '), 'title' => $PROGRAM_NAME }
52
+ { 'name' => clean(ARGV.join(' ')), 'title' => clean($PROGRAM_NAME) }
53
53
  end
54
54
  end
55
55
 
@@ -82,6 +82,14 @@ module Diffend
82
82
 
83
83
  tags
84
84
  end
85
+
86
+ # @param str [String] that we want to clean and truncate
87
+ def clean(str)
88
+ str
89
+ .dup
90
+ .gsub(/[[:space:]]+/, ' ')
91
+ .strip[0...255]
92
+ end
85
93
  end
86
94
  end
87
95
  end
@@ -31,12 +31,12 @@ module Diffend
31
31
  }.freeze
32
32
 
33
33
  class << self
34
- # @param command [String] either install or update
34
+ # @param command [String] command executed via bundler
35
35
  # @param definition [Bundler::Definition] definition for your source
36
36
  def call(command, definition)
37
- Bundler.ui.silence { definition.resolve_remotely! }
37
+ instance = new(command, definition)
38
38
 
39
- instance = new(definition)
39
+ Bundler.ui.silence { instance.resolve }
40
40
 
41
41
  case command
42
42
  when Commands::INSTALL, Commands::EXEC then instance.build_install
@@ -47,14 +47,22 @@ module Diffend
47
47
  end
48
48
  end
49
49
 
50
+ # @param command [String] command executed via bundler
50
51
  # @param definition [Bundler::Definition] definition for your source
51
52
  #
52
53
  # @return [Hash] local dependencies
53
- def initialize(definition)
54
+ def initialize(command, definition)
55
+ @command = command
54
56
  @definition = definition
55
57
  @direct_dependencies = Hash[definition.dependencies.map { |val| [val.name, val] }]
56
58
  # Support case without Gemfile.lock
57
59
  @locked_specs = @definition.locked_gems ? @definition.locked_gems.specs : []
60
+ @cached = command == Commands::EXEC
61
+ end
62
+
63
+ # Resolve definition
64
+ def resolve
65
+ @cached ? @definition.resolve_with_cache! : @definition.resolve_remotely!
58
66
  end
59
67
 
60
68
  # Build install specification
@@ -161,7 +169,7 @@ module Diffend
161
169
  def parse_platform(platform)
162
170
  case platform
163
171
  when String then platform
164
- when Gem::Platform then platform.os
172
+ when Gem::Platform then platform.to_s
165
173
  end
166
174
  end
167
175
 
@@ -209,12 +217,7 @@ module Diffend
209
217
 
210
218
  case spec.source
211
219
  when Bundler::Source::Rubygems
212
- spec
213
- .source
214
- .send(:remote_specs)
215
- .search(Bundler::Dependency.new(spec.name, spec.version))
216
- .last
217
- .remote
220
+ Bundler::Source::Rubygems::Remote.new(spec.source.remotes.last)
218
221
  when Bundler::Source::Metadata, Bundler::Source::Git, Bundler::Source::Path
219
222
  spec.source
220
223
  else
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Diffend logging
5
+ class Logger
6
+ # Low-level information, mostly for developers.
7
+ DEBUG = 0
8
+ # Generic (useful) information about system operation.
9
+ INFO = 1
10
+ # A warning.
11
+ WARN = 2
12
+ # A handleable error condition.
13
+ ERROR = 3
14
+ # An unhandleable error that results in a program crash.
15
+ FATAL = 4
16
+ # An unknown message that should always be logged.
17
+ UNKNOWN = 5
18
+
19
+ # @param level [Integer] logging severity threshold
20
+ def initialize(level = INFO)
21
+ @level = level
22
+ end
23
+
24
+ # @param message [String]
25
+ def debug(message)
26
+ log(DEBUG, message)
27
+ end
28
+
29
+ # @param message [String]
30
+ def info(message)
31
+ log(INFO, message)
32
+ end
33
+
34
+ # @param message [String]
35
+ def warn(message)
36
+ log(WARN, message)
37
+ end
38
+
39
+ # @param message [String]
40
+ def error(message)
41
+ log(ERROR, message)
42
+ end
43
+
44
+ # @param message [String]
45
+ def fatal(message)
46
+ log(FATAL, message)
47
+ end
48
+
49
+ private
50
+
51
+ # @param severity [Integer]
52
+ # @param message [String]
53
+ def log(severity, message)
54
+ return if severity < @level
55
+
56
+ case severity
57
+ when INFO
58
+ Bundler.ui.confirm(message)
59
+ when WARN
60
+ Bundler.ui.warn(message)
61
+ when ERROR, FATAL
62
+ Bundler.ui.error(message)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,16 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- ENV['DIFFEND_ENV'] ||= 'development'
4
-
5
3
  %w[
6
4
  version
5
+ logger
7
6
  errors
8
7
  build_bundler_definition
9
8
  commands
10
9
  config
11
- config/fetcher
12
- config/file_finder
13
- config/validator
10
+ configs/fetcher
11
+ configs/validator
14
12
  handle_errors/messages
15
13
  handle_errors/build_exception_payload
16
14
  handle_errors/display_to_stdout
@@ -27,10 +25,19 @@ ENV['DIFFEND_ENV'] ||= 'development'
27
25
  track
28
26
  ].each { |file| require "diffend/#{file}" }
29
27
 
30
- return if ENV['DIFFEND_ENV'].strip.empty?
31
- return if %w[development test].include?(ENV['DIFFEND_ENV'])
28
+ begin
29
+ config = Diffend::Config.new(
30
+ command: Diffend::Commands::EXEC,
31
+ severity: Diffend::Logger::FATAL
32
+ )
33
+ rescue Diffend::Errors::HandledException
34
+ # we silent exit here because we don't want to break client boot
35
+ return
36
+ end
37
+
38
+ return if %w[development test].include?(config.env)
32
39
 
33
40
  Thread.new do
34
- track = Diffend::Track.new
41
+ track = Diffend::Track.new(config)
35
42
  track.start
36
43
  end
@@ -1,20 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- ENV['DIFFEND_ENV'] ||= 'development'
4
-
5
3
  %w[
6
4
  bundler
7
5
  ].each(&method(:require))
8
6
 
9
7
  %w[
10
8
  version
9
+ logger
10
+ latest_version
11
11
  errors
12
12
  build_bundler_definition
13
13
  commands
14
14
  config
15
- config/fetcher
16
- config/file_finder
17
- config/validator
15
+ configs/fetcher
16
+ configs/validator
18
17
  handle_errors/messages
19
18
  handle_errors/build_exception_payload
20
19
  handle_errors/display_to_stdout
@@ -45,13 +44,14 @@ module Diffend
45
44
  def execute
46
45
  return unless enabled?
47
46
 
48
- verify_version
47
+ config = Diffend::Config.new(severity: Diffend::Logger::INFO)
49
48
 
50
- config = Diffend::Config.call
49
+ Diffend::LatestVersion.call(config)
51
50
 
52
- Diffend::Execute.call(command, config)
51
+ Diffend::Execute.call(config)
53
52
  rescue Diffend::Errors::HandledException
54
- return if ENV['DIFFEND_IGNORE_ERRORS'] == 'true'
53
+ # config will not be initialized when configuration file is missing
54
+ return if config&.ignore_errors?
55
55
 
56
56
  exit 255
57
57
  rescue StandardError => e
@@ -63,32 +63,11 @@ module Diffend
63
63
  raise_exception: false
64
64
  )
65
65
 
66
- return if ENV['DIFFEND_IGNORE_ERRORS'] == 'true'
66
+ return if config.ignore_errors?
67
67
 
68
68
  exit 255
69
69
  end
70
70
 
71
- def verify_version
72
- return if ENV['DIFFEND_DEVELOPMENT'] == 'true'
73
- return if installed_version == Diffend::VERSION
74
-
75
- build_outdated_version_message(installed_version)
76
- .tap(&::Bundler.ui.method(:error))
77
-
78
- exit 2
79
- end
80
-
81
- # @return [String] installed plugin version
82
- def installed_version
83
- ::Bundler::Plugin
84
- .index
85
- .plugin_path('diffend')
86
- .basename
87
- .to_s
88
- .split('-')
89
- .last
90
- end
91
-
92
71
  # Checks if plugin is enabled
93
72
  #
94
73
  # @return [Boolean] true if enabled, false otherwise
@@ -102,23 +81,6 @@ module Diffend
102
81
  .select { |line| line.start_with?('plugin') }
103
82
  .any? { |line| line.include?('diffend') }
104
83
  end
105
-
106
- # @param version [Hash] installed version
107
- #
108
- # @return [String]
109
- def build_outdated_version_message(version)
110
- <<~MSG
111
- \nYou are running an outdated version (#{version}) of the plugin, which will lead to issues.
112
- \nPlease upgrade to the latest one (#{VERSION}) by executing "rm -rf .bundle/plugin".\n
113
- MSG
114
- end
115
-
116
- # Command that was run with bundle
117
- #
118
- # @return [String]
119
- def command
120
- ARGV.first || ::Bundler.feature_flag.default_cli_command.to_s
121
- end
122
84
  end
123
85
  end
124
86
  end