forked from dependabot/dependabot-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependency_group.rb
91 lines (72 loc) · 2.65 KB
/
dependency_group.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# typed: strict
# frozen_string_literal: true
require "dependabot/experiments"
require "dependabot/config/ignore_condition"
require "dependabot/logger"
require "sorbet-runtime"
require "wildcard_matcher"
require "yaml"
module Dependabot
class DependencyGroup
extend T::Sig
sig { returns(String) }
attr_reader :name
sig { returns(T::Hash[String, T.any(String, T::Array[String])]) }
attr_reader :rules
sig { returns(T::Array[Dependabot::Dependency]) }
attr_reader :dependencies
sig do
params(
name: String,
rules: T::Hash[String, T.untyped]
)
.void
end
def initialize(name:, rules:)
@name = name
@rules = rules
@dependencies = T.let([], T::Array[Dependabot::Dependency])
end
sig { params(dependency: Dependabot::Dependency).returns(T::Boolean) }
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
sig { returns(T::Hash[String, String]) }
def to_h
{ "name" => name }
end
# Provides a debug utility to view the group as it appears in the config file.
sig { returns(String) }
def to_config_yaml
{
"groups" => { name => rules }
}.to_yaml.delete_prefix("---\n")
end
private
sig { params(dependency_name: String).returns(T::Boolean) }
def matches_pattern?(dependency_name)
return true unless rules.key?("patterns") # If no patterns are defined, we pass this check by default
T.unsafe(rules["patterns"]).any? { |rule| WildcardMatcher.match?(rule, dependency_name) }
end
sig { params(dependency_name: String).returns(T::Boolean) }
def matches_excluded_pattern?(dependency_name)
return false unless rules.key?("exclude-patterns") # If there are no exclusions, fail by default
T.unsafe(rules["exclude-patterns"]).any? { |rule| WildcardMatcher.match?(rule, dependency_name) }
end
sig { params(dependency: Dependabot::Dependency).returns(T::Boolean) }
def matches_dependency_type?(dependency)
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
sig { returns(T::Boolean) }
def experimental_rules_enabled?
Dependabot::Experiments.enabled?(:grouped_updates_experimental_rules)
end
end
end