Skip to content

Commit

Permalink
Properly deal with markdown in clang-tidy comments (#12)
Browse files Browse the repository at this point in the history
* Properly escape markdown characters in clang-tidy comments

* Remove unused variable

* Remove unused import

* Reorder imports according to PEP8

* Decorate quoted symbols as code
  • Loading branch information
oleg-derevenetz authored Aug 17, 2021
1 parent 5aec7f4 commit 9748376
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions run_action.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9748376

Please sign in to comment.