Class: Dependabot::PullRequestCreator::BranchNamer

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/pull_request_creator/branch_namer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependencies:, files:, target_branch:, separator: "/", prefix: "dependabot", max_length: nil) ⇒ BranchNamer

Returns a new instance of BranchNamer.



13
14
15
16
17
18
19
20
21
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 13

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

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



11
12
13
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 11

def dependencies
  @dependencies
end

#filesObject (readonly)

Returns the value of attribute files.



11
12
13
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 11

def files
  @files
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



11
12
13
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 11

def max_length
  @max_length
end

#prefixObject (readonly)

Returns the value of attribute prefix.



11
12
13
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 11

def prefix
  @prefix
end

#separatorObject (readonly)

Returns the value of attribute separator.



11
12
13
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 11

def separator
  @separator
end

#target_branchObject (readonly)

Returns the value of attribute target_branch.



11
12
13
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 11

def target_branch
  @target_branch
end

Instance Method Details

#new_branch_nameObject



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
# File 'lib/dependabot/pull_request_creator/branch_namer.rb', line 23

def new_branch_name
  @name ||=
    begin
      dependency_name_part =
        if dependencies.count > 1 && updating_a_property?
          property_name
        elsif dependencies.count > 1 && updating_a_dependency_set?
          dependency_set.fetch(:group)
        else
          dependencies.
            map(&:name).
            join("-and-").
            tr(":[]", "-").
            tr("@", "")
        end

      "#{dependency_name_part}-#{branch_version_suffix}"
    end

  # Some users need branch names without slashes
  sanitized_name = sanitize_ref(File.join(prefixes, @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