Skip to content

Commit ceee4af

Browse files
committed
Add GitHub PR update delta comments
1 parent 0e307f3 commit ceee4af

7 files changed

Lines changed: 573 additions & 87 deletions

File tree

eden/scm/sapling/ext/github/consts/query.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@
8181
}
8282
"""
8383

84+
GRAPHQL_ADD_COMMENT = """
85+
mutation ($subjectId: ID!, $body: String!) {
86+
addComment(input: {subjectId: $subjectId, body: $body}) {
87+
commentEdge {
88+
node {
89+
id
90+
}
91+
}
92+
}
93+
}
94+
"""
95+
8496
GRAPHQL_CREATE_BRANCH = """
8597
mutation ($repositoryId: ID!, $name: String!, $oid: GitObjectID!) {
8698
createRef(input: {repositoryId: $repositoryId, name: $name, oid: $oid}) {

eden/scm/sapling/ext/github/gh_submit.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,20 @@ async def update_pull_request(
355355
return Ok(result.unwrap()["data"]["updatePullRequest"]["pullRequest"]["id"])
356356

357357

358+
async def add_comment(hostname: str, subject_id: str, body: str) -> Result[str, str]:
359+
"""Adds a comment to an issue or pull request, returning the comment node ID."""
360+
params: Dict[str, _Params] = {
361+
"query": query.GRAPHQL_ADD_COMMENT,
362+
"subjectId": subject_id,
363+
"body": body,
364+
}
365+
result = await gh_cli.make_request(params, hostname=hostname)
366+
if result.is_err():
367+
return Err(result.unwrap_err())
368+
else:
369+
return Ok(result.unwrap()["data"]["addComment"]["commentEdge"]["node"]["id"])
370+
371+
358372
async def create_branch(
359373
*, hostname: str, repo_id: str, branch_name: str, oid: str
360374
) -> Result[str, str]:

eden/scm/sapling/ext/github/mock_utils.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def expect_update_pr_request(
286286
) -> "UpdatePrRequest":
287287
if not stack_pr_ids:
288288
stack_pr_ids = [pr_number]
289-
stack_pr_ids = list(reversed(sorted(stack_pr_ids)))
289+
stack_pr_ids = sorted(stack_pr_ids, reverse=True)
290290

291291
if len(stack_pr_ids) > 1:
292292
pr_list = [
@@ -313,6 +313,19 @@ def expect_update_pr_request(
313313
self._add_request(key, request)
314314
return request
315315

316+
def expect_add_comment_request(
317+
self, pr_id: str, body: str
318+
) -> "AddCommentRequest":
319+
params: ParamsType = {
320+
"query": query.GRAPHQL_ADD_COMMENT,
321+
"subjectId": pr_id,
322+
"body": body,
323+
}
324+
key = create_request_key(params, self.hostname)
325+
request = AddCommentRequest(key, pr_id)
326+
self._add_request(key, request)
327+
return request
328+
316329
def expect_get_username_request(
317330
self,
318331
) -> "GetUsernameRequest":
@@ -545,6 +558,29 @@ def get_response(self) -> Result[JsonDict, str]:
545558
return self._response
546559

547560

561+
class AddCommentRequest(MockRequest):
562+
def __init__(self, key: str, pr_id: str) -> None:
563+
self._key = key
564+
self._response: Optional[Result[JsonDict, str]] = None
565+
566+
self._pr_id = pr_id
567+
568+
def and_respond(self):
569+
data = {
570+
"data": {
571+
"addComment": {
572+
"commentEdge": {"node": {"id": f"comment_{self._pr_id}"}}
573+
}
574+
}
575+
}
576+
self._response = Ok(data)
577+
578+
def get_response(self) -> Result[JsonDict, str]:
579+
if self._response is None:
580+
raise MockResponseNotSet(self._key)
581+
return self._response
582+
583+
548584
class GetUsernameRequest(MockRequest):
549585
def __init__(self, key: str) -> None:
550586
self._key = key

0 commit comments

Comments
 (0)