Skip to content

Commit

Permalink
github: create/preview reactions on pull review request comments
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Mar 2, 2021
1 parent 12e28b4 commit 817608f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Changelog
* Add support for adding tags to existing photos ([bridgy#857](https://github.com/snarfed/bridgy/issues/857)).
* GitHub:
* Handle [HTTP 451 Unavailable for Legal Reasons](https://en.wikipedia.org/wiki/HTTP_451) responses ([eg for DMCA takedowns](https://developer.github.com/changes/2016-03-17-the-451-status-code-is-now-supported/)) gracefully.
* Add create/preview support for reactions on pull review request comments (ie URLs with `#discussion_r...` fragments).
* HTML/microformats2:
* Add `aria-hidden="true"` to empty links ([bridgy#947](https://github.com/snarfed/bridgy/issues/947)).
* Bug fix: escape `&`, `<`, and `>` characters in bare mf2 `content` properties ([aaronpk/XRay#102](https://github.com/aaronpk/XRay/issues/102)).
Expand Down
29 changes: 19 additions & 10 deletions granary/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
REST_API_ISSUE_LABELS = REST_API_BASE + '/repos/%s/%s/issues/%s/labels'
REST_API_COMMENTS = REST_API_BASE + '/repos/%s/%s/issues/%s/comments'
REST_API_REACTIONS = REST_API_BASE + '/repos/%s/%s/issues/%s/reactions'
REST_API_COMMENT = REST_API_BASE + '/repos/%s/%s/issues/comments/%s'
REST_API_COMMENT_REACTIONS = REST_API_BASE + '/repos/%s/%s/issues/comments/%s/reactions'
REST_API_COMMENT = REST_API_BASE + '/repos/%s/%s/%s/comments/%s'
REST_API_COMMENT_REACTIONS = REST_API_BASE + '/repos/%s/%s/%s/comments/%s/reactions'
REST_API_MARKDOWN = REST_API_BASE + '/markdown'
REST_API_NOTIFICATIONS = REST_API_BASE + '/notifications?all=true&participating=true'
GRAPHQL_BASE = 'https://api.github.com/graphql'
Expand Down Expand Up @@ -446,14 +446,16 @@ def get_comment(self, comment_id, **kwargs):
Returns: dict, an ActivityStreams comment object
"""
parts = tuple(comment_id.split(':'))
parts = comment_id.split(':')
if len(parts) != 3:
raise ValueError('GitHub comment ids must be of the form USER:REPO:COMMENT_ID')

if util.is_int(parts[2]): # REST API id
comment = self.rest(REST_API_COMMENT % parts)
id = parts[-1]
if util.is_int(id): # REST API id
parts.insert(2, 'issues')
comment = self.rest(REST_API_COMMENT % tuple(parts))
else: # GraphQL node id
comment = self.graphql(GRAPHQL_COMMENT, {'id': parts[2]})['node']
comment = self.graphql(GRAPHQL_COMMENT, {'id': id})['node']

return self.comment_to_object(comment)

Expand Down Expand Up @@ -561,9 +563,14 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
if len(path) == 4:
number = path[3]

comment_id = re.match(r'^issuecomment-([0-9]+)$', parsed.fragment)
# TODO: support #pullrequestreview-* URLs for top-level PR comments too.
# Haven't yet gotten those to work via either the issues or pulls APIs.
# https://github.com/snarfed/bridgy/issues/955#issuecomment-788478848
comment_id = re.match(r'^(discussion_r|issuecomment-)([0-9]+)$', parsed.fragment)
comment_type = None
if comment_id:
comment_id = comment_id.group(1)
comment_type = 'issues' if comment_id.group(1) == 'issuecomment-' else 'pulls'
comment_id = comment_id.group(2)
elif parsed.fragment:
return source.creation_result(
abort=True,
Expand All @@ -578,7 +585,8 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
is_reaction = orig_content in REACTIONS_GRAPHQL
if preview:
if comment_id:
comment = self.rest(REST_API_COMMENT % (owner, repo, comment_id))
comment = self.rest(REST_API_COMMENT % (owner, repo, comment_type,
comment_id))
target_link = '<a href="%s">a comment on %s/%s#%s, <em>%s</em></a>' % (
base_url, owner, repo, number, util.ellipsize(comment['body']))
else:
Expand All @@ -604,7 +612,8 @@ def _create(self, obj, preview=None, include_link=source.OMIT_LINK,
# https://github.com/snarfed/bridgy/issues/824
if is_reaction:
if comment_id:
api_url = REST_API_COMMENT_REACTIONS % (owner, repo, comment_id)
api_url = REST_API_COMMENT_REACTIONS % (owner, repo, comment_type,
comment_id)
reacted = self.rest(api_url, data={
'content': REACTIONS_REST.get(orig_content),
})
Expand Down
11 changes: 6 additions & 5 deletions granary/tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def test_issue_to_object_empty(self):
self.assert_equals({}, self.gh.issue_to_object({}))

def test_get_comment_rest(self):
self.expect_rest(REST_API_COMMENT % ('foo', 'bar', 123), COMMENT_REST)
self.expect_rest(REST_API_COMMENT % ('foo', 'bar', 'issues', 123), COMMENT_REST)
self.mox.ReplayAll()
self.assert_equals(COMMENT_OBJ, self.gh.get_comment('foo:bar:123'))

Expand Down Expand Up @@ -1066,9 +1066,10 @@ def test_preview_reaction_issue(self):
preview = self.gh.preview_create(REACTION_OBJ_INPUT)
self.assertEqual(u'<span class="verb">react 👍</span> to <a href="https://github.com/foo/bar/pull/123">foo/bar#123, <em>an issue title</em></a>.', preview.description)

def test_create_reaction_comment(self):
def test_create_reaction_issue_comment(self):
self.expect_requests_post(
REST_API_COMMENT_REACTIONS % ('foo', 'bar', 456), headers=EXPECTED_HEADERS,
REST_API_COMMENT_REACTIONS % ('foo', 'bar', 'issues', 456),
headers=EXPECTED_HEADERS,
json={
'content': '+1',
}, response={
Expand All @@ -1085,8 +1086,8 @@ def test_create_reaction_comment(self):
'type': 'react',
}, result.content, result)

def test_preview_reaction_comment(self):
self.expect_rest(REST_API_COMMENT % ('foo', 'bar', 456), COMMENT_REST)
def test_preview_reaction_issue_comment(self):
self.expect_rest(REST_API_COMMENT % ('foo', 'bar', 'issues', 456), COMMENT_REST)
self.mox.ReplayAll()

preview = self.gh.preview_create(COMMENT_REACTION_OBJ_INPUT)
Expand Down

0 comments on commit 817608f

Please sign in to comment.