Class: Dependabot::GitCommitChecker

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

VERSION_REGEX =
/
  (?<version>
    (?<=^v)[0-9]+(?:\-[a-z0-9]+)?
    |
    [0-9]+\.[0-9]+(?:\.[a-z0-9\-]+)*
  )$
/ix
VERSION_TAG_MATCH_PATTERN =

String pattern for matching version tags with optional prefixes (e.g., “v1.2.3” matches “1.2.3”)

"(?:[^0-9\\.]|\\A)%s\\z"

Instance Method Summary collapse

Constructor Details

#initialize(dependency:, credentials:, ignored_versions: [], raise_on_ignored: false, consider_version_branches_pinned: false, dependency_source_details: nil) ⇒ GitCommitChecker

Returns a new instance of GitCommitChecker.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dependabot/git_commit_checker.rb', line 44

def initialize(
  dependency:,
  credentials:,
  ignored_versions: [],
  raise_on_ignored: false,
  consider_version_branches_pinned: false,
  dependency_source_details: nil
)
  @dependency = dependency
  @credentials = credentials
  @ignored_versions = ignored_versions
  @raise_on_ignored = raise_on_ignored
  @consider_version_branches_pinned = consider_version_branches_pinned
  @dependency_source_details = dependency_source_details
end

Instance Method Details

#allowed_version_refsObject



195
196
197
# File 'lib/dependabot/git_commit_checker.rb', line 195

def allowed_version_refs
  allowed_versions(local_refs)
end

#allowed_version_tagsObject



190
191
192
# File 'lib/dependabot/git_commit_checker.rb', line 190

def allowed_version_tags
  allowed_versions(local_tags)
end

#branch_or_ref_in_release?(version) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/dependabot/git_commit_checker.rb', line 141

def branch_or_ref_in_release?(version)
  pinned_ref_in_release?(version) || branch_behind_release?(version)
end

#current_versionObject



200
201
202
203
204
# File 'lib/dependabot/git_commit_checker.rb', line 200

def current_version
  return unless dependency.version && version_tag?(T.must(dependency.version))

  version_from_ref(T.must(dependency.version))
end

#dependency_source_detailsObject



251
252
253
# File 'lib/dependabot/git_commit_checker.rb', line 251

def dependency_source_details
  @dependency_source_details || dependency.source_details(allowed_types: ["git"])
end

#filter_lower_versions(tags) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dependabot/git_commit_checker.rb', line 207

def filter_lower_versions(tags)
  return tags unless current_version

  versions = tags.map do |t|
    version_from_tag(t)
  end

  versions.select do |version|
    version > current_version
  end
end

#git_dependency?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/dependabot/git_commit_checker.rb', line 61

def git_dependency?
  return false if dependency_source_details.nil?

  dependency_source_details&.fetch(:type) == "git"
end

#git_repo_reachable?Boolean

Returns:

  • (Boolean)


243
244
245
246
247
248
# File 'lib/dependabot/git_commit_checker.rb', line 243

def git_repo_reachable?
  local_upload_pack
  true
rescue Dependabot::GitDependenciesNotReachable
  false
end

#head_commit_for_current_branchObject



146
147
148
149
150
151
152
153
# File 'lib/dependabot/git_commit_checker.rb', line 146

def head_commit_for_current_branch
  ref = ref_or_branch || "HEAD"

  sha = head_commit_for_local_branch(ref)
  return sha if pinned? || sha

  raise Dependabot::GitDependencyReferenceNotFound, dependency.name
end

#head_commit_for_local_branch(name) ⇒ Object



156
157
158
# File 'lib/dependabot/git_commit_checker.rb', line 156

def head_commit_for_local_branch(name)
  .head_commit_for_ref(name)
end

#head_commit_for_pinned_refObject



105
106
107
# File 'lib/dependabot/git_commit_checker.rb', line 105

def head_commit_for_pinned_ref
  .head_commit_for_ref_sha(T.must(ref))
end

#local_ref_for_latest_version_lower_precisionObject



168
169
170
171
172
# File 'lib/dependabot/git_commit_checker.rb', line 168

def local_ref_for_latest_version_lower_precision
  allowed_refs = local_tag_for_pinned_sha ? allowed_version_tags : allowed_version_refs

  max_local_tag_for_lower_precision(allowed_refs)
end

#local_ref_for_latest_version_matching_existing_precisionObject



161
162
163
164
165
# File 'lib/dependabot/git_commit_checker.rb', line 161

def local_ref_for_latest_version_matching_existing_precision
  allowed_refs = local_tag_for_pinned_sha ? allowed_version_tags : allowed_version_refs

  max_local_tag_for_current_precision(allowed_refs)
end

#local_tag_for_latest_versionObject



175
176
177
# File 'lib/dependabot/git_commit_checker.rb', line 175

def local_tag_for_latest_version
  max_local_tag(allowed_version_tags)
end

#local_tag_for_pinned_shaObject



