dependabot-core 0.81.1 → 0.82.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,79 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dependabot/file_updaters/base"
4
-
5
- module Dependabot
6
- module FileUpdaters
7
- module Elm
8
- class ElmPackage < Base
9
- require_relative "elm_package/elm_package_updater"
10
- require_relative "elm_package/elm_json_updater"
11
-
12
- def self.updated_files_regex
13
- [
14
- /^elm-package\.json$/,
15
- /^elm\.json$/
16
- ]
17
- end
18
-
19
- def updated_dependency_files
20
- updated_files = []
21
-
22
- elm_package_files.each do |file|
23
- next unless file_changed?(file)
24
-
25
- updated_files <<
26
- updated_file(
27
- file: file,
28
- content: updated_elm_package_content(file)
29
- )
30
- end
31
-
32
- elm_json_files.each do |file|
33
- next unless file_changed?(file)
34
-
35
- updated_files <<
36
- updated_file(
37
- file: file,
38
- content: updated_elm_json_content(file)
39
- )
40
- end
41
-
42
- raise "No files have changed!" if updated_files.none?
43
-
44
- updated_files
45
- end
46
-
47
- private
48
-
49
- def check_required_files
50
- return if elm_json_files.any? || elm_package_files.any?
51
-
52
- raise "No elm.json or elm-package.json!"
53
- end
54
-
55
- def updated_elm_package_content(file)
56
- ElmPackageUpdater.new(
57
- dependencies: dependencies,
58
- elm_package_file: file
59
- ).updated_elm_package_file_content
60
- end
61
-
62
- def updated_elm_json_content(file)
63
- ElmJsonUpdater.new(
64
- dependencies: dependencies,
65
- elm_json_file: file
66
- ).updated_content
67
- end
68
-
69
- def elm_package_files
70
- dependency_files.select { |f| f.name.end_with?("elm-package.json") }
71
- end
72
-
73
- def elm_json_files
74
- dependency_files.select { |f| f.name.end_with?("elm.json") }
75
- end
76
- end
77
- end
78
- end
79
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dependabot/file_updaters/elm/elm_package"
4
-
5
- module Dependabot
6
- module FileUpdaters
7
- module Elm
8
- class ElmPackage
9
- class ElmJsonUpdater
10
- def initialize(elm_json_file:, dependencies:)
11
- @elm_json_file = elm_json_file
12
- @dependencies = dependencies
13
- end
14
-
15
- def updated_content
16
- dependencies.
17
- select { |dep| requirement_changed?(elm_json_file, dep) }.
18
- reduce(elm_json_file.content.dup) do |content, dep|
19
- updated_content = content
20
-
21
- updated_content = update_requirement(
22
- content: updated_content,
23
- filename: elm_json_file.name,
24
- dependency: dep
25
- )
26
-
27
- next updated_content unless content == updated_content
28
-
29
- raise "Expected content to change!"
30
- end
31
- end
32
-
33
- private
34
-
35
- attr_reader :elm_json_file, :dependencies
36
-
37
- def requirement_changed?(file, dependency)
38
- changed_requirements =
39
- dependency.requirements - dependency.previous_requirements
40
-
41
- changed_requirements.any? { |f| f[:file] == file.name }
42
- end
43
-
44
- def update_requirement(content:, filename:, dependency:)
45
- updated_req =
46
- dependency.requirements.
47
- find { |r| r.fetch(:file) == filename }.
48
- fetch(:requirement)
49
-
50
- old_req =
51
- dependency.previous_requirements.
52
- find { |r| r.fetch(:file) == filename }.
53
- fetch(:requirement)
54
-
55
- return content unless old_req
56
-
57
- dep = dependency
58
- regex =
59
- /"#{Regexp.quote(dep.name)}"\s*:\s+"#{Regexp.quote(old_req)}"/
60
-
61
- content.gsub(regex) do |declaration|
62
- declaration.gsub(%("#{old_req}"), %("#{updated_req}"))
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dependabot/file_updaters/elm/elm_package"
4
-
5
- module Dependabot
6
- module FileUpdaters
7
- module Elm
8
- class ElmPackage
9
- class ElmPackageUpdater
10
- def initialize(elm_package_file:, dependencies:)
11
- @elm_package_file = elm_package_file
12
- @dependencies = dependencies
13
- end
14
-
15
- def updated_elm_package_file_content
16
- dependencies.
17
- select { |dep| requirement_changed?(elm_package_file, dep) }.
18
- reduce(elm_package_file.content.dup) do |content, dep|
19
- updated_content = content
20
-
21
- updated_content = update_requirement(
22
- content: updated_content,
23
- filename: elm_package_file.name,
24
- dependency: dep
25
- )
26
-
27
- next updated_content unless content == updated_content
28
-
29
- raise "Expected content to change!"
30
- end
31
- end
32
-
33
- private
34
-
35
- attr_reader :elm_package_file, :dependencies
36
-
37
- def requirement_changed?(file, dependency)
38
- changed_requirements =
39
- dependency.requirements - dependency.previous_requirements
40
-
41
- changed_requirements.any? { |f| f[:file] == file.name }
42
- end
43
-
44
- def update_requirement(content:, filename:, dependency:)
45
- updated_req =
46
- dependency.requirements.
47
- find { |r| r.fetch(:file) == filename }.
48
- fetch(:requirement)
49
-
50
- old_req =
51
- dependency.previous_requirements.
52
- find { |r| r.fetch(:file) == filename }.
53
- fetch(:requirement)
54
-
55
- return content unless old_req
56
-
57
- dep = dependency
58
- regex =
59
- /"#{Regexp.quote(dep.name)}"\s*:\s+"#{Regexp.quote(old_req)}"/
60
-
61
- content.gsub(regex) do |declaration|
62
- declaration.gsub(%("#{old_req}"), %("#{updated_req}"))
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dependabot/metadata_finders/base"
4
- require "dependabot/shared_helpers"
5
-
6
- module Dependabot
7
- module MetadataFinders
8
- module Elm
9
- class ElmPackage < Dependabot::MetadataFinders::Base
10
- private
11
-
12
- def look_up_source
13
- # For Elm 0.18 an elm-package is guaranteed to be `owner/name`
14
- # on github. For 0.19 a lot will change, including the name of
15
- # the dependency file, so I won't try to build something more
16
- # sophisticated here for now.
17
- Source.from_url("https://github.com/" + dependency.name)
18
- end
19
- end
20
- end
21
- end
22
- end
@@ -1,126 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "excon"
4
- require "dependabot/update_checkers/base"
5
- require "dependabot/shared_helpers"
6
- require "dependabot/errors"
7
-
8
- module Dependabot
9
- module UpdateCheckers
10
- module Elm
11
- class ElmPackage < Dependabot::UpdateCheckers::Base
12
- require_relative "elm_package/requirements_updater"
13
- require_relative "elm_package/elm_18_version_resolver"
14
- require_relative "elm_package/elm_19_version_resolver"
15
-
16
- def latest_version
17
- @latest_version ||= candidate_versions.max
18
- end
19
-
20
- # Overwrite the base class to allow multi-dependency update PRs for
21
- # dependencies for which we don't have a version.
22
- def can_update?(requirements_to_unlock:)
23
- if dependency.appears_in_lockfile?
24
- version_can_update?(requirements_to_unlock: requirements_to_unlock)
25
- elsif requirements_to_unlock == :none
26
- false
27
- elsif requirements_to_unlock == :own
28
- requirements_can_update?
29
- elsif requirements_to_unlock == :all
30
- updated_dependencies_after_full_unlock.any?
31
- end
32
- end
33
-
34
- def latest_resolvable_version
35
- @latest_resolvable_version ||=
36
- version_resolver.
37
- latest_resolvable_version(unlock_requirement: :own)
38
- end
39
-
40
- def latest_resolvable_version_with_no_unlock
41
- # Irrelevant, since Elm has a single dependency file (well, there's
42
- # also `exact-dependencies.json`, but it's not recommended that that
43
- # is committed).
44
- nil
45
- end
46
-
47
- def updated_requirements
48
- RequirementsUpdater.new(
49
- requirements: dependency.requirements,
50
- latest_resolvable_version: latest_resolvable_version
51
- ).updated_requirements
52
- end
53
-
54
- private
55
-
56
- def version_resolver
57
- @version_resolver ||=
58
- if dependency.requirements.any? { |r| r.fetch(:file) == "elm.json" }
59
- Elm19VersionResolver.new(
60
- dependency: dependency,
61
- dependency_files: dependency_files
62
- )
63
- else
64
- Elm18VersionResolver.new(
65
- dependency: dependency,
66
- dependency_files: dependency_files,
67
- candidate_versions: candidate_versions
68
- )
69
- end
70
- end
71
-
72
- def updated_dependencies_after_full_unlock
73
- version_resolver.updated_dependencies_after_full_unlock
74
- end
75
-
76
- def latest_version_resolvable_with_full_unlock?
77
- latest_version == version_resolver.
78
- latest_resolvable_version(unlock_requirement: :all)
79
- end
80
-
81
- def candidate_versions
82
- all_versions.
83
- reject { |v| ignore_reqs.any? { |r| r.satisfied_by?(v) } }
84
- end
85
-
86
- def all_versions
87
- return @all_versions if @version_lookup_attempted
88
-
89
- @version_lookup_attempted = true
90
-
91
- response = Excon.get(
92
- "https://package.elm-lang.org/packages/#{dependency.name}/"\
93
- "releases.json",
94
- idempotent: true,
95
- **Dependabot::SharedHelpers.excon_defaults
96
- )
97
-
98
- return @all_versions = [] unless response.status == 200
99
-
100
- @all_versions =
101
- JSON.parse(response.body).
102
- keys.
103
- map { |v| version_class.new(v) }.
104
- sort
105
- end
106
-
107
- # Overwrite the base class's requirements_up_to_date? method to instead
108
- # check whether the latest version is allowed
109
- def requirements_up_to_date?
110
- return false unless latest_version
111
-
112
- dependency.requirements.
113
- map { |r| r.fetch(:requirement) }.
114
- map { |r| requirement_class.new(r) }.
115
- all? { |r| r.satisfied_by?(latest_version) }
116
- end
117
-
118
- def ignore_reqs
119
- # Note: we use Gem::Requirement here because ignore conditions will
120
- # be passed as Ruby ranges
121
- ignored_versions.map { |req| Gem::Requirement.new(req.split(",")) }
122
- end
123
- end
124
- end
125
- end
126
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dependabot/utils/elm/version"
4
- require "dependabot/update_checkers/elm/elm_package"
5
-
6
- module Dependabot
7
- module UpdateCheckers
8
- module Elm
9
- class ElmPackage
10
- class CliParser
11
- INSTALL_DEPENDENCY_REGEX =
12
- %r{([^\s]+\/[^\s]+)\s+(\d+\.\d+\.\d+)}.freeze
13
- UPGRADE_DEPENDENCY_REGEX =
14
- %r{([^\s]+\/[^\s]+) \(\d+\.\d+\.\d+ => (\d+\.\d+\.\d+)\)}.freeze
15
-
16
- def self.decode_install_preview(text)
17
- installs = {}
18
-
19
- # Parse new installs
20
- text.scan(INSTALL_DEPENDENCY_REGEX).
21
- each { |n, v| installs[n] = Utils::Elm::Version.new(v) }
22
-
23
- # Parse upgrades
24
- text.scan(UPGRADE_DEPENDENCY_REGEX).
25
- each { |n, v| installs[n] = Utils::Elm::Version.new(v) }
26
-
27
- installs
28
- end
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,234 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "dependabot/shared_helpers"
4
- require "dependabot/errors"
5
- require "dependabot/file_parsers/elm/elm_package"
6
- require "dependabot/update_checkers/elm/elm_package"
7
- require "dependabot/update_checkers/elm/elm_package/cli_parser"
8
- require "dependabot/update_checkers/elm/elm_package/requirements_updater"
9
- require "dependabot/utils/elm/requirement"
10
-
11
- module Dependabot
12
- module UpdateCheckers
13
- module Elm
14
- class ElmPackage
15
- class Elm18VersionResolver
16
- class UnrecoverableState < StandardError; end
17
-
18
- def initialize(dependency:, dependency_files:, candidate_versions:)
19
- @dependency = dependency
20
- @dependency_files = dependency_files
21
- @candidate_versions = candidate_versions
22
- end
23
-
24
- def latest_resolvable_version(unlock_requirement:)
25
- unless %i(none own all).include?(unlock_requirement)
26
- raise "Invalid unlock setting: #{unlock_requirement}"
27
- end
28
-
29
- # Elm has no lockfile, so we will never create an update PR if
30
- # unlock requirements are `none`. Just return the current version.
31
- return current_version if unlock_requirement == :none
32
-
33
- # Otherwise, we gotta check a few conditions to see if bumping
34
- # wouldn't also bump other deps in elm-package.json
35
- candidate_versions.sort.reverse_each do |version|
36
- return version if can_update?(version, unlock_requirement)
37
- end
38
-
39
- # Fall back to returning the dependency's current version, which is
40
- # presumed to be resolvable
41
- current_version
42
- end
43
-
44
- def updated_dependencies_after_full_unlock
45
- version = latest_resolvable_version(unlock_requirement: :all)
46
- deps_after_install = fetch_install_metadata(target_version: version)
47
-
48
- original_dependency_details.map do |original_dep|
49
- new_version = deps_after_install.fetch(original_dep.name)
50
-
51
- old_reqs = original_dep.requirements.map do |req|
52
- requirement_class.new(req[:requirement])
53
- end
54
-
55
- next if old_reqs.all? { |req| req.satisfied_by?(new_version) }
56
-
57
- new_requirements =
58
- RequirementsUpdater.new(
59
- requirements: original_dep.requirements,
60
- latest_resolvable_version: new_version.to_s
61
- ).updated_requirements
62
-
63
- Dependency.new(
64
- name: original_dep.name,
65
- version: new_version.to_s,
66
- requirements: new_requirements,
67
- previous_version: original_dep.version,
68
- previous_requirements: original_dep.requirements,
69
- package_manager: original_dep.package_manager
70
- )
71
- end.compact
72
- end
73
-
74
- private
75
-
76
- attr_reader :dependency, :dependency_files, :candidate_versions
77
-
78
- def can_update?(version, unlock_requirement)
79
- deps_after_install = fetch_install_metadata(target_version: version)
80
-
81
- result = check_install_result(deps_after_install, version)
82
-
83
- # If the install was clean then we can definitely update
84
- return true if result == :clean_bump
85
-
86
- # Otherwise, we can still update if the result was a forced full
87
- # unlock and we're allowed to unlock other requirements
88
- return false unless unlock_requirement == :all
89
-
90
- result == :forced_full_unlock_bump
91
- end
92
-
93
- def check_install_result(deps_after_install, target_version)
94
- # This can go one of 5 ways:
95
- # 1) We bump our dep and no other dep is bumped
96
- # 2) We bump our dep and another dep is bumped too
97
- # Scenario: NoRedInk/datetimepicker bump to 3.0.2 also
98
- # bumps elm-css to 14
99
- # 3) We bump our dep but actually elm-package doesn't bump it
100
- # Scenario: elm-css bump to 14 but datetimepicker is at 3.0.1
101
- # 4) We bump our dep but elm-package just says
102
- # "Packages configured successfully!"
103
- # Narrator: they weren't
104
- # Scenario: impossible dependency (i.e. elm-css 999.999.999)
105
- # a <= v < b where a is greater than latest version
106
- # 5) We bump our dep but elm-package blows up (not handled here)
107
- # Scenario: rtfeldman/elm-css 14 && rtfeldman/hashed-class 1.0.0
108
- # I'm not sure what's different from this scenario
109
- # to 3), why it blows up instead of just rolling
110
- # elm-css back to version 9 which is what
111
- # hashed-class requires
112
-
113
- # 4) We bump our dep but elm-package just says
114
- # "Packages configured successfully!"
115
- return :empty_elm_stuff_bug if deps_after_install.empty?
116
-
117
- version_after_install = deps_after_install.fetch(dependency.name)
118
-
119
- # 3) We bump our dep but actually elm-package doesn't bump it
120
- return :downgrade_bug if version_after_install < target_version
121
-
122
- other_top_level_deps_bumped =
123
- original_dependency_details.
124
- reject { |dep| dep.name == dependency.name }.
125
- select do |dep|
126
- reqs = dep.requirements.map { |r| r.fetch(:requirement) }
127
- reqs = reqs.map { |r| requirement_class.new(r) }
128
- reqs.any? { |r| !r.satisfied_by?(deps_after_install[dep.name]) }
129
- end
130
-
131
- # 2) We bump our dep and another dep is bumped
132
- return :forced_full_unlock_bump if other_top_level_deps_bumped.any?
133
-
134
- # 1) We bump our dep and no other dep is bumped
135
- :clean_bump
136
- end
137
-
138
- def fetch_install_metadata(target_version:)
139
- @install_cache ||= {}
140
- @install_cache[target_version.to_s] ||=
141
- SharedHelpers.in_a_temporary_directory do
142
- write_temporary_dependency_files(target_version: target_version)
143
-
144
- # Elm package install outputs a preview of the actions to be
145
- # performed. We can use this preview to calculate whether it
146
- # would do anything funny
147
- command = "yes n | elm-package install"
148
- response = run_shell_command(command)
149
-
150
- deps_after_install = CliParser.decode_install_preview(response)
151
-
152
- deps_after_install
153
- rescue SharedHelpers::HelperSubprocessFailed => error
154
- # 5) We bump our dep but elm-package blows up
155
- handle_elm_package_errors(error)
156
- end
157
- end
158
-
159
- def run_shell_command(command)
160
- raw_response = nil
161
- IO.popen(command, err: %i(child out)) do |process|
162
- raw_response = process.read
163
- end
164
-
165
- # Raise an error with the output from the shell session if Elm
166
- # returns a non-zero status
167
- return raw_response if $CHILD_STATUS.success?
168
-
169
- raise SharedHelpers::HelperSubprocessFailed.new(
170
- raw_response,
171
- command
172
- )
173
- end
174
-
175
- def handle_elm_package_errors(error)
176
- if error.message.include?("I cannot find a set of packages that " \
177
- "works with your constraints")
178
- raise Dependabot::DependencyFileNotResolvable, error.message
179
- end
180
-
181
- # I don't know any other errors
182
- raise error
183
- end
184
-
185
- def write_temporary_dependency_files(target_version:)
186
- dependency_files.each do |file|
187
- path = file.name
188
- FileUtils.mkdir_p(Pathname.new(path).dirname)
189
-
190
- File.write(
191
- path,
192
- updated_elm_package_content(file.content, target_version)
193
- )
194
- end
195
- end
196
-
197
- def updated_elm_package_content(content, version)
198
- json = JSON.parse(content)
199
-
200
- new_requirement = RequirementsUpdater.new(
201
- requirements: dependency.requirements,
202
- latest_resolvable_version: version.to_s
203
- ).updated_requirements.first[:requirement]
204
-
205
- json["dependencies"][dependency.name] = new_requirement
206
- JSON.dump(json)
207
- end
208
-
209
- def original_dependency_details
210
- @original_dependency_details ||=
211
- FileParsers::Elm::ElmPackage.new(
212
- dependency_files: dependency_files,
213
- source: nil
214
- ).parse
215
- end
216
-
217
- def current_version
218
- return unless dependency.version
219
-
220
- version_class.new(dependency.version)
221
- end
222
-
223
- def version_class
224
- Utils::Elm::Version
225
- end
226
-
227
- def requirement_class
228
- Utils::Elm::Requirement
229
- end
230
- end
231
- end
232
- end
233
- end
234
- end