Class: Dependabot::GitMetadataFetcher

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/git_metadata_fetcher.rb

Constant Summary collapse

KNOWN_HOSTS =
/github\.com|bitbucket\.org|gitlab.com/i

Instance Method Summary collapse

Constructor Details

#initialize(url:, credentials:) ⇒ GitMetadataFetcher

Returns a new instance of GitMetadataFetcher.



25
26
27
28
# File 'lib/dependabot/git_metadata_fetcher.rb', line 25

def initialize(url:, credentials:)
  @url = url
  @credentials = credentials
end

Instance Method Details

#head_commit_for_ref(ref) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dependabot/git_metadata_fetcher.rb', line 72

def head_commit_for_ref(ref)
  if ref == "HEAD"
    # Remove the opening clause of the upload pack as this isn't always
    # followed by a line break. When it isn't (e.g., with Bitbucket) it
    # causes problems for our `sha_for_update_pack_line` logic. The format
    # of this opening clause is documented at
    # https://git-scm.com/docs/http-protocol#_smart_server_response
    line = T.must(upload_pack).gsub(/^[0-9a-f]{4}# service=git-upload-pack/, "")
            .lines.find { |l| l.include?(" HEAD") }
    return sha_for_update_pack_line(line) if line
  end

  refs_for_upload_pack
    .find { |r| r.name == ref }
    &.commit_sha
end

#head_commit_for_ref_sha(ref) ⇒ Object



90
91
92
93
94
# File 'lib/dependabot/git_metadata_fetcher.rb', line 90

def head_commit_for_ref_sha(ref)
  refs_for_upload_pack
    .find { |r| r.ref_sha == ref }
    &.commit_sha
end

#ref_namesObject



67
68
69
# File 'lib/dependabot/git_metadata_fetcher.rb', line 67

def ref_names
  refs_for_upload_pack.map(&:name)
end

#refs_for_upload_packObject



62
63
64
# File 'lib/dependabot/git_metadata_fetcher.rb', line 62

def refs_for_upload_pack
  @refs_for_upload_pack ||= T.let(parse_refs_for_upload_pack, T.nilable(T::Array[GitRef]))
end

#tagsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dependabot/git_metadata_fetcher.rb', line 38

def tags
  return [] unless upload_pack

  @tags ||= T.let(
    tags_for_upload_pack.map do |ref|
      GitRef.new(
        name: ref.name,
        tag_sha: ref.ref_sha,
        commit_sha: ref.commit_sha
      )
    end,
    T.nilable(T::Array[GitRef])
  )
end

#tags_for_upload_packObject



54
55
56
57
58
59
# File 'lib/dependabot/git_metadata_fetcher.rb', line 54

def tags_for_upload_pack
  @tags_for_upload_pack ||= T.let(
    refs_for_upload_pack.select { |ref| ref.ref_type == RefType::Tag },
    T.nilable(T::Array[GitRef])
  )
end

#upload_packObject



31
32
33
34
35
# File 'lib/dependabot/git_metadata_fetcher.rb', line 31

def upload_pack
  @upload_pack ||= T.let(fetch_upload_pack_for(url), T.nilable(String))
rescue Octokit::ClientError
  raise Dependabot::GitDependenciesNotReachable, [url]
end