226
227
228
229
230
231
232
233
# File 'lib/dependabot/git_commit_checker.rb', line 226

def local_tag_for_pinned_sha
  return unless pinned_ref_looks_like_commit_sha?

  @local_tag_for_pinned_sha = T.let(
    most_specific_version_tag_for_sha(ref),
    T.nilable(String)
  )
end

#local_tags_for_allowed_versionsObject



185
186
187
# File 'lib/dependabot/git_commit_checker.rb', line 185

def local_tags_for_allowed_versions
  allowed_version_tags.filter_map { |t| to_local_tag(t) }
end

#local_tags_for_allowed_versions_matching_existing_precisionObject



180
181
182
# File 'lib/dependabot/git_commit_checker.rb', line 180

def local_tags_for_allowed_versions_matching_existing_precision
  select_matching_existing_precision(allowed_version_tags).filter_map { |t| to_local_tag(t) }
end

#max_local_tag(tags) ⇒ Object



274
275
276
277
278
# File 'lib/dependabot/git_commit_checker.rb', line 274

def max_local_tag(tags)
  max_version_tag = tags.max_by { |t| version_from_tag(t) }

  to_local_tag(max_version_tag)
end

#most_specific_tag_equivalent_to_pinned_refObject



220
221
222
223
# File 'lib/dependabot/git_commit_checker.rb', line 220

def most_specific_tag_equivalent_to_pinned_ref
  commit_sha = head_commit_for_local_branch(T.must(ref))
  most_specific_version_tag_for_sha(commit_sha)
end

#most_specific_version_tag_for_sha(commit_sha) ⇒ Object



261
262
263
264
265
266
# File 'lib/dependabot/git_commit_checker.rb', line 261

def most_specific_version_tag_for_sha(commit_sha)
  tags = local_tags_matching_sha(commit_sha)
  return if tags.empty?

  tags[-1]&.name
end

#most_specific_version_tags_for_sha(commit_sha) ⇒ Object



269
270
271
# File 'lib/dependabot/git_commit_checker.rb', line 269

def most_specific_version_tags_for_sha(commit_sha)
  local_tags_matching_sha(commit_sha).map(&:name)
end

#pinned?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dependabot/git_commit_checker.rb', line 69

def pinned?
  raise "Not a git dependency!" unless git_dependency?

  branch = dependency_source_details&.fetch(:branch)

  return false if ref.nil?
  return false if branch == ref
  return true if branch
  return true if dependency.version&.start_with?(T.must(ref))
  return true if ref_matches_tag?

  # Assume we're pinned unless the specified `ref` is actually a branch
  return true unless local_upload_pack&.match?(%r{ refs/heads/#{ref}$})

  # TODO: Research whether considering branches that look like versions pinned makes sense for all ecosystems
  @consider_version_branches_pinned && version_tag?(T.must(ref))
end

#pinned_ref_looks_like_commit_sha?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/dependabot/git_commit_checker.rb', line 96

def pinned_ref_looks_like_commit_sha?
  return false unless ref && ref_looks_like_commit_sha?(T.must(ref))

  return false unless pinned?

  .head_commit_for_ref(T.must(ref)).nil?
end

#pinned_ref_looks_like_version?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/dependabot/git_commit_checker.rb', line 89

def pinned_ref_looks_like_version?
  return false unless pinned?

  version_tag?(T.must(ref))
end

#ref_details(ref) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dependabot/git_commit_checker.rb', line 118

def ref_details(ref)
  T.must(
    T.let(
      GitMetadataFetcher.new(
        url: dependency.source_details&.fetch(:url, nil),
        credentials: credentials
      ).ref_details_for_pinned_ref(ref),
      T.nilable(Excon::Response)
    )
  )
end

#ref_details_for_pinned_refObject



131
132
133
# File 'lib/dependabot/git_commit_checker.rb', line 131

def ref_details_for_pinned_ref
  ref_details(ref_pinned)
end

#ref_looks_like_commit_sha?(ref) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/dependabot/git_commit_checker.rb', line 136

def ref_looks_like_commit_sha?(ref)
  ref.match?(/^[0-9a-f]{6,40}$/)
end

#refs_for_tag_with_detailObject



256
257
258
# File 'lib/dependabot/git_commit_checker.rb', line 256

def refs_for_tag_with_detail
  .refs_for_tag_with_detail
end

#tagsObject



110
111
112
113
114
115
# File 'lib/dependabot/git_commit_checker.rb', line 110

def tags
  GitMetadataFetcher.new(
    url: dependency.source_details&.fetch(:url, nil),
    credentials: credentials
  ).tags
end

#version_for_pinned_shaObject



236
237
238
239
240
# File 'lib/dependabot/git_commit_checker.rb', line 236

def version_for_pinned_sha
  return unless local_tag_for_pinned_sha && version_class.correct?(local_tag_for_pinned_sha)

  version_class.new(local_tag_for_pinned_sha)
end