From 9748376e589e28d383a9c52acbb35def24280fed Mon Sep 17 00:00:00 2001 From: Oleg Derevenetz Date: Tue, 17 Aug 2021 22:01:51 +0300 Subject: [PATCH] Properly deal with markdown in clang-tidy comments (#12) * Properly escape markdown characters in clang-tidy comments * Remove unused variable * Remove unused import * Reorder imports according to PEP8 * Decorate quoted symbols as code --- run_action.py | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/run_action.py b/run_action.py index 116f7ec..6578ce8 100755 --- a/run_action.py +++ b/run_action.py @@ -1,16 +1,16 @@ #!/usr/bin/env python3 -import sys -import os -import json -import requests import argparse -import yaml -import re import itertools +import json +import os import posixpath +import re +import sys import time -from string import Template + +import requests +import yaml def chunks(lst, n): @@ -20,6 +20,37 @@ def chunks(lst, n): yield lst[i : i + n] +def markdown(s): + md_chars = "\\`*_{}[]<>()#+-.!|" + + + def escape_chars(s): + for ch in md_chars: + s = s.replace(ch, "\\" + ch) + + return s + + + def unescape_chars(s): + for ch in md_chars: + s = s.replace("\\" + ch, ch) + + return s + + + # Escape markdown characters + s = escape_chars(s) + # Decorate quoted symbols as code + s = re.sub( + "'([^']*)'", + lambda match: + "`` " + unescape_chars(match.group(1)) + " ``", + s + ) + + return s + + def main(): parser = argparse.ArgumentParser( description="Pull request comments from clang-tidy reports action runner" @@ -142,7 +173,6 @@ def main(): ] repository_root = args.repository_root + "/" - clang_tidy_fixes_for_available_files = list() # Normalize paths for diagnostic in clang_tidy_fixes["Diagnostics"]: # diagnostic = d["DiagnosticMessage"] if "DiagnosticMessage" in d.keys() else d @@ -228,9 +258,9 @@ def main(): if line_number in changed_lines: review_comment_body = ( ":warning: **" - + diagnostic["DiagnosticName"] + + markdown(diagnostic["DiagnosticName"]) + "** :warning:\n" - + diagnostic["DiagnosticMessage"]["Message"] + + markdown(diagnostic["DiagnosticMessage"]["Message"]) + suggestions ) review_comments.append(