Skip to content

Commit ee9e166

Browse files
committed
Add links to check documentation in review
1 parent 9008958 commit ee9e166

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

Diff for: post/clang_tidy_review/clang_tidy_review/__init__.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,28 @@ def set_output(key: str, val: str) -> bool:
10851085
return True
10861086

10871087

1088+
def decorate_comment_body(comment: str) -> str:
1089+
"""
1090+
Split on first dash into two groups of string in [] at end of line
1091+
exception: if the first group starts with 'clang' such as 'clang-diagnostic-error'
1092+
exception to the exception: if the string starts with 'clang-analyzer', in which case, make it the first group
1093+
"""
1094+
version = "extra"
1095+
regex = r"(\[((?:clang-analyzer)|(?:(?!clang)[\w]+))-([\.\w-]+)\]$)"
1096+
subst = "[\\g<1>(https://clang.llvm.org/extra/clang-tidy/checks/\\g<2>/\\g<3>.html)]"
1097+
return re.sub(regex, subst, comment)
1098+
1099+
1100+
def decorate_comment(comment: PRReviewComment) -> PRReviewComment:
1101+
comment.body = decorate_comment_body(comment.body)
1102+
return comment
1103+
1104+
1105+
def decorate_comments(review: PRReview) -> PRReview:
1106+
review.comments = list(map(decorate_comment, review.comments))
1107+
return review
1108+
1109+
10881110
def post_review(
10891111
pull_request: PullRequest,
10901112
review: Optional[PRReview],
@@ -1104,21 +1126,23 @@ def post_review(
11041126

11051127
total_comments = len(review["comments"])
11061128

1107-
set_output("total_comments", total_comments)
1129+
set_output("total_comments", str(total_comments))
11081130

11091131
print("Removing already posted or extra comments", flush=True)
11101132
trimmed_review = cull_comments(pull_request, review, max_comments)
11111133

1112-
if trimmed_review["comments"] == []:
1134+
if not trimmed_review["comments"]:
11131135
print("Everything already posted!")
11141136
return total_comments
11151137

11161138
if dry_run:
11171139
pprint.pprint(review, width=130)
11181140
return total_comments
11191141

1120-
print("Posting the review:\n", pprint.pformat(trimmed_review), flush=True)
1121-
pull_request.post_review(trimmed_review)
1142+
decorated_review = decorate_comments(trimmed_review)
1143+
1144+
print("Posting the review:\n", pprint.pformat(decorated_review), flush=True)
1145+
pull_request.post_review(decorated_review)
11221146

11231147
return total_comments
11241148

Diff for: tests/test_review.py

+32
Original file line numberDiff line numberDiff line change
@@ -427,3 +427,35 @@ def test_config_file(monkeypatch, tmp_path):
427427
"not-clang-tidy", clang_tidy_checks="readability", config_file=""
428428
)
429429
assert flag == "--checks=readability"
430+
431+
432+
def test_decorate_comment_body():
433+
# No link to generic error so the message shouldn't be changed
434+
error_message = (
435+
"warning: no member named 'ranges' in namespace 'std' [clang-diagnostic-error]"
436+
)
437+
assert ctr.decorate_comment_body(error_message) == error_message
438+
439+
todo_message = "warning: missing username/bug in TODO [google-readability-todo]"
440+
todo_message_decorated = "warning: missing username/bug in TODO [[google-readability-todo](https://clang.llvm.org/extra/clang-tidy/checks/google/readability-todo.html)]"
441+
assert ctr.decorate_comment_body(todo_message) == todo_message_decorated
442+
443+
naming_message = "warning: invalid case style for constexpr variable 'foo' [readability-identifier-naming]"
444+
naming_message_decorated = "warning: invalid case style for constexpr variable 'foo' [[readability-identifier-naming](https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html)]"
445+
assert ctr.decorate_comment_body(naming_message) == naming_message_decorated
446+
447+
clang_analyzer_message = "warning: Array access (from variable 'foo') results in a null pointer dereference [clang-analyzer-core.NullDereference]"
448+
clang_analyzer_message_decorated = "warning: Array access (from variable 'foo') results in a null pointer dereference [[clang-analyzer-core.NullDereference](https://clang.llvm.org/extra/clang-tidy/checks/clang-analyzer/core.NullDereference.html)]"
449+
assert ctr.decorate_comment_body(clang_analyzer_message) == clang_analyzer_message_decorated
450+
451+
# Not sure it's necessary to link to prior version documentation especially since we have to map versions such as
452+
# "17" to "17.0.1" and "18" to "18.1.0" because no other urls exist
453+
# version_message_pre_15_version = "14.0.0"
454+
# version_message_pre_15 = "warning: missing username/bug in TODO [google-readability-todo]"
455+
# version_message_pre_15_decorated = "warning: missing username/bug in TODO [[google-readability-todo](https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/google-readability-todo.html)]"
456+
# assert ctr.decorate_comment_body(version_message_pre_15, version_message_pre_15_version) == version_message_pre_15_decorated
457+
#
458+
# version_message_1500_version = "15.0.0"
459+
# version_message_1500 = "warning: missing username/bug in TODO [google-readability-todo]"
460+
# version_message_1500_decorated = "warning: missing username/bug in TODO [[google-readability-todo](https://releases.llvm.org/15.0.0/tools/clang/tools/extra/docs/clang-tidy/checks/google/readability-todo.html)]"
461+
# assert ctr.decorate_comment_body(version_message_1500, version_message_1500_version) == version_message_1500_decorated

0 commit comments

Comments
 (0)