Class: Dependabot::MetadataFinders::Base::ChangelogPruner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependency:, changelog_text:) ⇒ ChangelogPruner

Returns a new instance of ChangelogPruner.



26
27
28
29
# File 'lib/dependabot/metadata_finders/base/changelog_pruner.rb', line 26

def initialize(dependency:, changelog_text:)
  @dependency = dependency
  @changelog_text = changelog_text
end

Instance Attribute Details

#changelog_textObject (readonly)

Returns the value of attribute changelog_text.



17
18
19
# File 'lib/dependabot/metadata_finders/base/changelog_pruner.rb', line 17

def changelog_text
  @changelog_text
end

#dependencyObject (readonly)

Returns the value of attribute dependency.



14
15
16
# File 'lib/dependabot/metadata_finders/base/changelog_pruner.rb', line 14

def dependency
  @dependency
end

Instance Method Details

#includes_new_version?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dependabot/metadata_finders/base/changelog_pruner.rb', line 32

def includes_new_version?
  !new_version_changelog_line.nil?
end

#includes_previous_version?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/dependabot/metadata_finders/base/changelog_pruner.rb', line 37

def includes_previous_version?
  !old_version_changelog_line.nil?
end

#pruned_textObject

rubocop:disable Metrics/PerceivedComplexity



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dependabot/metadata_finders/base/changelog_pruner.rb', line 42

def pruned_text # rubocop:disable Metrics/PerceivedComplexity
  changelog_lines = changelog_text&.split("\n")

  slice_range =
    if old_version_changelog_line && new_version_changelog_line
      if T.must(old_version_changelog_line) < T.must(new_version_changelog_line)
        Range.new(old_version_changelog_line, -1)
      else
        Range.new(new_version_changelog_line,
                  T.must(old_version_changelog_line) - 1)
      end
    elsif old_version_changelog_line
      return if T.must(old_version_changelog_line).zero?

      # Assumes changelog is in descending order
      Range.new(0, T.must(old_version_changelog_line) - 1)
    elsif new_version_changelog_line
      # Assumes changelog is in descending order
      Range.new(new_version_changelog_line, -1)
    else
      return unless changelog_contains_relevant_versions?

      # If the changelog contains any relevant versions, return it in
      # full. We could do better here by fully parsing the changelog
      Range.new(0, -1)
    end

  changelog_lines&.slice(slice_range)&.join("\n")&.rstrip
end