Class: Dependabot::GitCommitChecker

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of GitCommitChecker.



24
25
26
27
28
29
30
31
32
33
# File 'lib/dependabot/git_commit_checker.rb', line 24

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

Instance Method Details

#allowed_version_tagsObject



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dependabot/git_commit_checker.rb', line 145

def allowed_version_tags
  tags =
    local_tags.
    select { |t| version_tag?(t.name) && matches_existing_prefix?(t.name) }
  filtered = tags.
             reject { |t| tag_included_in_ignore_requirements?(t) }
  if @raise_on_ignored && filter_lower_versions(filtered).empty? && filter_lower_versions(tags).any?
    raise Dependabot::AllVersionsIgnored
  end

  filtered.
    reject { |t| tag_is_prerelease?(t) && !wants_prerelease? }
end

#branch_or_ref_in_release?(version) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/dependabot/git_commit_checker.rb', line 81

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

#current_versionObject



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

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

  version = dependency.version.match(VERSION_REGEX).named_captures.fetch("version")
  version_class.new(version)
end

#filter_lower_versions(tags) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/dependabot/git_commit_checker.rb', line 166

def filter_lower_versions(tags)
  return tags unless current_version

  versions = tags.map do |t|
    version = t.name.match(VERSION_REGEX).named_captures.fetch("version")
    version_class.new(version)
  end

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

#git_dependency?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/dependabot/git_commit_checker.rb', line 35

def git_dependency?
  return false if dependency_source_details.nil?

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

#git_repo_reachable?Boolean

Returns:

  • (Boolean)


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

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

#head_commit_for_current_branchObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dependabot/git_commit_checker.rb', line 85

def head_commit_for_current_branch
  ref = ref_or_branch || "HEAD"

  if pinned?
    return dependency.version ||
           .head_commit_for_ref(ref)
  end

  sha = .head_commit_for_ref(ref)
  return sha if sha

  raise Dependabot::GitDependencyReferenceNotFound, dependency.name
end

#head_commit_for_local_branch(name) ⇒ Object



99
100
101
# File 'lib/dependabot/git_commit_checker.rb', line 99

def head_commit_for_local_branch(name)
  .head_commit_for_ref(name)
end

#local_tag_for_latest_versionObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dependabot/git_commit_checker.rb', line 122

def local_tag_for_latest_version
  tag = max_version_tag(allowed_version_tags)

  return unless tag

  version = tag.name.match(VERSION_REGEX).named_captures.fetch("version")
  {
    tag: tag.name,
    version: version_class.new(version),
    commit_sha: tag.commit_sha,
    tag_sha: tag.tag_sha
  }
end

#local_tag_for_pinned_versionObject



179
180
181
182
183
184
185
186
187
188
# File 'lib/dependabot/git_commit_checker.rb', line 179

def local_tag_for_pinned_version
  return unless pinned?

  ref = dependency_source_details.fetch(:ref)
  tags = local_tags.select { |t| t.commit_sha == ref && version_class.correct?(t.name) }.
         sort_by { |t| version_class.new(t.name) }
  return if tags.empty?

  tags[-1].name
end

#local_tags_for_latest_version_commit_shaObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dependabot/git_commit_checker.rb', line 103

def local_tags_for_latest_version_commit_sha
  tags = allowed_version_tags
  max_tag = max_version_tag(tags)

  return [] unless max_tag

  tags.
    select { |t| t.commit_sha == max_tag.commit_sha }.
    map do |t|
      version = t.name.match(VERSION_REGEX).named_captures.fetch("version")
      {
        tag: t.name,
        version: version_class.new(version),
        commit_sha: t.commit_sha,
        tag_sha: t.tag_sha
      }
    end
end

#max_version_tag(tags) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/dependabot/git_commit_checker.rb', line 136

def max_version_tag(tags)
  tags.
    max_by do |t|
    version = t.name.match(VERSION_REGEX).named_captures.
              fetch("version")
    version_class.new(version)
  end
end

#pinned?Boolean

Returns:

  • (Boolean)


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

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

  ref = dependency_source_details.fetch(:ref)
  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?(ref)

  # If the specified `ref` is actually a tag, we're pinned
  return true if local_upload_pack.match?(%r{ refs/tags/#{ref}$})

  # If the specified `ref` is actually a branch, we're NOT pinned
  return false if local_upload_pack.match?(%r{ refs/heads/#{ref}$})

  # Otherwise, assume we're pinned
  true
end

#pinned_ref_looks_like_commit_sha?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/dependabot/git_commit_checker.rb', line 68

def pinned_ref_looks_like_commit_sha?
  ref = dependency_source_details.fetch(:ref)
  ref_looks_like_commit_sha?(ref)
end

#pinned_ref_looks_like_version?Boolean

Returns:

  • (Boolean)


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

def pinned_ref_looks_like_version?
  return false unless pinned?

  dependency_source_details.fetch(:ref).match?(VERSION_REGEX)
end

#ref_looks_like_commit_sha?(ref) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/dependabot/git_commit_checker.rb', line 73

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

  return false unless pinned?

  .head_commit_for_ref(ref).nil?
end