Skip to content

Commit

Permalink
Add an option to merge multiple comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bwrsandman committed Jan 11, 2024
1 parent a09d6ab commit e78c0b1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
63 changes: 53 additions & 10 deletions post/clang_tidy_review/clang_tidy_review/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import re
import io
import zipfile
import shlex
from github import Github
from github.Requester import Requester
from github.PaginatedList import PaginatedList
Expand Down Expand Up @@ -853,21 +852,65 @@ def save_metadata(pr_number: int) -> None:
json.dump(metadata, metadata_file)


def load_review() -> Optional[PRReview]:
"""Load review output from the standard REVIEW_FILE path.
This file contains
def load_review(review_file: pathlib.Path) -> Optional[PRReview]:
"""Load review output"""

"""

if not pathlib.Path(REVIEW_FILE).exists():
print(f"WARNING: Could not find review file ('{REVIEW_FILE}')", flush=True)
if not review_file.exists():
print(f"WARNING: Could not find review file ('{review_file}')", flush=True)
return None

with open(REVIEW_FILE, "r") as review_file:
payload = json.load(review_file)
with open(review_file, "r") as review_file_handle:
payload = json.load(review_file_handle)
return payload or None


def load_and_merge_reviews(review_files: List[pathlib.Path]) -> Optional[PRReview]:
reviews = []
for file in review_files:
review = load_review(file)
if review is not None:
reviews.append(review)

if not reviews:
return None

result = reviews[0]

class Comment:
def __init__(self, data):
self.data = data
def __hash__(self):
return hash(
(
self.data["body"],
self.data["line"],
self.data["path"],
self.data["side"],
)
)
def __eq__(self, other):
return type(other) is Comment and self.data == other.data

def __lt__(self, other):
if self.data["path"] != other.data["path"]:
return self.data["path"] < other.data["path"]
if self.data["line"] != other.data["line"]:
return self.data["line"] < other.data["line"]
if self.data["side"] != other.data["side"]:
return self.data["side"] < other.data["side"]
if self.data["body"] != other.data["body"]:
return self.data["body"] < other.data["body"]
return hash(self) < hash(other)

comments = set()
for review in reviews:
comments.update(map(Comment, review["comments"]))

result["comments"] = [c.data for c in sorted(comments)]

return result


def get_line_ranges(diff, files):
"""Return the line ranges of added lines in diff, suitable for the
line-filter argument of clang-tidy
Expand Down
14 changes: 12 additions & 2 deletions post/clang_tidy_review/clang_tidy_review/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
# See LICENSE for more information

import argparse
import pathlib
import pprint

from clang_tidy_review import (
PullRequest,
load_review,
load_and_merge_reviews,
post_review,
load_metadata,
strip_enclosing_quotes,
download_artifacts,
post_annotations,
bool_argument,
REVIEW_FILE,
)


Expand Down Expand Up @@ -53,14 +55,22 @@ def main() -> int:
type=bool_argument,
default=False,
)
parser.add_argument(
"reviews",
metavar="REVIEW_FILES",
type=pathlib.Path,
nargs="*",
default=[REVIEW_FILE],
help="Split workflow review results",
)

args = parser.parse_args()

pull_request = PullRequest(args.repo, None, args.token)

# Try to read the review artifacts if they're already present
metadata = load_metadata()
review = load_review()
review = load_and_merge_reviews(args.reviews)

# If not, try to download them automatically
if metadata is None and args.workflow_id is not None:
Expand Down

0 comments on commit e78c0b1

Please sign in to comment.