Skip to content

Commit

Permalink
Add links to check documentation in review
Browse files Browse the repository at this point in the history
  • Loading branch information
bwrsandman committed May 11, 2024
1 parent 9008958 commit ac933fc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
34 changes: 30 additions & 4 deletions post/clang_tidy_review/clang_tidy_review/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,30 @@ def set_output(key: str, val: str) -> bool:
return True


def decorate_comment_body(comment: str) -> str:
"""
Split on first dash into two groups of string in [] at end of line
exception: if the first group starts with 'clang' such as 'clang-diagnostic-error'
exception to the exception: if the string starts with 'clang-analyzer', in which case, make it the first group
"""
version = "extra"
regex = r"(\[((?:clang-analyzer)|(?:(?!clang)[\w]+))-([\.\w-]+)\]$)"
subst = (
"[\\g<1>(https://clang.llvm.org/extra/clang-tidy/checks/\\g<2>/\\g<3>.html)]"
)
return re.sub(regex, subst, comment)


def decorate_comment(comment: PRReviewComment) -> PRReviewComment:
comment.body = decorate_comment_body(comment.body)
return comment


def decorate_comments(review: PRReview) -> PRReview:
review.comments = list(map(decorate_comment, review.comments))
return review


def post_review(
pull_request: PullRequest,
review: Optional[PRReview],
Expand All @@ -1104,21 +1128,23 @@ def post_review(

total_comments = len(review["comments"])

set_output("total_comments", total_comments)
set_output("total_comments", str(total_comments))

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

if trimmed_review["comments"] == []:
if not trimmed_review["comments"]:
print("Everything already posted!")
return total_comments

if dry_run:
pprint.pprint(review, width=130)
return total_comments

print("Posting the review:\n", pprint.pformat(trimmed_review), flush=True)
pull_request.post_review(trimmed_review)
decorated_review = decorate_comments(trimmed_review)

print("Posting the review:\n", pprint.pformat(decorated_review), flush=True)
pull_request.post_review(decorated_review)

return total_comments

Expand Down
35 changes: 35 additions & 0 deletions tests/test_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,38 @@ def test_config_file(monkeypatch, tmp_path):
"not-clang-tidy", clang_tidy_checks="readability", config_file=""
)
assert flag == "--checks=readability"


def test_decorate_comment_body():
# No link to generic error so the message shouldn't be changed
error_message = (
"warning: no member named 'ranges' in namespace 'std' [clang-diagnostic-error]"
)
assert ctr.decorate_comment_body(error_message) == error_message

todo_message = "warning: missing username/bug in TODO [google-readability-todo]"
todo_message_decorated = "warning: missing username/bug in TODO [[google-readability-todo](https://clang.llvm.org/extra/clang-tidy/checks/google/readability-todo.html)]"
assert ctr.decorate_comment_body(todo_message) == todo_message_decorated

naming_message = "warning: invalid case style for constexpr variable 'foo' [readability-identifier-naming]"
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)]"
assert ctr.decorate_comment_body(naming_message) == naming_message_decorated

clang_analyzer_message = "warning: Array access (from variable 'foo') results in a null pointer dereference [clang-analyzer-core.NullDereference]"
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)]"
assert (
ctr.decorate_comment_body(clang_analyzer_message)
== clang_analyzer_message_decorated
)

# Not sure it's necessary to link to prior version documentation especially since we have to map versions such as
# "17" to "17.0.1" and "18" to "18.1.0" because no other urls exist
# version_message_pre_15_version = "14.0.0"
# version_message_pre_15 = "warning: missing username/bug in TODO [google-readability-todo]"
# 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)]"
# assert ctr.decorate_comment_body(version_message_pre_15, version_message_pre_15_version) == version_message_pre_15_decorated
#
# version_message_1500_version = "15.0.0"
# version_message_1500 = "warning: missing username/bug in TODO [google-readability-todo]"
# 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)]"
# assert ctr.decorate_comment_body(version_message_1500, version_message_1500_version) == version_message_1500_decorated

0 comments on commit ac933fc

Please sign in to comment.