Skip to content

Commit

Permalink
Decouple patterns and exclude-patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
brrygrdn committed Jul 24, 2023
1 parent 2f9a3db commit e03674b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
22 changes: 9 additions & 13 deletions common/lib/dependabot/dependency_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(name:, rules:)

def contains?(dependency)
return true if @dependencies.include?(dependency)
return false if matches_excluded_pattern?(dependency.name)

matches_pattern?(dependency.name) && matches_dependency_type?(dependency)
end
Expand All @@ -33,30 +34,25 @@ def to_config_yaml
private

def matches_pattern?(dependency_name)
return true unless pattern_rules? # If no patterns are defined, we pass this check by default
return true unless rules.key?("patterns") # If no patterns are defined, we pass this check by default

positive_match = rules["patterns"].any? { |rule| WildcardMatcher.match?(rule, dependency_name) }
negative_match = rules["exclude-patterns"]&.any? { |rule| WildcardMatcher.match?(rule, dependency_name) }
rules["patterns"].any? { |rule| WildcardMatcher.match?(rule, dependency_name) }
end

def matches_excluded_pattern?(dependency_name)
return false unless rules.key?("exclude-patterns") # If there are no exclusions, fail by default

positive_match && !negative_match
rules["exclude-patterns"].any? { |rule| WildcardMatcher.match?(rule, dependency_name) }
end

def matches_dependency_type?(dependency)
return true unless dependency_type_rules? # If no dependency-type is set, match by default
return true unless rules.key?("dependency-type") # If no dependency-type is set, match by default

rules["dependency-type"] == if dependency.production?
"production"
else
"development"
end
end

def pattern_rules?
rules.key?("patterns") && rules["patterns"]&.any?
end

def dependency_type_rules?
rules.key?("dependency-type")
end
end
end
15 changes: 14 additions & 1 deletion common/spec/dependabot/dependency_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,22 @@
expect(dependency_group.contains?(test_dependency1)).to be_falsey
expect(dependency_group.contains?(test_dependency2)).to be_falsey
end

context "when a dependency is specifically excluded" do
let(:rules) do
{
"dependency-type" => "production",
"exclude-patterns" => [production_dependency.name]
}
end

it "returns false even if the dependency matches the specified type" do
expect(dependency_group.contains?(production_dependency)).to be_falsey
end
end
end

context "when the rules specify a mix of dependency-types" do
context "when the rules specify a mix of conditions" do
let(:rules) do
{
"patterns" => ["*dependency*"],
Expand Down

0 comments on commit e03674b

Please sign in to comment.