diffend 0.2.30 → 0.2.35

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.
@@ -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,7 +31,7 @@ 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
37
  Bundler.ui.silence { definition.resolve_remotely! }
@@ -161,7 +161,7 @@ module Diffend
161
161
  def parse_platform(platform)
162
162
  case platform
163
163
  when String then platform
164
- when Gem::Platform then platform.os
164
+ when Gem::Platform then platform.to_s
165
165
  end
166
166
  end
167
167
 
@@ -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
@@ -14,7 +14,8 @@ module Diffend
14
14
  Errno::ECONNRESET,
15
15
  Errno::ENETUNREACH,
16
16
  Errno::EHOSTUNREACH,
17
- Errno::ECONNREFUSED
17
+ Errno::ECONNREFUSED,
18
+ SocketError
18
19
  ].freeze
19
20
  # Message displayed when timeout occured and we will retry
20
21
  TIMEOUT_MESSAGE = 'We experienced a connection issue, retrying...'
@@ -67,7 +68,7 @@ module Diffend
67
68
  rescue Diffend::Errors::RequestServerError => e
68
69
  retry_count += 1
69
70
 
70
- retry if handle_retry(SERVER_ERROR_MESSAGE, retry_count)
71
+ retry if handle_retry(request_object.config, SERVER_ERROR_MESSAGE, retry_count)
71
72
 
72
73
  Diffend::HandleErrors::Report.call(
73
74
  exception: e,
@@ -78,7 +79,7 @@ module Diffend
78
79
  rescue *CONNECTION_EXCEPTIONS => e
79
80
  retry_count += 1
80
81
 
81
- retry if handle_retry(CONNECTION_MESSAGE, retry_count)
82
+ retry if handle_retry(request_object.config, CONNECTION_MESSAGE, retry_count)
82
83
 
83
84
  Diffend::HandleErrors::Report.call(
84
85
  exception: e,
@@ -89,7 +90,7 @@ module Diffend
89
90
  rescue *TIMEOUT_EXCEPTIONS => e
90
91
  retry_count += 1
91
92
 
92
- retry if handle_retry(TIMEOUT_MESSAGE, retry_count)
93
+ retry if handle_retry(request_object.config, TIMEOUT_MESSAGE, retry_count)
93
94
 
94
95
  Diffend::HandleErrors::Report.call(
95
96
  exception: e,
@@ -101,12 +102,13 @@ module Diffend
101
102
 
102
103
  # Handle retry
103
104
  #
105
+ # @param config [Diffend::Config]
104
106
  # @param message [String] message we want to display
105
107
  # @param retry_count [Integer]
106
- def handle_retry(message, retry_count)
108
+ def handle_retry(config, message, retry_count)
107
109
  return false if retry_count == RETRIES
108
110
 
109
- Bundler.ui.error(message)
111
+ config.logger.warn(message)
110
112
  sleep(exponential_backoff(retry_count))
111
113
 
112
114
  retry_count < RETRIES
@@ -123,8 +125,8 @@ module Diffend
123
125
  uri.port,
124
126
  use_ssl: uri.scheme == 'https',
125
127
  verify_mode: OpenSSL::SSL::VERIFY_NONE,
126
- open_timeout: 5,
127
- read_timeout: 5
128
+ open_timeout: 15,
129
+ read_timeout: 15
128
130
  ) { |http| yield(http, uri) }
129
131
  end
130
132
 
@@ -132,7 +134,7 @@ module Diffend
132
134
  #
133
135
  # @param uri [URI::HTTPS]
134
136
  # @param request_method [Symbol]
135
- # @param config [OpenStruct] Diffend config
137
+ # @param config [Diffend::Config]
136
138
  # @param payload [Hash] with versions to check
137
139
  #
138
140
  # @return [Net::HTTP::Post, Net::HTTP::Put]
@@ -160,9 +162,8 @@ module Diffend
160
162
  # Assigns basic authorization if provided in the config
161
163
  #
162
164
  # @param request [Net::HTTP::Post] prepared http post
163
- # @param config [OpenStruct] Diffend config
165
+ # @param config [Diffend::Config]
164
166
  def assign_auth(request, config)
165
- return unless config
166
167
  return unless config.shareable_id
167
168
  return unless config.shareable_key
168
169