Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform update_types in IgnoreCondition #3550

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions common/lib/dependabot/config/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,13 @@ def self.parse(config)
"terraform" => "terraform"
}.freeze

UPDATE_TYPE_LOOKUP = {
"version-update:semver-patch" => :ignore_patch_versions,
"version-update:semver-minor" => :ignore_minor_versions,
"version-update:semver-major" => :ignore_major_versions
}.freeze

def ignore_conditions(cfg)
ignores = cfg&.dig(:ignore) || []
ignores.map do |ic|
update_types = ic[:"update-types"]&.
map { |t| UPDATE_TYPE_LOOKUP[t.downcase.strip] }&.
compact
Dependabot::Config::IgnoreCondition.new(
dependency_name: ic[:"dependency-name"],
versions: ic[:versions],
update_types: update_types
update_types: ic[:"update-types"]
)
end
end
Expand Down
25 changes: 14 additions & 11 deletions common/lib/dependabot/config/ignore_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,40 @@ module Dependabot
module Config
# Filters versions that should not be considered for dependency updates
class IgnoreCondition
UPDATE_TYPES = %i(
ignore_major_versions
ignore_minor_versions
ignore_patch_versions
).freeze
PATCH_VERSION_TYPE = "version-update:semver-patch"
MINOR_VERSION_TYPE = "version-update:semver-minor"
MAJOR_VERSION_TYPE = "version-update:semver-major"

ALL_VERSIONS = ">= 0"

attr_reader :dependency_name, :versions, :update_types

def initialize(dependency_name:, versions: nil, update_types: nil)
@dependency_name = dependency_name
@versions = versions || []
@update_types = update_types || []
end

def ignored_versions(dependency)
return [ALL_VERSIONS] if @versions.empty? && @update_types.empty?
return [ALL_VERSIONS] if versions.empty? && transformed_update_types.empty?

versions_by_type(dependency) + @versions
versions_by_type(dependency) + versions
end

private

def transformed_update_types
update_types.map(&:downcase).map(&:strip).compact
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
update_types.map(&:downcase).map(&:strip).compact
update_types.compact.map(&:downcase).map(&:strip)

Nit: this compact does nothing, as nil values have already raised?

irb(main):002:0> [nil].map(&:downcase)
(irb):2:in `map': undefined method `downcase' for nil:NilClass (NoMethodError)

I'm not sure if it should be refactored to be a guard for the map()s (the suggestion), or just removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: should this transform happen in the ConfigFile / parsing logic?

We might be able to simplify by assuming the caller of IgnoreCondition has normalized these values: either through ConfigFile or the mysterious server-side parsing process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Yeah true, might be better to move to the File class and rely on the api to have done this already

end

def versions_by_type(dependency)
@update_types.flat_map do |t|
transformed_update_types.flat_map do |t|
case t
when :ignore_patch_versions
when PATCH_VERSION_TYPE
ignore_patch(dependency.version)
when :ignore_minor_versions
when MINOR_VERSION_TYPE
ignore_minor(dependency.version)
when :ignore_major_versions
when MAJOR_VERSION_TYPE
ignore_major(dependency.version)
else
[]
Expand Down
4 changes: 2 additions & 2 deletions common/spec/dependabot/config/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
expect(update_config.ignore_conditions.length).to eq(3)
end

it "maps update-types string" do
it "passes update-types" do
types_ignore = update_config.ignore_conditions.find { |ic| ic.dependency_name == "@types/node" }
expect(types_ignore.update_types).to eq([:ignore_patch_versions])
expect(types_ignore.update_types).to eq(["version-update:semver-patch"])
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions common/spec/dependabot/config/ignore_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def expect_ignored(versions)
let(:major_upgrades) { %w(2 2.0 2.0.0) }

context "with ignore_patch_versions" do
let(:update_types) { [:ignore_patch_versions] }
let(:update_types) { ["version-update:semver-patch"] }

it "ignores expected versions" do
expect_allowed(minor_upgrades + major_upgrades)
Expand All @@ -81,7 +81,7 @@ def expect_ignored(versions)
end

context "with ignore_minor_versions" do
let(:update_types) { [:ignore_minor_versions] }
let(:update_types) { ["version-update:semver-minor"] }

it "ignores expected versions" do
expect_allowed(patch_upgrades + major_upgrades)
Expand All @@ -94,7 +94,7 @@ def expect_ignored(versions)
end

