From e474a648bb1e65f2908c50d7a358c22265a01fb1 Mon Sep 17 00:00:00 2001 From: Joel Jacob Date: Fri, 6 Dec 2024 13:02:50 -0500 Subject: [PATCH] [Runbooks] Improve Runbook report output by cleaning literal line breaks fixes: 380512825 Change-Id: I94809e1fff6fce0bc43724db00c41cde9ca085e3 GitOrigin-RevId: 36b358d84afc62faf3eb441ee533bbefc4010a07 --- gcpdiag/runbook/report.py | 49 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/gcpdiag/runbook/report.py b/gcpdiag/runbook/report.py index 27e6b4e5c..38aad3e43 100644 --- a/gcpdiag/runbook/report.py +++ b/gcpdiag/runbook/report.py @@ -173,7 +173,17 @@ def add_step_eval(self, run_id, execution_id, evaluation: ResourceEvaluation): self.reports[run_id].results[execution_id].results.append(evaluation) def serialize_report(self, report: Report): - line_breaks = '\t|\n|\r|\r\n|\x0b|\x0c' + + def remove_line_breaks(text): + if text is None: + return None + # First, replace escaped line breaks like \n, \r, etc. + text = re.sub(r'\\n|\\r|\\t|\\r\\n|\\x0b|\\x0c', ' ', text) + # Then, replace any remaining literal newline characters + text = re.sub(r'\n|\r', ' ', text) + # Remove extra spaces at start / end of string + text = text.strip() + return text def resource_evaluation(eval_list: List[ResourceEvaluation]): return [{ @@ -182,37 +192,26 @@ def resource_evaluation(eval_list: List[ResourceEvaluation]): 'status': r.status, 'reason': - re.sub(line_breaks, '', str(r.reason)), + remove_line_breaks(str(r.reason)) if r.reason else '-', 'remediation': - re.sub(line_breaks, '', r.remediation) if r.remediation else '-', + remove_line_breaks(r.remediation) if r.remediation else '-', 'remediation_skipped': False if config.get('auto') else r.remediation_skipped } for r in eval_list] def result_to_dict(entry: StepResult): return { - 'execution_id': - entry.step.execution_id, - 'totals_by_status': - entry.totals_by_status, - 'description': - re.sub(line_breaks, '', entry.step.__doc__), - 'execution_message': - re.sub(line_breaks, '', entry.step.execution_message), - 'overall_status': - entry.overall_status, - 'start_time': - entry.start_time, - 'end_time': - entry.end_time, - 'metadata': - entry.metadata, - 'info': - entry.info, - 'execution_error': - entry.step_error, - 'resource_evaluation': - resource_evaluation(entry.results) + 'execution_id': entry.step.execution_id, + 'totals_by_status': entry.totals_by_status, + 'description': remove_line_breaks(entry.step.__doc__), + 'execution_message': remove_line_breaks(entry.step.execution_message), + 'overall_status': entry.overall_status, + 'start_time': entry.start_time, + 'end_time': entry.end_time, + 'metadata': entry.metadata, + 'info': entry.info, + 'execution_error': entry.step_error, + 'resource_evaluation': resource_evaluation(entry.results) } def parse_report_data(data):