diffend 0.2.25 → 0.2.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['DIFFEND_ENV'] ||= 'development'
4
+
5
+ %w[
6
+ bundler
7
+ ].each(&method(:require))
8
+
9
+ %w[
10
+ version
11
+ errors
12
+ build_bundler_definition
13
+ commands
14
+ config
15
+ config/fetcher
16
+ config/file_finder
17
+ config/validator
18
+ handle_errors/messages
19
+ handle_errors/build_exception_payload
20
+ handle_errors/display_to_stdout
21
+ handle_errors/report
22
+ request_object
23
+ request
24
+ local_context/diffend
25
+ local_context/host
26
+ local_context/packages
27
+ local_context/platform
28
+ local_context
29
+ request_verdict
30
+ execute
31
+ track
32
+ ].each { |file| require "diffend/#{file}" }
33
+
34
+ module Diffend
35
+ module Plugin
36
+ class << self
37
+ # Registers the plugin and add before install all hook
38
+ def register
39
+ ::Bundler::Plugin.add_hook('before-install-all') do |_|
40
+ execute
41
+ end
42
+ end
43
+
44
+ # Execute diffend plugin
45
+ def execute
46
+ return unless enabled?
47
+
48
+ verify_version
49
+
50
+ config = Diffend::Config.call
51
+
52
+ Diffend::Execute.call(command, config)
53
+ rescue Diffend::Errors::HandledException
54
+ return if ENV['DIFFEND_IGNORE_ERRORS'] == 'true'
55
+
56
+ exit 255
57
+ rescue StandardError => e
58
+ Diffend::HandleErrors::Report.call(
59
+ exception: e,
60
+ config: config,
61
+ message: :unhandled_exception,
62
+ report: true,
63
+ raise_exception: false
64
+ )
65
+
66
+ return if ENV['DIFFEND_IGNORE_ERRORS'] == 'true'
67
+
68
+ exit 255
69
+ end
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
+ # Checks if plugin is enabled
93
+ #
94
+ # @return [Boolean] true if enabled, false otherwise
95
+ def enabled?
96
+ ::Bundler
97
+ .default_gemfile
98
+ .read
99
+ .split("\n")
100
+ .reject(&:empty?)
101
+ .map(&:strip)
102
+ .select { |line| line.start_with?('plugin') }
103
+ .any? { |line| line.include?('diffend') }
104
+ 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
+ end
123
+ end
124
+ end
@@ -42,16 +42,21 @@ module Diffend
42
42
  class << self
43
43
  # Execute request
44
44
  #
45
- # @param config [OpenStruct] diffend config
46
- # @param endpoint_url [String]
47
- # @param payload [Hash]
45
+ # @param request_object [Diffend::RequestObject]
48
46
  #
49
47
  # @return [Net::HTTPResponse] response from Diffend
50
- def call(config, endpoint_url, payload)
48
+ def call(request_object)
51
49
  retry_count ||= -1
52
50
 
53
- build_http(endpoint_url) do |http, uri|
54
- response = http.request(build_request(uri, config, payload))
51
+ build_http(request_object.url) do |http, uri|
52
+ response = http.request(
53
+ build_request(
54
+ uri,
55
+ request_object.request_method,
56
+ request_object.config,
57
+ request_object.payload
58
+ )
59
+ )
55
60
 
56
61
  if SERVER_ERRORS.include?(response.code.to_i)
57
62
  raise Diffend::Errors::RequestServerError, response.code.to_i
@@ -66,8 +71,8 @@ module Diffend
66
71
 
67
72
  Diffend::HandleErrors::Report.call(
68
73
  exception: e,
69
- payload: payload,
70
- config: config,
74
+ payload: request_object.payload,
75
+ config: request_object.config,
71
76
  message: :request_error
72
77
  )
73
78
  rescue *CONNECTION_EXCEPTIONS => e
@@ -77,8 +82,8 @@ module Diffend
77
82
 
78
83
  Diffend::HandleErrors::Report.call(
79
84
  exception: e,
80
- payload: payload,
81
- config: config,
85
+ payload: request_object.payload,
86
+ config: request_object.config,
82
87
  message: :request_error
83
88
  )
84
89
  rescue *TIMEOUT_EXCEPTIONS => e
@@ -88,8 +93,8 @@ module Diffend
88
93
 
89
94
  Diffend::HandleErrors::Report.call(
90
95
  exception: e,
91
- payload: payload,
92
- config: config,
96
+ payload: request_object.payload,
97
+ config: request_object.config,
93
98
  message: :request_error
94
99
  )
95
100
  end
@@ -126,17 +131,32 @@ module Diffend
126
131
  # Build http post request and assigns headers and payload
