Module: Dependabot::GithubActions::ContainingBranchFinder

Extended by:
T::Sig
Defined in:
lib/dependabot/github_actions/containing_branch_finder.rb

Constant Summary collapse

COMMIT_NOT_FOUND_REGEX =

A missing commit has no containing branch, so callers can handle it like an empty branch lookup without masking unrelated subprocess failures.

T.let(
  /no such commit|malformed object name|bad object|not a valid object name/i,
  Regexp
)

Class Method Summary collapse

Class Method Details

.find(sha) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dependabot/github_actions/containing_branch_finder.rb', line 21

def self.find(sha)
  branches_including_ref = SharedHelpers.run_shell_command(
    "git branch --remotes --contains #{sha}",
    fingerprint: "git branch --remotes --contains <sha>"
  ).split("\n").map { |branch| branch.strip.gsub("origin/", "") }
  return if branches_including_ref.empty?

  current_branch = branches_including_ref.find { |branch| branch.start_with?("HEAD -> ") }

  if current_branch
    current_branch.delete_prefix("HEAD -> ")
  elsif branches_including_ref.size > 1
    raise "Multiple ambiguous branches (#{branches_including_ref.join(', ')}) include #{sha}!"
  else
    branches_including_ref.first
  end
rescue SharedHelpers::HelperSubprocessFailed => e
  raise unless e.message.match?(COMMIT_NOT_FOUND_REGEX)

  nil
end