From 9d34e8d0e9984045e72978fe3bbf93cf8346654a Mon Sep 17 00:00:00 2001 From: Brendan <2bndy5@gmail.com> Date: Fri, 7 Jun 2024 03:02:26 -0700 Subject: [PATCH] allow PR reviews to be passive (#107) ref cpp-linter/cpp-linter-action#243 --- cpp_linter/cli.py | 8 ++++++++ cpp_linter/rest_api/github_api.py | 4 ++++ docs/conf.py | 2 ++ docs/permissions.rst | 4 ++-- tests/reviews/test_pr_review.py | 14 +++++++++++--- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/cpp_linter/cli.py b/cpp_linter/cli.py index 78966e4f..7c812fa7 100644 --- a/cpp_linter/cli.py +++ b/cpp_linter/cli.py @@ -67,6 +67,8 @@ class Args(UserDict): ignore_tidy: str = "" #: See :std:option:`--ignore-format`. ignore_format: str = "" + #: See :std:option:`--passive-reviews`. + passive_reviews: bool = False _parser_args: Dict[Sequence[str], Any] = {} @@ -343,6 +345,12 @@ class Args(UserDict): Defaults to ``%(default)s``.""", ) +_parser_args[("-R", "--passive-reviews")] = dict( + default="false", + type=lambda input: input.lower() == "true", + help="""Set to ``true`` to prevent Pull Request +reviews from requesting or approving changes.""", +) def _parse_jobs(val: str) -> Optional[int]: diff --git a/cpp_linter/rest_api/github_api.py b/cpp_linter/rest_api/github_api.py index 297c7ade..70a438de 100644 --- a/cpp_linter/rest_api/github_api.py +++ b/cpp_linter/rest_api/github_api.py @@ -212,6 +212,7 @@ def post_feedback( tidy_review=args.tidy_review, format_review=args.format_review, no_lgtm=args.no_lgtm, + passive_reviews=args.passive_reviews, ) def make_annotations( @@ -346,6 +347,7 @@ def post_review( tidy_review: bool, format_review: bool, no_lgtm: bool, + passive_reviews: bool, ): url = f"{self.api_url}/repos/{self.repo}/pulls/{self.pull_request}" response = self.api_request(url=url) @@ -393,6 +395,8 @@ def post_review( return body += "\nGreat job! :tada:" event = "APPROVE" + if passive_reviews: + event = "COMMENT" body += USER_OUTREACH payload = { "body": body, diff --git a/docs/conf.py b/docs/conf.py index 9b712cc3..f41f0278 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -226,12 +226,14 @@ def run(self): "1.4.7": ["extra_arg"], "1.8.1": ["jobs"], "1.9.0": ["ignore_tidy", "ignore_format"], + "1.10.0": ["passive_reviews"], } PERMISSIONS = { "thread_comments": ["thread-comments", "contents: write"], "tidy_review": ["pull-request-reviews", "pull-requests: write"], "format_review": ["pull-request-reviews", "pull-requests: write"], + "passive_reviews": ["pull-request-reviews", "pull-requests: write"], "files_changed_only": ["file-changes", "contents: read"], "lines_changed_only": ["file-changes", "contents: read"], } diff --git a/docs/permissions.rst b/docs/permissions.rst index 5e89fd28..2ea2e04d 100644 --- a/docs/permissions.rst +++ b/docs/permissions.rst @@ -90,8 +90,8 @@ The :std:option:`--thread-comments` feature requires the following permissions: Pull Request Reviews ---------------------- -The :std:option:`--tidy-review` and :std:option:`--format-review` features require the following permissions: - +The :std:option:`--tidy-review`, :std:option:`--format-review`, and :std:option:`--passive-reviews` +features require the following permissions: .. code-block:: yaml diff --git a/tests/reviews/test_pr_review.py b/tests/reviews/test_pr_review.py index 5ad8d553..2853b8ff 100644 --- a/tests/reviews/test_pr_review.py +++ b/tests/reviews/test_pr_review.py @@ -29,6 +29,7 @@ summary_only=False, no_lgtm=False, num_workers=None, + is_passive=False, ) @@ -56,6 +57,7 @@ def mk_param_set(**kwargs) -> OrderedDict: tuple(mk_param_set(tidy_review=True, changes=1).values()), tuple(mk_param_set(tidy_review=True, changes=0).values()), tuple(mk_param_set(tidy_review=True, changes=0, summary_only=True).values()), + tuple(mk_param_set(is_passive=True).values()), ], ids=[ "draft", @@ -68,6 +70,7 @@ def mk_param_set(**kwargs) -> OrderedDict: "lines_added", "all_lines", "summary_only", + "passive", ], ) def test_post_review( @@ -83,6 +86,7 @@ def test_post_review( summary_only: bool, no_lgtm: bool, num_workers: int, + is_passive: bool, ): """A mock test of posting PR reviews""" # patch env vars @@ -162,6 +166,7 @@ def test_post_review( args.thread_comments = "false" args.no_lgtm = no_lgtm args.file_annotations = False + args.passive_reviews = is_passive capture_clang_tools_output(files, args=args) if not force_approved: @@ -208,10 +213,13 @@ def test_post_review( assert "clang-format" in json_payload["body"] else: # pragma: no cover raise RuntimeError("review payload is incorrect") - if force_approved: - assert json_payload["event"] == "APPROVE" + if is_passive: + assert json_payload["event"] == "COMMENT" else: - assert json_payload["event"] == "REQUEST_CHANGES" + if force_approved: + assert json_payload["event"] == "APPROVE" + else: + assert json_payload["event"] == "REQUEST_CHANGES" # save the body of the review json for manual inspection assert hasattr(last_request, "text")