127
132
  #
128
133
  # @param uri [URI::HTTPS]
134
+ # @param request_method [Symbol]
129
135
  # @param config [OpenStruct] Diffend config
130
136
  # @param payload [Hash] with versions to check
131
137
  #
132
- # @return [Net::HTTP::Post]
133
- def build_request(uri, config, payload)
134
- Net::HTTP::Post
138
+ # @return [Net::HTTP::Post, Net::HTTP::Put]
139
+ def build_request(uri, request_method, config, payload)
140
+ pick_request_method(request_method)
135
141
  .new(uri.request_uri, HEADERS)
136
142
  .tap { |request| assign_auth(request, config) }
137
143
  .tap { |request| assign_payload(request, payload) }
138
144
  end
139
145
 
146
+ # Pick request method
147
+ #
148
+ # @param request_method [Symbol]
149
+ #
150
+ # @return [Net::HTTP::Post, Net::HTTP::Put]
151
+ def pick_request_method(request_method)
152
+ case request_method
153
+ when :post
154
+ Net::HTTP::Post
155
+ when :put
156
+ Net::HTTP::Put
157
+ end
158
+ end
159
+
140
160
  # Assigns basic authorization if provided in the config
141
161
  #
142
162
  # @param request [Net::HTTP::Post] prepared http post
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Class responsible for preparing diffend request object
5
+ RequestObject = Struct.new(:config, :url, :payload, :request_method, keyword_init: true)
6
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module Diffend
6
+ # Module responsible for fetching diffend verdict on local context
7
+ module RequestVerdict
8
+ class << self
9
+ # @param command [String] either install or update
10
+ # @param definition [Bundler::Definition] definition for your source
11
+ # @param config [OpenStruct] diffend config
12
+ def call(command, config, definition)
13
+ payload = Diffend::LocalContext.call(command, config.project_id, definition)
14
+
15
+ response = Diffend::Request.call(
16
+ build_request_object(command, config, payload)
17
+ )
18
+
19
+ JSON.parse(response.body)
20
+ rescue Bundler::GemNotFound
21
+ raise ::Diffend::Errors::DependenciesResolveException
22
+ rescue StandardError => e
23
+ Diffend::HandleErrors::Report.call(
24
+ exception: e,
25
+ payload: payload || {},
26
+ config: config,
27
+ message: :unhandled_exception,
28
+ report: true
29
+ )
30
+ end
31
+
32
+ # @param command [String] either install or update
33
+ # @param config [OpenStruct] diffend config
34
+ # @param payload [Hash]
35
+ #
36
+ # @return [Diffend::RequestObject]
37
+ def build_request_object(command, config, payload)
38
+ Diffend::RequestObject.new(
39
+ config: config,
40
+ url: commands_url(command, config.project_id),
41
+ payload: payload,
42
+ request_method: :post
43
+ )
44
+ end
45
+
46
+ # Provides diffend command endpoint url
47
+ #
48
+ # @param command [String] either install or update
49
+ # @param project_id [String] diffend project_id
50
+ #
51
+ # @return [String] diffend endpoint
52
+ def commands_url(command, project_id)
53
+ return ENV['DIFFEND_COMMAND_URL'] if ENV.key?('DIFFEND_COMMAND_URL')
54
+
55
+ "https://my.diffend.io/api/projects/#{project_id}/bundle/#{command}"
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Track what is run in production
5
+ class Track
6
+ # Time that we want to wait between track requests
7
+ TRACK_SLEEP = 15
8
+ # Time that we want to wait before we retry
9
+ RETRY_SLEEP = 15
10
+
11
+ # Initialize tracking
12
+ def initialize
13
+ @mutex = Mutex.new
14
+ @config = Diffend::Config.call
15
+ end
16
+
17
+ # Start tracking
18
+ def start
19
+ response = exec_request
20
+
21
+ perform(response['id'])
22
+ rescue Diffend::Errors::HandledException
23
+ sleep(RETRY_SLEEP)
24
+
25
+ retry
26
+ rescue StandardError => e
27
+ Diffend::HandleErrors::Report.call(
28
+ exception: e,
29
+ config: @config,
30
+ message: :unhandled_exception,
31
+ report: true,
32
+ raise_exception: false
33
+ )
34
+
35
+ sleep(RETRY_SLEEP)
36
+
37
+ retry
38
+ end
39
+
40
+ # @param request_id [String]
41
+ def perform(request_id)
42
+ loop do
43
+ @mutex.synchronize do
44
+ track_request(request_id)
45
+ end
46
+
47
+ sleep(TRACK_SLEEP)
48
+ end
49
+ end
50
+
51
+ # Perform an exec request
52
+ def exec_request
53
+ Diffend::Execute.call(Diffend::Commands::EXEC, @config)
54
+ end
55
+
56
+ # Perform a track request
57
+ #
58
+ # @param request_id [String]
59
+ def track_request(request_id)
60
+ Diffend::Request.call(
61
+ build_request_object(request_id)
62
+ )
63
+ end
64
+
65
+ # @param request_id [String]
66
+ #
67
+ # @return [Diffend::RequestObject]
68
+ def build_request_object(request_id)
69
+ Diffend::RequestObject.new(
70
+ config: @config,
71
+ url: track_url(@config.project_id, request_id),
72
+ payload: { id: request_id }.freeze,
73
+ request_method: :put
74
+ ).freeze
75
+ end
76
+
77
+ # @param project_id [String] diffend project_id
78
+ # @param request_id [String]
79
+ #
80
+ # @return [String]
81
+ def track_url(project_id, request_id)
82
+ "https://my.diffend.io/api/projects/#{project_id}/bundle/#{request_id}/track"
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Diffend
4
+ # Current version
5
+ VERSION = '0.2.30'
6
+ end
data/plugins.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'diffend'
3
+ require 'diffend/plugin'
4
4
 
