-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjenkins_failed_lines.rb
executable file
·196 lines (166 loc) · 4.85 KB
/
jenkins_failed_lines.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env ruby
require 'rubygems'
require 'net/http'
require 'nokogiri'
require 'fileutils'
require 'mechanize'
class JenkinsFailedLines
attr_accessor :scheme_with_host, :job, :build
CACHE_STORE = '/tmp/.JenkinsFailedLines'.freeze
UNAUTHORIZED_STRINGS = [
'Server returned HTTP response code: 401'
].freeze
def initialize(scheme_with_host, job, build)
@scheme_with_host = scheme_with_host
@job = job
@build = build
end
def self.from_url(url)
scheme_with_host = url.split('/').slice(0..2).join('/')
job = url.split('/').slice(4)
build = url.split('/').slice(5).to_i
new(scheme_with_host, job, build).failed_lines
end
def failed_lines
if cucumber?
if child_reports.any?
failed_cucumber_lines_from_child_reports
else
failed_cucumber_lines_from_stack_traces
end
else
failed_rspec_lines
end
end
private
def cucumber?
job.include?('-features')
end
def failed_rspec_lines
check_authorized!
stack_traces.map do |stack_trace|
begin
stack_trace
.text
.split("\n")
.detect { |line| line =~ /spec\.rb:/ && !line.include?('# ./') }
.split(':')
.slice(0..1)
.join(':')
rescue NoMethodError
end
end.compact.uniq
end
def stack_traces
valid_xml_doc.xpath('//errorStackTrace')
end
def failed_cucumber_lines_from_stack_traces
check_authorized!
stack_traces.map do |stack_trace|
begin
stack_trace
.text
.split("\n")
.detect { |line| line =~ /\.feature/ }
.split(':')
.slice(0..1)
.join(':')
rescue NoMethodError
end
end.compact.uniq
end
def failed_cucumber_lines_from_child_reports
check_authorized!
failed_child_report_urls.map do |failed_child_report_url|
content = cache(failed_child_report_url.gsub(/[^0-9A-Za-z]/i, '_')) do
fetch(failed_child_report_url)
end
Nokogiri::HTML(content).xpath('//pre').map(&:text).map do |text|
text.split("\n").detect { |line| line =~ /^feature/ }
end.compact.first.split(':').slice(0..1).join(':')
end.uniq
end
def check_authorized!
return true unless UNAUTHORIZED_STRINGS.detect { |string| xml.include?(string) }
FileUtils.rm_f(cache_path)
fail ArgumentError, 'Unauthorized, you probably did not set JENKINS_LOGIN and JENKINS_TOKEN env variables, removing cache ...'
end
def failed_child_report_urls
failed_child_reports.map do |failed_child_report|
failed_cases = failed_child_report.xpath('result/suite/case').select do |the_case|
the_case.xpath('status').text == 'FAILED'
end
report_url = failed_child_report.xpath('child/url').text
failed_cases.map do |failed_case|
"#{report_url}testReport/(root)/#{URI.escape(failed_case.xpath('className').text)}/#{failed_case.xpath('name').text.gsub(/[^0-9A-Za-z]/i, '_')}/"
end
end.flatten
end
def failed_child_reports
child_reports.select do |report|
report.xpath('result/failCount').text.to_i > 0
end
end
def child_reports
@child_reports ||= valid_xml_doc.xpath('//childReport')
end
def valid_xml_doc
if xml_doc.xpath('//body').text.include?('HTTP ERROR')
fail RuntimeError, xml_doc.xpath('//body').text
end
xml_doc
end
def xml_doc
@xml_doc ||= Nokogiri::XML(xml)
end
def xml
@xml ||= cache do
fetch(request_url)
end
end
def fetch(url)
# TODO: debugging
puts
puts "'#{csrf_crumb}'"
puts "'#{csrf_header_name}'"
puts "'#{csrf_header_value}'"
puts "'#{url}'"
puts
agent = Mechanize.new
agent.add_auth(request_url, JENKINS_LOGIN, JENKINS_TOKEN)
agent.post(request_url, [], {csrf_header_name => csrf_header_value}).body
end
def request_url
%W[#{scheme_with_host} job #{job} #{build} testReport api xml].join('/')
end
def csrf_header_name
csrf_crumb.split(':').first
end
def csrf_header_value
csrf_crumb.split(':').last
end
def csrf_crumb
@csrf_crumb ||= `wget -q --auth-no-challenge --user #{JENKINS_LOGIN} --password #{JENKINS_TOKEN} --output-document - '#{scheme_with_host}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'`
end
def cache(report = nil)
FileUtils.mkdir_p(CACHE_STORE) unless File.directory?(CACHE_STORE)
if File.exists?(cache_path(report))
File.read(cache_path(report))
else
content = Proc.new { yield }.call
File.write(cache_path(report), content)
content
end
end
def cache_path(report = nil)
[CACHE_STORE, cache_file(report)].join('/')
end
def cache_file(report = nil)
[job, build, report].compact.join('-')
end
end
JENKINS_LOGIN = ENV['JENKINS_LOGIN'].freeze
JENKINS_TOKEN = ENV['JENKINS_TOKEN'].freeze
ARGV.each do |url|
puts JenkinsFailedLines.from_url(url)
end