context "with ignore_major_versions" do
let(:update_types) { [:ignore_major_versions] }
let(:update_types) { ["version-update:semver-major"] }

it "ignores expected versions" do
expect_allowed(patch_upgrades + minor_upgrades)
Expand All @@ -107,7 +107,7 @@ def expect_ignored(versions)
end

context "with ignore_major_versions and ignore_patch_versions" do
let(:update_types) { %i(ignore_major_versions ignore_patch_versions) }
let(:update_types) { %w(version-update:semver-major version-update:semver-patch) }

it "ignores expected versions" do
expect_allowed(minor_upgrades)
Expand All @@ -119,7 +119,7 @@ def expect_ignored(versions)
let(:dependency_version) { "1.2" }

context "with ignore_major_versions" do
let(:update_types) { [:ignore_major_versions] }
let(:update_types) { ["version-update:semver-major"] }

it "ignores expected versions" do
expect_allowed(patch_upgrades + minor_upgrades)
Expand All @@ -132,7 +132,7 @@ def expect_ignored(versions)
end

context "with ignore_minor_versions" do
let(:update_types) { [:ignore_minor_versions] }
let(:update_types) { ["version-update:semver-minor"] }

it "ignores expected versions" do
expect_allowed(patch_upgrades + major_upgrades)
Expand All @@ -145,7 +145,7 @@ def expect_ignored(versions)
end

context "with ignore_patch_versions" do
let(:update_types) { [:ignore_patch_versions] }
let(:update_types) { ["version-update:semver-patch"] }

it "ignores expected versions" do
expect_allowed(patch_upgrades + major_upgrades + minor_upgrades)
Expand All @@ -161,23 +161,23 @@ def expect_ignored(versions)
let(:dependency_version) { "1" }

context "with ignore_major_versions" do
let(:update_types) { [:ignore_major_versions] }
let(:update_types) { ["version-update:semver-major"] }

it "returns the expected range" do
expect(ignored_versions).to eq([])
end
end

context "with ignore_minor_versions" do
let(:update_types) { [:ignore_minor_versions] }
let(:update_types) { ["version-update:semver-minor"] }

it "returns the expected range" do
expect(ignored_versions).to eq([])
end
end

context "with ignore_patch_versions" do
let(:update_types) { [:ignore_patch_versions] }
let(:update_types) { ["version-update:semver-patch"] }

it "returns the expected range" do
expect(ignored_versions).to eq([])
Expand All @@ -189,14 +189,14 @@ def expect_ignored(versions)
let(:dependency_version) { "Finchley.SR3" }

context "with ignore_patch_versions" do
let(:update_types) { [:ignore_patch_versions] }
let(:update_types) { ["version-update:semver-patch"] }
it "returns the expected range" do
expect(ignored_versions).to eq([])
end
end

context "with ignore_minor_versions" do
let(:update_types) { [:ignore_minor_versions] }
let(:update_types) { ["version-update:semver-minor"] }
it "returns the expected range" do
expect(ignored_versions).to eq([">= Finchley.a, < Finchley.999999"])
end
Expand Down
10 changes: 5 additions & 5 deletions common/spec/dependabot/config/update_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
let(:ignore_conditions) do
[Dependabot::Config::IgnoreCondition.new(dependency_name: "@types/node",
versions: [">= 14.14.x, < 15"],
update_types: [:ignore_minor_versions])]
update_types: ["version-update:semver-minor"])]
end

it "returns versions" do
Expand All @@ -74,11 +74,11 @@
[
Dependabot::Config::IgnoreCondition.new(
dependency_name: "@types/node",
update_types: [:ignore_minor_versions]
update_types: ["version-update:semver-minor"]
),
Dependabot::Config::IgnoreCondition.new(
dependency_name: "@types/node",
update_types: [:ignore_minor_versions]
update_types: ["version-update:semver-minor"]
)
]
end
Expand All @@ -93,11 +93,11 @@
[
Dependabot::Config::IgnoreCondition.new(
dependency_name: "@types/*",
update_types: [:ignore_major_versions]
update_types: ["version-update:semver-major"]
),
Dependabot::Config::IgnoreCondition.new(
dependency_name: "@types/node",
update_types: [:ignore_minor_versions]
update_types: ["version-update:semver-minor"]
)
]
end
Expand Down