5
- Diffend.register
5
+ Diffend::Plugin.register
@@ -3,7 +3,6 @@
3
3
  require 'byebug'
4
4
  require 'diffend'
5
5
 
6
-
7
6
  command = 'install'
8
7
  project_id = nil
9
8
 
@@ -12,4 +11,4 @@ lockfile = ARGV[1]
12
11
 
13
12
  definition = Diffend::BuildBundlerDefinition.call(command, gemfile lockfile)
14
13
 
15
- pp Diffend::Voting::Versions::Remote.payload(command, project_id, definition)
14
+ pp Diffend::LocalContext.call(command, project_id, definition)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diffend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.25
4
+ version: 0.2.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomasz Pajor
@@ -34,7 +34,7 @@ cert_chain:
34
34
  9MmF6uCQa1EjK2p8tYT0MnbHrFkoehxdX4VO9y99GAkhZyJNKPYPtyAUFV27sT2V
35
35
  LfCJRk4ifKIN/FUCwDSn8Cz0m6oH265q0p6wdzI6qrWOjP8tGOMBTA==
36
36
  -----END CERTIFICATE-----
37
- date: 2020-09-09 00:00:00.000000000 Z
37
+ date: 2020-09-21 00:00:00.000000000 Z
38
38
  dependencies:
39
39
  - !ruby/object:Gem::Dependency
40
40
  name: bundler
@@ -94,18 +94,28 @@ files:
94
94
  - lib/diffend.rb
95
95
  - lib/diffend/build_bundler_definition.rb
96
96
  - lib/diffend/commands.rb
97
+ - lib/diffend/config.rb
97
98
  - lib/diffend/config/fetcher.rb
98
99
  - lib/diffend/config/file_finder.rb
99
100
  - lib/diffend/config/validator.rb
100
101
  - lib/diffend/errors.rb
102
+ - lib/diffend/execute.rb
101
103
  - lib/diffend/handle_errors/build_exception_payload.rb
102
104
  - lib/diffend/handle_errors/display_to_stdout.rb
103
105
  - lib/diffend/handle_errors/messages.rb
104
106
  - lib/diffend/handle_errors/report.rb
107
+ - lib/diffend/local_context.rb
108
+ - lib/diffend/local_context/diffend.rb
109
+ - lib/diffend/local_context/host.rb
110
+ - lib/diffend/local_context/packages.rb
111
+ - lib/diffend/local_context/platform.rb
112
+ - lib/diffend/monitor.rb
113
+ - lib/diffend/plugin.rb
105
114
  - lib/diffend/request.rb
106
- - lib/diffend/voting.rb
107
- - lib/diffend/voting/versions/local.rb
108
- - lib/diffend/voting/versions/remote.rb
115
+ - lib/diffend/request_object.rb
116
+ - lib/diffend/request_verdict.rb
117
+ - lib/diffend/track.rb
118
+ - lib/diffend/version.rb
109
119
  - plugins.rb
110
120
  - scripts/generate_payload_for_file.rb
111
121
  homepage: https://diffend.io
@@ -130,5 +140,5 @@ requirements: []
130
140
  rubygems_version: 3.1.2
131
141
  signing_key:
132
142
  specification_version: 4
133
- summary: OSS supply chain security and management platform.
143
+ summary: OSS supply chain security and management platform
134
144
  test_files: []