Skip to content

Commit

Permalink
Merge pull request #5 from ZedThree/max-comments
Browse files Browse the repository at this point in the history
Fix issue with creating a review with too many comments
  • Loading branch information
ZedThree authored Dec 3, 2020
2 parents cdee719 + f39dd85 commit 33e0150
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ you install additional libraries/packages yourself, you'll need to
make sure they are in this directory, otherwise they won't be
accessible from inside this container.

It seems the GitHub API might only accept a limited number of comments
at once, so `clang-tidy-review` will only attempt to post the first
`max_comments` of them (default 25, as this has worked for me).

## Inputs

- `token`: Authentication token
Expand All @@ -68,6 +72,8 @@ accessible from inside this container.
- default: ''
- `apt_packages`: Comma-separated list of apt packages to install
- default: ''
- `max_comments`: Maximum number of comments to post at once
- default: '25'

## Outputs:

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ inputs:
description: 'Comma-separated list of apt packages to install'
required: false
default: ''
max_comments:
description: 'Maximum number of comments to post at once'
required: false
default: '25'
pr:
default: ${{ github.event.pull_request.number }}
repo:
Expand Down
57 changes: 39 additions & 18 deletions review.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,11 @@ def post_lgtm_comment(pull_request):
pull_request.create_issue_comment(BODY)


def post_review(pull_request, review):
"""Post the review on the pull_request, making sure not to spam"""
def cull_comments(pull_request, review, max_comments):
"""Remove comments from review that have already been posted, and keep
only the first max_comments
"""

comments = pull_request.get_review_comments()

Expand All @@ -186,16 +189,15 @@ def post_review(pull_request, review):
)
)

print(f"::set-output name=total_comments::{len(review['comments'])}")

if review["comments"] == []:
print("Everything already posted!")
return

review_string = pprint.pformat(review)
print("\nNew comments to post:\n", review_string, flush=True)
if len(review["comments"]) > max_comments:
review["body"] += (
"\n\nThere were too many comments to post at once. "
f"Showing the first {max_comments} out of {len(review['comments'])}. "
"Check the log or trigger a new build to see more."
)
review["comments"] = review["comments"][:max_comments]

pull_request.create_review(**review)
return review


def main(
Expand All @@ -207,6 +209,7 @@ def main(
token,
include,
exclude,
max_comments,
):

diff = get_pr_diff(repo, pr_number, token)
Expand All @@ -228,6 +231,8 @@ def main(
print("No files to check!")
return

print(f"Checking these files: {files}", flush=True)

clang_tidy_warnings = get_clang_tidy_warnings(
line_ranges, build_dir, clang_tidy_checks, clang_tidy_binary, " ".join(files)
)
Expand All @@ -236,8 +241,7 @@ def main(
lookup = make_file_line_lookup(diff)
review = make_review(clang_tidy_warnings, lookup)

review_string = pprint.pformat(review)
print("Created the following review:\n", review_string, flush=True)
print("Created the following review:\n", pprint.pformat(review), flush=True)

github = Github(token)
repo = github.get_repo(f"{repo}")
Expand All @@ -247,8 +251,17 @@ def main(
post_lgtm_comment(pull_request)
return

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

print(f"::set-output name=total_comments::{len(review['comments'])}")

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

print("Posting the review:\n", pprint.pformat(trimmed_review), flush=True)
pull_request.create_review(**trimmed_review)


if __name__ == "__main__":
Expand Down Expand Up @@ -287,6 +300,12 @@ def main(
type=str,
default="",
)
parser.add_argument(
"--max-comments",
help="Maximum number of comments to post at once",
type=int,
default=25,
)
parser.add_argument("--token", help="github auth token")

args = parser.parse_args()
Expand All @@ -295,11 +314,12 @@ def main(

if args.apt_packages:
# Try to make sure only 'apt install' is run
apt_packages = re.split(BAD_CHARS_APT_PACKAGES_PATTERN, args.apt_packages)[0].split(",")
apt_packages = re.split(BAD_CHARS_APT_PACKAGES_PATTERN, args.apt_packages)[
0
].split(",")
print("Installing additional packages:", apt_packages)
subprocess.run(
["apt", "install", "-y", "--no-install-recommends"]
+ apt_packages
["apt", "install", "-y", "--no-install-recommends"] + apt_packages
)

build_compile_commands = f"{args.build_dir}/compile_commands.json"
Expand Down Expand Up @@ -345,4 +365,5 @@ def main(
token=args.token,
include=args.include.split(","),
exclude=exclude,
max_comments=args.max_comments,
)

0 comments on commit 33e0150

Please sign in to comment.