Skip to content

Commit

Permalink
allow PR reviews to be passive (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
2bndy5 committed Jun 7, 2024
1 parent 1f67e57 commit 9d34e8d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cpp_linter/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand Down Expand Up @@ -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]:
Expand Down
4 changes: 4 additions & 0 deletions cpp_linter/rest_api/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
Expand Down
4 changes: 2 additions & 2 deletions docs/permissions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions tests/reviews/test_pr_review.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
summary_only=False,
no_lgtm=False,
num_workers=None,
is_passive=False,
)


Expand Down Expand Up @@ -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",
Expand All @@ -68,6 +70,7 @@ def mk_param_set(**kwargs) -> OrderedDict:
"lines_added",
"all_lines",
"summary_only",
"passive",
],
)
def test_post_review(
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 9d34e8d

Please sign in to comment.