Skip to content

Commit 3374bc6

Browse files
authored
code review
clean up and various fixes
1 parent 9350370 commit 3374bc6

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

cpp_linter/rest_api/github_api.py

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
retry="retry-after",
3838
)
3939

40-
QUERY_REVIEW_COMMENTS = """
41-
query($owner: String!, $name: String!, $number: Int!) {
40+
QUERY_REVIEW_COMMENTS = """query($owner: String!, $name: String!, $number: Int!) {
4241
repository(owner: $owner, name: $name) {
4342
pullRequest(number: $number) {
4443
id
@@ -66,38 +65,31 @@
6665
}
6766
}
6867
}
69-
}
70-
"""
68+
}"""
7169

72-
RESOLVE_REVIEW_COMMENT = """
73-
mutation($threadId: ID!) {
70+
RESOLVE_REVIEW_COMMENT = """mutation($threadId: ID!) {
7471
resolveReviewThread(input: {threadId: $threadId, clientMutationId: "github-actions"}) {
7572
thread {
7673
id
7774
}
7875
}
79-
}
80-
"""
76+
}"""
8177

82-
DELETE_REVIEW_COMMENT = """
83-
mutation($id: ID!) {
78+
DELETE_REVIEW_COMMENT = """mutation($id: ID!) {
8479
deletePullRequestReviewComment(input: {id: $id, clientMutationId: "github-actions"}) {
8580
pullRequestReviewComment {
8681
id
8782
}
8883
}
89-
}
90-
"""
84+
}"""
9185

92-
HIDE_REVIEW_COMMENT = """
93-
mutation($subjectId: ID!) {
86+
HIDE_REVIEW_COMMENT = """mutation($subjectId: ID!) {
9487
minimizeComment(input: {classifier:OUTDATED, subjectId: $subjectId, clientMutationId: "github-actions"}) {
9588
minimizedComment {
9689
isMinimized
9790
}
9891
}
99-
}
100-
"""
92+
}"""
10193

10294

10395
class GithubApiClient(RestApiClient):
@@ -731,24 +723,12 @@ def _close_review_comment(
731723
data=json.dumps({"query": mutation, "variables": variables}),
732724
strict=False,
733725
)
734-
if response.status_code != 200:
735-
logger.error(
736-
"Failed to %s review thread comment: %d",
737-
operation,
738-
response.status_code,
739-
)
740-
elif "errors" in response.json():
741-
error_msg = response.json()["errors"][0]["message"]
742-
if "Resource not accessible by integration" in error_msg:
743-
logger.error(
744-
"Changing review thread comments requires `contents: write` permission."
745-
)
746-
else:
747-
logger.error(
748-
"Failed to %s review thread comment: %s", operation, error_msg
749-
)
750-
else:
751-
logger.debug("Review comment thread %sd: %s", operation, thread_id)
726+
logger.debug(
727+
"%s review comment thread %s (thread_id: %s)",
728+
operation.title(),
729+
"failed" if response.status_code != 200 else "succeeded",
730+
thread_id,
731+
)
752732

753733
def _hide_stale_reviews(self, ignored_reviews: List[str]):
754734
"""Hide all review comments that were previously created by cpp-linter
@@ -777,17 +757,8 @@ def _hide_stale_reviews(self, ignored_reviews: List[str]):
777757
data=json.dumps({"query": mutation, "variables": variables}),
778758
strict=False,
779759
)
780-
if response.status_code != 200:
781-
logger.error(
782-
"Failed to hide review comment: %d", response.status_code
783-
)
784-
elif "errors" in response.json():
785-
error_msg = response.json()["errors"][0]["message"]
786-
if "Resource not accessible by integration" in error_msg:
787-
logger.error(
788-
"Hiding review comments requires `contents: write` permission."
789-
)
790-
else:
791-
logger.error("Hiding review comment failed: %s", error_msg)
792-
else:
793-
logger.debug("Review comment minimized: %s", review["node_id"])
760+
logger.debug(
761+
"Minimized review comment: %s (node_id: %s)",
762+
repr(response.status_code != 200).lower(),
763+
review["node_id"],
764+
)

tests/reviews/test_pr_review.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ def test_post_review(
145145

146146
def graphql_callback(request, context):
147147
context.status_code = 200
148-
if request.data.startswith("query"):
148+
query: str = request.json()["query"]
149+
if query.startswith("query"):
149150
# get existing review comments
150151
return (cache_path / "pr_reviews_graphql.json").read_text(
151152
encoding="utf-8"
152153
)
153-
elif "resolveReviewThread" in request.data:
154+
elif "resolveReviewThread" in query:
154155
# resolve review
155156
id_pos = request.data.find('threadId:"') + 10
156157
id_end_pos = request.data.find('"', id_pos + 1)
@@ -167,7 +168,7 @@ def graphql_callback(request, context):
167168
}"""
168169
% id_tag
169170
)
170-
elif "deletePullRequestReviewComment" in request.data:
171+
elif "deletePullRequestReviewComment" in query:
171172
# delete PR or minimizeComment
172173
id_pos = request.data.find('id:"') + 4
173174
id_end_pos = request.data.find('"', id_pos + 1)
@@ -184,7 +185,7 @@ def graphql_callback(request, context):
184185
}"""
185186
% id_tag
186187
)
187-
elif "minimizeComment" in request.data:
188+
elif "minimizeComment" in query:
188189
# minimizeComment
189190
return """{
190191
"data": {
@@ -195,7 +196,13 @@ def graphql_callback(request, context):
195196
}
196197
}
197198
}"""
198-
return "errors"
199+
return json.dumps(
200+
{
201+
"errors": [
202+
{"message": "Failed to parse query when mocking a response"}
203+
]
204+
}
205+
)
199206

200207
mock.post(graphql_url, text=graphql_callback)
201208

0 commit comments

Comments
 (0)