Class: Dependabot::GithubActions::FileUpdater::WorkflowUpdater::FlowSequenceComments

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/github_actions/file_updater/workflow_updater/flow_sequence_comments.rb

Defined Under Namespace

Classes: ItemComments

Instance Method Summary collapse

Constructor Details

#initialize(workflow_file:, sequence:, commenter:, comment_locator:, item_indent:) ⇒ FlowSequenceComments

Returns a new instance of FlowSequenceComments.



34
35
36
37
38
39
40
41
42
# File 'lib/dependabot/github_actions/file_updater/workflow_updater/flow_sequence_comments.rb', line 34

def initialize(workflow_file:, sequence:, commenter:, comment_locator:, item_indent:)
  @workflow_file = workflow_file
  @sequence = sequence
  @commenter = commenter
  @comment_locator = comment_locator
  @item_indent = item_indent
  @leading_comments = T.let(build_leading_comments, T::Array[String])
  @source_comments = T.let(build_source_comments, T::Hash[Integer, ItemComments])
end

Instance Method Details

#claim_trailing_comment(updates) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dependabot/github_actions/file_updater/workflow_updater/flow_sequence_comments.rb', line 79

def claim_trailing_comment(updates)
  located_comment = comment_locator.for_sequence(sequence)
  return unless located_comment

  update = updates.find do |candidate|
    commenter.recognized_comment?(
      located_comment.comment,
      candidate.old_ref,
      candidate.new_ref
    )
  end
  return unless update
  return unless update.declaration.sequence_item_index

  add_source_comment(T.must(update.declaration.sequence_item_index), located_comment.comment)
  ContentEdit.new(
    start_offset: located_comment.start_offset,
    end_offset: located_comment.end_offset,
    replacement: ""
  )
end

#leading_linesObject



74
75
76
# File 'lib/dependabot/github_actions/file_updater/workflow_updater/flow_sequence_comments.rb', line 74

def leading_lines
  @leading_comments.map { |comment| "#{item_indent}#{comment}" }
end

#render_item(index:, item_count:, source:, updates:) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/dependabot/github_actions/file_updater/workflow_updater/flow_sequence_comments.rb', line 65

def render_item(index:, item_count:, source:, updates:)
  comments = rendered_for(index, updates)
  comma = index == item_count - 1 ? "" : ","
  line = "#{item_indent}#{source}#{comma}#{comments.trailing}"
  standalone = comments.standalone.map { |comment| "#{item_indent}#{comment}" }
  ([line] + standalone).join(workflow_file.newline)
end

#rendered_for(index, updates) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dependabot/github_actions/file_updater/workflow_updater/flow_sequence_comments.rb', line 45

def rendered_for(index, updates)
  source_comments = @source_comments.fetch(index, ItemComments.new(trailing: nil, standalone: []))
  return generated_comments(updates) unless source_comments.any?

  ItemComments.new(
    trailing: process_comment(source_comments.trailing, updates, trailing: true),
    standalone: source_comments.standalone.filter_map do |comment|
      process_comment(comment, updates, trailing: false)
    end
  )
end