-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7452 from TomNaessens/add-sanitization-to-dep-gro…
…up-strat-branch-namer Add sanitization to BranchNamer::DependencyGroupStrategy
- Loading branch information
Showing
4 changed files
with
118 additions
and
52 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
common/lib/dependabot/pull_request_creator/branch_namer/base.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module Dependabot | ||
class PullRequestCreator | ||
class BranchNamer | ||
class Base | ||
attr_reader :dependencies, :files, :target_branch, :separator, :prefix, :max_length | ||
|
||
def initialize(dependencies:, files:, target_branch:, separator: "/", | ||
prefix: "dependabot", max_length: nil) | ||
@dependencies = dependencies | ||
@files = files | ||
@target_branch = target_branch | ||
@separator = separator | ||
@prefix = prefix | ||
@max_length = max_length | ||
end | ||
|
||
private | ||
|
||
def sanitize_branch_name(ref_name) | ||
# General git ref validation | ||
sanitized_name = sanitize_ref(ref_name) | ||
|
||
# Some users need branch names without slashes | ||
sanitized_name = sanitized_name.gsub("/", separator) | ||
|
||
# Shorten the ref in case users refs have length limits | ||
if max_length && (sanitized_name.length > max_length) | ||
sha = Digest::SHA1.hexdigest(sanitized_name)[0, max_length] | ||
sanitized_name[[max_length - sha.size, 0].max..] = sha | ||
end | ||
|
||
sanitized_name | ||
end | ||
|
||
def sanitize_ref(ref) | ||
# This isn't a complete implementation of git's ref validation, but it | ||
# covers most cases that crop up. Its list of allowed characters is a | ||
# bit stricter than git's, but that's for cosmetic reasons. | ||
ref. | ||
# Remove forbidden characters (those not already replaced elsewhere) | ||
gsub(%r{[^A-Za-z0-9/\-_.(){}]}, ""). | ||
# Slashes can't be followed by periods | ||
gsub(%r{/\.}, "/dot-").squeeze(".").squeeze("/"). | ||
# Trailing periods are forbidden | ||
sub(/\.$/, "") | ||
end | ||
end | ||
end | ||
end | ||
end |
28 changes: 14 additions & 14 deletions
28
common/lib/dependabot/pull_request_creator/branch_namer/dependency_group_strategy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters