Skip to content

Commit

Permalink
[Runbooks] Improve Runbook report output by
Browse files Browse the repository at this point in the history
cleaning literal line breaks

fixes: 380512825
Change-Id: I94809e1fff6fce0bc43724db00c41cde9ca085e3
GitOrigin-RevId: 36b358d84afc62faf3eb441ee533bbefc4010a07
  • Loading branch information
Y0Q authored and copybara-github committed Dec 7, 2024
1 parent a4392a9 commit e474a64
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions gcpdiag/runbook/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 [{
Expand All @@ -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):
Expand Down

0 comments on commit e474a64

Please sign in to comment.