Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add screenshots to github_pr_errors output

Christophe Bliardd1f0827f17e0
diff --git a/script/github_pr_errors b/script/github_pr_errors
index d70131962ba..26591ba1889 100755
--- a/script/github_pr_errors
+++ b/script/github_pr_errors
@@ -15,8 +15,7 @@ require 'yaml'
GITHUB_API_OPENPROJECT_PREFIX = 'https://api.github.com/repos/opf/openproject'.freeze
GITHUB_HTML_OPENPROJECT_PREFIX = 'https://github.com/opf/openproject'.freeze
RAILS_ROOT = Pathname.new(__dir__).dirname
-SPEC_PATTERN = %r{^\S+ (?:rspec (\S+) #.+|An error occurred while loading (\S+)\.\r?)$}
-EXCLUDED_JOB_NAMES = %w[eslint rubocop]
+EXCLUDED_JOB_NAMES = %w[eslint rubocop].freeze
if !ENV['GITHUB_USERNAME']
raise "Missing GITHUB_USERNAME env"
@@ -31,7 +30,7 @@ class Options
run_id: nil
}.freeze
- BANNER = <<~BANNER
+ BANNER = <<~BANNER.freeze
Usage: #{$0} [options]
Fetches rspec failures from last completed GitHub actions on current
@@ -60,6 +59,10 @@ class Options
end
end
+ def respond_to_missing?(method_name, include_private = false)
+ DEFAULTS.key?(name) || super
+ end
+
def parse_options
options = DEFAULTS.dup
opt_parser = OptionParser.new do |parser|
@@ -123,7 +126,8 @@ def error_details(rest_client_exception_with_response)
error = JSON.parse(response.body)
parts = []
- parts << "Failed to perform API request #{response.request.method.upcase} #{response.request.url}: #{rest_client_exception_with_response}"
+ parts << "Failed to perform API request #{response.request.method.upcase} #{response.request.url}: " \
+ "#{rest_client_exception_with_response}"
parts << " #{error['message']}"
parts << " See #{error['documentation_url']}"
parts += rest_client_exception_with_response.backtrace.map { " #{_1}" }
@@ -141,12 +145,6 @@ def path_to_cache_key(path)
.gsub(/\W/, '_') # transform non alphanum chars
end
-def commit_message(workflow_run)
- workflow_run['head_commit']
- .then { |commit| commit["message"] }
- .then { |message| message.split("\n", 2).first }
-end
-
def get_jobs(workflow_run)
workflow_run['jobs_url']
cache_key = [
@@ -179,35 +177,6 @@ def cached(unique_name)
end
end
-def status_icon(job)
- case job['status']
- when "queued", "in_progress"
- "●".yellow
- else
- case job['conclusion']
- when "success"
- "✓".green
- when "failure"
- "✗".red
- else
- "-"
- end
- end
-end
-
-def status_url(job)
- return if job['conclusion'] == "success"
-
- job['html_url'].white.dark
-end
-
-def status_line(job)
- [
- "#{status_icon(job)} #{job['name']}: #{job['conclusion'] || job['status']}",
- status_url(job)
- ].compact.join(" ")
-end
-
def last_with_status(workflow_runs, status)
workflow_runs
.select { |entry| entry['status'] == status }
@@ -236,58 +205,190 @@ def get_workflow_run(run_id)
end
end
-def display_pull_request_info(workflow_run)
- return unless workflow_run['event'] == 'pull_request'
+class Error
+ attr_accessor :location, :page_html, :page_screenshot
+end
+
+class JobErrorsFinder
+ SPEC_PATTERN = %r{^\S+ (?:rspec (\S+) #.+|An error occurred while loading (\S+)\.\r?)$}
+ SCREENSHOT_PATTERN = /\{"message":"Screenshot captured for failed feature test"[^\n]+$/
- pr = workflow_run['pull_requests'].first or
- raise "Pull Request info cannot be found, perhaps it is already merged or closed?"
- pr_number = "##{pr['number']}"
- pr_html_url = "#{GITHUB_HTML_OPENPROJECT_PREFIX}/pull/#{pr['number']}"
- pr_display_title = "#{workflow_run['display_title']} #{pr_number.white.dark} #{pr_html_url.white.dark}"
- warn " Pull Request: #{pr_display_title} "
+ def self.scan_logs(logs)
+ finder = new
+ logs.each do |log|
+ finder.scan_log(log)
+ end
+ finder.errors
+ end
+
+ def scan_log(log)
+ find_errors(log)
+ find_screenshots(log)
+ end
+
+ def errors
+ @errors.values
+ end
+
+ protected
+
+ def initialize
+ @errors = Hash.new { |h, k| h[k] = Error.new }
+ end
+
+ def error(location)
+ @errors[location]
+ end
+
+ def find_errors(log)
+ log.scan(SPEC_PATTERN)
+ .flatten
+ .compact
+ .uniq
+ .sort
+ .each do |location|
+ error(location).location = location
+ end
+ end
+
+ def find_screenshots(log)
+ log.scan(SCREENSHOT_PATTERN)
+ .map { JSON.parse _1 }
+ .each do |screenshot_info|
+ location = screenshot_info["test_location"]
+ error(location).page_html = screenshot_info["html"]
+ error(location).page_screenshot = screenshot_info["image"]
+ end
+ end
end
-def display_workflow_run_info(workflow_run)
- warn " Branch: #{workflow_run['head_branch'].bold}"
- warn " Commit SHA: #{workflow_run['head_sha'].bold}"
- warn " Commit message: #{commit_message(workflow_run).bold}"
- display_pull_request_info(workflow_run)
+class Formatter
+ def initialize(compact: false)
+ @compact = compact
+ end
+
+ def compact?
+ @compact
+ end
+
+ def display_workflow_run_info(workflow_run)
+ warn " Branch: #{workflow_run['head_branch'].bold}"
+ warn " Commit SHA: #{workflow_run['head_sha'].bold}"
+ warn " Commit message: #{commit_message(workflow_run).bold}"
+ display_pull_request_info(workflow_run)
+ end
+
+ def display_workflow_status(workflow_run)
+ warn " #{status_line(workflow_run)}"
+ end
+
+ def display_job_status(job)
+ warn " #{status_line(job)}"
+ end
+
+ def display_errors(errors)
+ if errors.empty?
+ warn "No rspec errors found :-/"
+ elsif compact?
+ puts errors.map { escaped_location(_1) }.join(" ")
+ else
+ errors.each { display_error(_1) }
+ end
+ end
+
+ private
+
+ def display_error(error)
+ puts escaped_location(error)
+ display_error_attribute("html", error.page_html)
+ display_error_attribute("screenshot", error.page_screenshot)
+ end
+
+ def display_error_attribute(name, value)
+ return unless value
+
+ warn [
+ " ",
+ '↳'.blue.bold,
+ " ",
+ name.blue,
+ ": ",
+ value.blue
+ ].join
+ end
+
+ def display_pull_request_info(workflow_run)
+ return unless workflow_run['event'] == 'pull_request'
+
+ pr = workflow_run['pull_requests'].first or
+ raise "Pull Request info cannot be found, perhaps it is already merged or closed?"
+ pr_number = "##{pr['number']}"
+ pr_html_url = "#{GITHUB_HTML_OPENPROJECT_PREFIX}/pull/#{pr['number']}"
+ pr_display_title = "#{workflow_run['display_title']} #{pr_number.white.dark} #{pr_html_url.white.dark}"
+ warn " Pull Request: #{pr_display_title} "
+ end
+
+ def commit_message(workflow_run)
+ workflow_run['head_commit']
+ .then { |commit| commit["message"] }
+ .then { |message| message.split("\n", 2).first }
+ end
+
+ def status_icon(job)
+ case job['status']
+ when "queued", "in_progress"
+ "●".yellow
+ else
+ case job['conclusion']
+ when "success"
+ "✓".green
+ when "failure"
+ "✗".red
+ else
+ "-"
+ end
+ end
+ end
+
+ def status_url(job)
+ return if job['conclusion'] == "success"
+
+ job['html_url'].white.dark
+ end
+
+ def status_line(job)
+ [
+ "#{status_icon(job)} #{job['name']}: #{job['conclusion'] || job['status']}",
+ status_url(job)
+ ].compact.join(" ")
+ end
+
+ def escaped_location(error)
+ "'#{error.location}'"
+ end
end
##########
workflow_run = get_workflow_run(Options.run_id)
-display_workflow_run_info(workflow_run)
+formatter = Formatter.new(compact: Options.compact)
+formatter.display_workflow_run_info(workflow_run)
-errors = []
-is_successful = true
-warn " #{status_line(workflow_run)}"
-get_jobs(workflow_run)
+formatter.display_workflow_status(workflow_run)
+failed_jobs_logs = get_jobs(workflow_run)
.then { |jobs_response| jobs_response['jobs'] }
.sort_by { _1['name'] }
- .each { |job| warn " #{status_line(job)}" }
+ .each { |job| formatter.display_job_status(job) }
.select { _1['conclusion'] == 'failure' }
.reject { EXCLUDED_JOB_NAMES.include?(_1['name']) }
- .each do |job|
- is_successful = false
- get_log(job)
- .scan(SPEC_PATTERN)
- .flatten
- .compact
- .uniq
- .sort
- .each do |match|
- errors << match
- end
- end
+ .map { |job| get_log(job) }
+
+is_successful = failed_jobs_logs.none?
+errors = JobErrorsFinder.scan_logs(failed_jobs_logs)
if is_successful
warn "All jobs successful 🎉"
-elsif errors.empty?
- warn "No rspec errors found :-/"
else
- errors = errors.map { "'#{_1}'" }
- errors = errors.join(" ") if Options.compact
- puts errors
+ formatter.display_errors(errors)
end