-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemaphore_parser.rb
137 lines (104 loc) · 4.81 KB
/
semaphore_parser.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'open-uri'
require 'nokogiri'
require_relative 'semaphore_scraper'
class SemaphoreParser
def initialize(auth_token, project_name, branch_name, build_number, folder_name)
scraper = SemaphoreScraper.new(auth_token)
puts "Downloading build information..."
hash_id ||= hash_id(scraper.projects_hash, project_name)
branch_id ||= branch_id(scraper.branches_hash(hash_id), branch_name)
@build_stats = scraper.build_stats(hash_id, branch_id, build_number)
@build_log = scraper.build_log(hash_id, branch_id, build_number)
@totals = { tests: 0, assertions: 0, failures: 0, errors: 0, skips: 0 }
build = "build_#{build_number}"
@stats_filename = "#{folder_name}#{build}_stats.txt"
@combined_output_filename = "#{folder_name}#{build}_thread_output_combined.txt"
@common_lines_filename = "#{folder_name}#{build}_thread_output_common_lines.txt"
@test_numbers_filename = "#{folder_name}#{build}_thread_output_test_numbers.txt"
end
def parse
@combined_output = File.open("#{@combined_output_filename}", "w+")
@stats = File.open("#{@stats_filename}", "w+")
puts "Compiling all output to: #{@combined_output_filename}..."
generate_combined_output_and_stats
@combined_output.close
@stats.close
puts "Outputting the common lines in the compiled output to: #{@common_lines_filename}..."
generate_common_output_lines
puts "Outputting the test numbers for errors and failures: #{@test_numbers_filename}..."
generate_test_numbers
puts "Outputted all statistics to #{@stats_filename}"
end
private
def hash_id(projects_hash, project_name)
projects_hash.detect { |project_hash| project_hash["name"] == project_name }["hash_id"]
end
def branch_id(branches_hash, branch_name)
branches_hash.detect { |branch_hash| branch_hash["name"] == branch_name }["id"]
end
def generate_combined_output_and_stats
@stats.write(build_information)
thread_outputs = sorted_threads.map do |thread|
command = thread["commands"].last
thread_number = command["name"].scan(/\d/).join("")
thread_output = command["output"]
download_node = Nokogiri::HTML(thread_output).css(".text-info").css("a").first
download_link = download_node.attributes["href"].value if download_node.respond_to?(:attributes)
if download_link
thread_output = File.read(open(download_link))
end
write_thread_totals_to_stats(thread_number, thread_output)
["THREAD #{thread_number}:\n", thread_output]
end.flatten.compact
@combined_output.write(thread_outputs.join("\n"))
write_combined_totals_to_stats
end
def sorted_threads
@build_log["threads"].sort_by { |thread| thread["commands"].last["name"].scan(/\d/).join("").to_i }
end
def thread_stats_regex_runs
/\d+ runs, \d+ assertions, \d+ failures, \d+ errors, \d+ skips/
end
def thread_stats_regex_tests
/\d+ tests, \d+ assertions, \d+ failures, \d+ errors, \d+ skips/
end
def thread_stats_regex_omissions
/\d+ tests, \d+ assertions, \d+ failures, \d+ errors, \d+ pendings, \d+ omissions, \d+ notifications/
end
def build_information
["build #{@build_stats["number"]} for #{@build_stats["project_name"]}",
"branch: #{@build_stats["branch_name"]}",
"commit: #{@build_stats["commits"].first["url"]}\n\n"].join("\n")
end
def add_to_totals(stats_line)
line_totals = stats_line.scan(/\d+/)
@totals.each_with_index { |(key, _), i| @totals[key] += line_totals[i].to_i }
end
def write_thread_totals_to_stats(thread_number, thread_output)
stats_line_array = thread_output.scan(thread_stats_regex_tests)
stats_line_array = thread_output.scan(thread_stats_regex_runs) if stats_line_array.empty?
stats_line_array = thread_output.scan(thread_stats_regex_omissions) if stats_line_array.empty?
if stats_line_array.any?
@stats.print("#{thread_number}: ")
@stats.write(stats_line_array.join("\n ") + "\n")
stats_line_array.map { |thread_output_line| add_to_totals(thread_output_line) }
end
end
def write_combined_totals_to_stats
@stats.write("\n")
@totals.each { |key, value| @stats.write("#{value} #{key}\n") }
@stats.write("\nfailures + errors + skips: #{@totals[:failures] + @totals[:errors] + @totals[:skips]}")
end
def generate_common_output_lines
common_lines_command = "cat #{@combined_output_filename} | grep -v \"^\s*$\" | sort | uniq -c | sort -nr > #{@common_lines_filename}"
system(common_lines_command)
end
def generate_test_numbers
common_lines_command = "grep -oG '.*Test#' #{@combined_output_filename} | uniq -c | sort -n > #{@test_numbers_filename}"
system(common_lines_command)
end
end
if __FILE__ == $0
semaphore_parser = SemaphoreParser.new(ARGV[0], ARGV[1], ARGV[2], ARGV[3], ARGV[4] || "")
semaphore_parser.parse
end