Fix rubocop warning

And use Data class to make Report a value object.
This commit is contained in:
Christophe Bliard
2025-04-10 13:49:28 +02:00
parent b751170f61
commit 1eeebb72bb
+87 -46
View File
@@ -272,14 +272,56 @@ def get_workflow_run(run_id)
end
end
class Report
attr_accessor :errors,
:failures_explanation,
:head_branch,
:head_sha,
:commit_message,
:run_started_at,
:merge_branch_sha
class WorkflowRunDecorator
attr_reader :workflow_run
def initialize(workflow_run)
@workflow_run = workflow_run
end
def head_branch = workflow_run["head_branch"]
def head_sha = workflow_run["head_sha"]
def run_started_at = Time.parse(workflow_run["run_started_at"]).utc
def commit_message
workflow_run["head_commit"]
.then { |commit| commit["message"] }
.then { |message| message.split("\n", 2).first }
end
end
Report = Data.define(
:errors,
:failures_explanation,
:head_branch,
:head_sha,
:commit_message,
:run_started_at,
:run_status,
:failed_job_logs,
:merge_branch_sha
) do
def initialize(errors: [],
failures_explanation: nil,
head_branch: nil,
head_sha: nil,
commit_message: nil,
run_started_at: nil,
run_status: nil,
failed_job_logs: nil,
merge_branch_sha: nil)
super
end
def with_workflow_run_info(workflow_run)
workflow_run = WorkflowRunDecorator.new(workflow_run)
with(
head_branch: workflow_run.head_branch,
head_sha: workflow_run.head_sha,
commit_message: workflow_run.commit_message,
run_started_at: workflow_run.run_started_at
)
end
end
class Error
@@ -322,10 +364,11 @@ class JobErrorsFinder
logs.each do |log|
finder.scan_log(log)
end
report.errors = finder.errors
report.failures_explanation = finder.failures_explanation
report.merge_branch_sha = finder.merge_branch_sha
report
report.with(
errors: finder.errors,
failures_explanation: finder.failures_explanation,
merge_branch_sha: finder.merge_branch_sha
)
end
def scan_log(log)
@@ -454,16 +497,11 @@ class Formatter
@compact
end
def display_workflow_run_info(report, workflow_run)
report.head_branch = workflow_run["head_branch"]
report.head_sha = workflow_run["head_sha"]
report.commit_message = commit_message(workflow_run)
report.run_started_at = Time.parse(workflow_run["run_started_at"]).utc
def display_workflow_run_info(report)
warn " Branch: #{report.head_branch.bold}"
warn " Commit SHA: #{report.head_sha.bold}"
warn " Commit message: #{report.commit_message.bold}"
warn " Last attempted run started at: #{report.run_started_at.localtime.to_s.bold}"
display_pull_request_info(workflow_run)
end
def display_workflow_status(workflow_run)
@@ -475,7 +513,7 @@ class Formatter
warn " #{status_line(job)}"
end
def display_report(report)
def display_errors_from_report(report)
display_failures_explanation(report.failures_explanation)
display_errors(report.errors)
display_checkout_test_commit_information(report) if Options.display_rerun_info
@@ -499,6 +537,19 @@ class Formatter
end
end
def display_pull_request_info(workflow_run)
return unless workflow_run["event"] == "pull_request"
if pr = workflow_run["pull_requests"].first
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} "
else
warn " Pull Request: not found; perhaps it is already merged or closed?"
end
end
private
def display_errors_compact(errors)
@@ -599,25 +650,6 @@ class Formatter
warn ""
end
def display_pull_request_info(workflow_run)
return unless workflow_run["event"] == "pull_request"
if pr = workflow_run["pull_requests"].first
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} "
else
warn " Pull Request: not found; perhaps it is already merged or closed?"
end
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"
@@ -680,13 +712,18 @@ end
def get_failed_jobs_logs_from_github(report, formatter)
workflow_run = get_workflow_run(Options.run_id)
report = report.with_workflow_run_info(workflow_run)
formatter.display_workflow_run_info(report, workflow_run)
formatter.display_workflow_run_info(report)
formatter.display_pull_request_info(workflow_run)
job_logs = get_relevant_jobs(Options.job_id, workflow_run, formatter)
failed_job_logs = get_relevant_jobs(Options.job_id, workflow_run, formatter)
.map { |job| get_log(job) }
[job_logs, job_logs.any? ? "error" : workflow_run["status"]]
report.with(
failed_job_logs:,
run_status: failed_job_logs.any? ? "error" : workflow_run["status"]
)
end
def get_failed_jobs_logs_from_args
@@ -695,7 +732,11 @@ end
def get_failed_jobs_logs(report, formatter)
if Options.failed_jobs_logs.any?
[get_failed_jobs_logs_from_args, failed_jobs_logs.none? ? "completed" : "error"]
failed_job_logs = get_failed_jobs_logs_from_args
report.with(
failed_job_logs:,
run_status: failed_job_logs.none? ? "completed" : "error"
)
else
get_failed_jobs_logs_from_github(report, formatter)
end
@@ -707,15 +748,15 @@ end
report = Report.new
formatter = Formatter.new(compact: Options.compact)
failed_jobs_logs, status = get_failed_jobs_logs(report, formatter)
report = get_failed_jobs_logs(report, formatter)
JobErrorsFinder.scan_logs(report, failed_jobs_logs)
report = JobErrorsFinder.scan_logs(report, report.failed_job_logs)
case status
case report.run_status
when "completed"
warn "All jobs successful 🎉"
when "in_progress"
# NOOP
when "error"
formatter.display_report(report)
formatter.display_errors_from_report(report)
end