Skip to content

Commit 2723b34

Browse files
Copilota-schur
andcommitted
Add validation for unsupported dependency-type option
Co-authored-by: a-schur <227858738+a-schur@users.noreply.github.com>
1 parent 2190d0d commit 2723b34

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

updater/lib/dependabot/dependency_group_engine.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ class DependencyGroupEngine
2525

2626
class ConfigurationError < StandardError; end
2727

28+
# Package managers that support the dependency-type option in group rules
29+
PACKAGE_MANAGERS_SUPPORTING_DEPENDENCY_TYPE = T.let(
30+
%w[bundler composer hex maven npm_and_yarn pip uv].freeze,
31+
T::Array[String]
32+
)
33+
2834
sig { params(job: Dependabot::Job).returns(Dependabot::DependencyGroupEngine) }
2935
def self.from_job_config(job:)
36+
validate_group_configuration!(job)
37+
3038
groups = job.dependency_groups.map do |group|
3139
Dependabot::DependencyGroup.new(name: group["name"], rules: group["rules"], applies_to: group["applies-to"])
3240
end
@@ -41,6 +49,25 @@ def self.from_job_config(job:)
4149
new(dependency_groups: groups)
4250
end
4351

52+
sig { params(job: Dependabot::Job).void }
53+
def self.validate_group_configuration!(job)
54+
return unless job.dependency_groups.any?
55+
56+
unsupported_groups = job.dependency_groups.select do |group|
57+
rules = group["rules"] || {}
58+
rules.key?("dependency-type") &&
59+
!PACKAGE_MANAGERS_SUPPORTING_DEPENDENCY_TYPE.include?(job.package_manager)
60+
end
61+
62+
return unless unsupported_groups.any?
63+
64+
group_names = unsupported_groups.map { |g| g["name"] }.join(", ")
65+
raise ConfigurationError,
66+
"The 'dependency-type' option is not supported for the '#{job.package_manager}' package manager. " \
67+
"It is only supported for: #{PACKAGE_MANAGERS_SUPPORTING_DEPENDENCY_TYPE.join(', ')}. " \
68+
"Affected groups: #{group_names}"
69+
end
70+
4471
sig { returns(T::Array[Dependabot::DependencyGroup]) }
4572
attr_reader :dependency_groups
4673

updater/spec/dependabot/dependency_group_engine_spec.rb

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,141 @@
480480
end
481481
end
482482
end
483+
484+
describe "::from_job_config validation" do
485+
let(:dependency_groups_config) do
486+
[
487+
{
488+
"name" => "test-group",
489+
"rules" => {
490+
"dependency-type" => "production"
491+
}
492+
}
493+
]
494+
end
495+
496+
context "when dependency-type is used with a supported package manager" do
497+
%w[bundler composer hex maven npm_and_yarn pip uv].each do |package_manager|
498+
context "with #{package_manager}" do
499+
let(:job) do
500+
instance_double(
501+
Dependabot::Job,
502+
dependency_groups: dependency_groups_config,
503+
source: source,
504+
dependencies: nil,
505+
security_updates_only?: false,
506+
package_manager: package_manager
507+
)
508+
end
509+
510+
it "does not raise an error" do
511+
expect { dependency_group_engine }.not_to raise_error
512+
end
513+
end
514+
end
515+
end
516+
517+
context "when dependency-type is used with an unsupported package manager" do
518+
%w[gradle go_modules cargo docker terraform].each do |package_manager|
519+
context "with #{package_manager}" do
520+
let(:job) do
521+
instance_double(
522+
Dependabot::Job,
523+
dependency_groups: dependency_groups_config,
524+
source: source,
525+
dependencies: nil,
526+
security_updates_only?: false,
527+
package_manager: package_manager
528+
)
529+
end
530+
531+
it "raises a ConfigurationError" do
532+
expect { dependency_group_engine }.to raise_error(
533+
Dependabot::DependencyGroupEngine::ConfigurationError,
534+
/The 'dependency-type' option is not supported for the '#{package_manager}' package manager/
535+
)
536+
end
537+
538+
it "includes the group name in the error message" do
539+
expect { dependency_group_engine }.to raise_error(
540+
Dependabot::DependencyGroupEngine::ConfigurationError,
541+
/Affected groups: test-group/
542+
)
543+
end
544+
545+
it "lists supported package managers in the error message" do
546+
expect { dependency_group_engine }.to raise_error(
547+
Dependabot::DependencyGroupEngine::ConfigurationError,
548+
/bundler, composer, hex, maven, npm_and_yarn, pip, uv/
549+
)
550+
end
551+
end
552+
end
553+
end
554+
555+
context "when multiple groups use dependency-type with an unsupported package manager" do
556+
let(:dependency_groups_config) do
557+
[
558+
{
559+
"name" => "group-one",
560+
"rules" => {
561+
"dependency-type" => "production"
562+
}
563+
},
564+
{
565+
"name" => "group-two",
566+
"rules" => {
567+
"dependency-type" => "development"
568+
}
569+
}
570+
]
571+
end
572+
573+
let(:job) do
574+
instance_double(
575+
Dependabot::Job,
576+
dependency_groups: dependency_groups_config,
577+
source: source,
578+
dependencies: nil,
579+
security_updates_only?: false,
580+
package_manager: "gradle"
581+
)
582+
end
583+
584+
it "raises an error mentioning all affected groups" do
585+
expect { dependency_group_engine }.to raise_error(
586+
Dependabot::DependencyGroupEngine::ConfigurationError,
587+
/Affected groups: group-one, group-two/
588+
)
589+
end
590+
end
591+
592+
context "when groups don't use dependency-type with an unsupported package manager" do
593+
let(:dependency_groups_config) do
594+
[
595+
{
596+
"name" => "test-group",
597+
"rules" => {
598+
"patterns" => ["dummy-*"]
599+
}
600+
}
601+
]
602+
end
603+
604+
let(:job) do
605+
instance_double(
606+
Dependabot::Job,
607+
dependency_groups: dependency_groups_config,
608+
source: source,
609+
dependencies: nil,
610+
security_updates_only?: false,
611+
package_manager: "gradle"
612+
)
613+
end
614+
615+
it "does not raise an error" do
616+
expect { dependency_group_engine }.not_to raise_error
617+
end
618+
end
619+
end
483620
end

0 commit comments

Comments
 (0)