Class: Dependabot::GithubActions::WorkflowFile::MetadataBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(workflow_file) ⇒ MetadataBuilder

Returns a new instance of MetadataBuilder.



13
14
15
# File 'lib/dependabot/github_actions/workflow_file/metadata_builder.rb', line 13

def initialize(workflow_file)
  @workflow_file = workflow_file
end

Instance Method Details

#build(declaration) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dependabot/github_actions/workflow_file/metadata_builder.rb', line 18

def build(declaration)
   = T.let(
    {
      path: declaration.path,
      value: (declaration.value_node),
      target: (declaration.source_node),
      mapping: (declaration.mapping_node)
    },
    Metadata
  )

  sequence = declaration.steps_sequence
  if sequence
    [:sequence] = (
      sequence,
      declaration.steps_key_node,
      declaration.sequence_item_index
    )
  end

  
end

#collision_declarations_for(declaration) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/dependabot/github_actions/workflow_file/metadata_builder.rb', line 42

def collision_declarations_for(declaration)
  anchor = comment_anchor(declaration)
  return [] unless anchor

  workflow_file.uses_declarations.select do |item|
    item_anchor = comment_anchor(item)
    item_anchor && item_anchor.first == anchor.first
  end
end

#collision_sequences_for(declaration) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dependabot/github_actions/workflow_file/metadata_builder.rb', line 67

def collision_sequences_for(declaration)
  anchor = comment_anchor(declaration)
  return [] unless anchor

  line = anchor.first
  workflow_file.steps_sequences.select do |sequence|
    line.between?(
      workflow_file.node_start_line(sequence),
      workflow_file.node_end_line(sequence)
    )
  end
end

#comment_anchor(declaration) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dependabot/github_actions/workflow_file/metadata_builder.rb', line 81

def comment_anchor(declaration)
  source_node = declaration.source_node
  return unless source_node

  if source_node.is_a?(Psych::Nodes::Scalar) && block_scalar?(source_node)
    return [
      workflow_file.node_start_line(source_node),
      workflow_file.node_start_column(source_node)
    ]
  end

  mapping = declaration.mapping_node
  if mapping && workflow_file.mapping_node_style(mapping) == Psych::Nodes::Mapping::FLOW
    return [workflow_file.node_end_line(mapping), workflow_file.node_end_column(mapping)]
  end

  value_node = declaration.value_node
  return unless value_node

  [workflow_file.node_end_line(value_node), workflow_file.node_end_column(value_node)]
end

#comment_line_collision?(declaration) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dependabot/github_actions/workflow_file/metadata_builder.rb', line 53

def comment_line_collision?(declaration)
  anchor = comment_anchor(declaration)
  return false unless anchor
  return true if collision_declarations_for(declaration).length > 1

  line = anchor.first
  anchor_offset = workflow_file.offset(anchor.first, anchor.last)
  workflow_file.steps_sequences.any? do |sequence|
    sequence_starts_after_anchor?(sequence, line, anchor_offset) ||
      child_starts_after_anchor?(sequence, line, anchor_offset)
  end
end