Class: Dependabot::PullRequestCreator::BranchNameTemplate
- Inherits:
-
Object
- Object
- Dependabot::PullRequestCreator::BranchNameTemplate
- Extended by:
- T::Sig
- Defined in:
- lib/dependabot/pull_request_creator/branch_name_template.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- SOLO_PLACEHOLDERS =
%w(prefix package_manager directory target_branch dependency version name).freeze
- GROUP_PLACEHOLDERS =
%w(prefix package_manager directory target_branch group_name name).freeze
- MULTI_ECO_PLACEHOLDERS =
%w(prefix target_branch group_name name).freeze
Class Method Summary collapse
- .allowed_placeholders(strategy) ⇒ Object
- .collect_template_errors(template, allowed, strategy) ⇒ Object
- .render(template, vars, strategy:, digest: nil) ⇒ Object
-
.validate_ref_name(name) ⇒ Object
rubocop:disable Naming/PredicateMethod.
-
.validate_template(template, strategy:) ⇒ Object
rubocop:disable Naming/PredicateMethod.
Class Method Details
.allowed_placeholders(strategy) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/dependabot/pull_request_creator/branch_name_template.rb', line 38 def self.allowed_placeholders(strategy) case strategy when :solo then SOLO_PLACEHOLDERS when :group then GROUP_PLACEHOLDERS when :multi_ecosystem then MULTI_ECO_PLACEHOLDERS else raise Error, "Unknown strategy: #{strategy}" end end |
.collect_template_errors(template, allowed, strategy) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/dependabot/pull_request_creator/branch_name_template.rb', line 49 def self.collect_template_errors(template, allowed, strategy) used = template.scan(/\{(\w+)\}/).flatten unknown = used.uniq.reject { |t| allowed.include?(t) } unknown -= ["package_manager"] if strategy == :multi_ecosystem && used.include?("package_manager") malformed = template.gsub(/\{\w+\}/, "").match?(/[{}]/) = T.let([], T::Array[String]) << "Unknown placeholder(s): #{unknown.map { |v| "{#{v}}" }.join(', ')}" unless unknown.empty? << "Malformed or unclosed braces detected." if malformed if strategy == :multi_ecosystem && used.include?("package_manager") << "{package_manager} is not available for multi-ecosystem groups (spans multiple ecosystems)." end end |
.render(template, vars, strategy:, digest: nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/dependabot/pull_request_creator/branch_name_template.rb', line 104 def self.render(template, vars, strategy:, digest: nil) validate_template(template, strategy: strategy) # Substitute placeholders name = template.gsub(/\{(\w+)\}/) do key = T.must(Regexp.last_match(1)) raise Error, "Missing value for placeholder \"{#{key}}\"." unless vars.key?(key) T.must(vars[key]) end # Auto-append digest for Group/MultiEcosystem name = "#{name}-#{digest}" if digest && strategy != :solo # Collapse empty path segments (e.g. {target_branch} when nil) and strip edge slashes name = name.squeeze("/").gsub(%r{\A/|/\z}, "") raise Error, "Resolved branch name is empty." if name.empty? # Validate git ref after all transformations validate_ref_name(name) name end |
.validate_ref_name(name) ⇒ Object
rubocop:disable Naming/PredicateMethod
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dependabot/pull_request_creator/branch_name_template.rb', line 71 def self.validate_ref_name(name) # rubocop:disable Naming/PredicateMethod illegal = %r{ [\x00-\x1F\x7F\ ~^:?*\[\\] | \.\. | @\{ | // | /\. | \A/ | /\z | \.lock\z | \A- | \.\z | \A@\z }x raise Error, "Resolved branch name \"#{name}\" is not a valid Git ref." if name.match?(illegal) true end |
.validate_template(template, strategy:) ⇒ Object
rubocop:disable Naming/PredicateMethod
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dependabot/pull_request_creator/branch_name_template.rb', line 26 def self.validate_template(template, strategy:) # rubocop:disable Naming/PredicateMethod raise Error, "Template must be a non-empty string." if template.empty? allowed = allowed_placeholders(strategy) = collect_template_errors(template, allowed, strategy) raise Error, "#{.join("\n")}\nAllowed: #{allowed.map { |v| "{#{v}}" }.join(', ')}" if .any? true end |