-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
executable file
·48 lines (36 loc) · 1.49 KB
/
main.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
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'optparse'
end
require 'json'
require_relative './parser'
require_relative './depth_first_search'
options = Parser.parse(ARGV)
pr_limits = 40_000
pr_attributes = 'baseRefName,headRefName,url,state'
debug_string = "gh pr list --state open -L #{pr_limits} --json #{pr_attributes}"
puts debug_string if options.debug
open_prs = JSON.parse(`#{debug_string}`)
merged_prs = if options.open_only
[]
else
exclude_base = options.base_branches.map { |base_branch| "-base:#{base_branch}" }.join(' ')
debug_string = "gh pr list --state merged -L #{pr_limits} --json #{pr_attributes} --search \"#{exclude_base}\""
puts debug_string if options.debug
JSON.parse(`#{debug_string}`)
end
json = open_prs + merged_prs
initial_object = Hash.new { |hash, key| hash[key] = { 'dependents' => [] } }
dependency_hash = json.each_with_object(initial_object) do |pull_request_data, object|
head_branch = pull_request_data['headRefName']
base_branch = pull_request_data['baseRefName']
object[head_branch].merge!(**pull_request_data)
object[base_branch]['dependents'].push(head_branch)
object
end
output = options.base_branches.map do |base_branch|
"<!-- PULL REQUEST TRAIN FROM THE BASE BRANCH #{base_branch} -->\n#{DepthFirstSearch.new(dependency_hash, options: options).execute(base_branch)}"
end.join("\n\n")
puts output