-
Notifications
You must be signed in to change notification settings - Fork 0
/
depth_first_search.rb
59 lines (40 loc) · 1.8 KB
/
depth_first_search.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require_relative './visited_state'
# Navigates the dependency hash and returns a string to be printed for the pull request trees
class DepthFirstSearch
STATE_OPEN = "OPEN"
def initialize(nodes, options:)
@nodes = nodes
@visited = VisitedState.new
@options = options
end
# @param level [Integer]
def execute(current_node, level = 0, any_open_node: false)
visited.enter(current_node)
new_any_open_node = any_open_node || nodes[current_node]['state'] == STATE_OPEN
dependent_nodes = nodes[current_node]['dependents']
current_node_name = nodes[current_node]['headRefName'] || current_node
dependents_strings = dependent_nodes.map do |dependent_node|
next if visited.left?(dependent_node)
if visited.in?(dependent_node)
puts "Circular graph between #{current_node} AND #{dependent_node}"
next
end
execute(dependent_node, level + 1, any_open_node: new_any_open_node)
end
dependents_strings = dependents_strings.compact.join("\n")
visited.leave(current_node)
# Doesn't print base branches
return dependents_strings if level.zero?
# Last node and no open PR in the tree
return nil if !new_any_open_node && dependents_strings.empty?
# Will only be printed when has dependents or is a dependent (i.e., has level of a non-base dependent)
return nil if dependent_nodes.empty? && level <= 1
filled_template = options.template.gsub('{{url}}', nodes[current_node]['url']).gsub('{{branch}}', current_node_name)
current_node_string = "#{' ' * [level - 1, 0].max}#{filled_template}"
# If has no dependent, print only itself (and no \n)
return current_node_string if dependents_strings.empty?
"#{current_node_string}\n#{dependents_strings}"
end
private
attr_accessor :nodes, :visited, :options
end