def test_issue_comment_without_pull_request(responses): responses.add( responses.GET, github_url('/repositories/169101701/issues/19'), json=load_fixture('issue-19.json'), status=200 ) responses.add( responses.GET, github_url('repos/ursa-labs/ursabot/pulls/19'), json={}, status=404 ) responses.add( responses.POST, github_url('/repos/ursa-labs/ursabot/issues/19/comments'), json={} ) def handler(command, **kwargs): pass payload = load_fixture('event-issue-comment-without-pull-request.json') bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) post = responses.calls[2] assert json.loads(post.request.body) == { 'body': "The comment bot only listens to pull request comments!" }
def test_respond_with_usage(responses): responses.add( responses.GET, github_url('/repositories/169101701/issues/26'), json=load_fixture('issue-26.json'), status=200 ) responses.add( responses.GET, github_url('/repos/ursa-labs/ursabot/pulls/26'), json=load_fixture('pull-request-26.json'), status=200 ) responses.add( responses.GET, github_url('/repos/ursa-labs/ursabot/issues/comments/480243811'), json=load_fixture('issue-comment-480243811.json') ) responses.add( responses.POST, github_url('/repos/ursa-labs/ursabot/issues/26/comments'), json={} ) def handler(command, **kwargs): raise CommandError('test-usage') payload = load_fixture('event-issue-comment-with-empty-command.json') bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) post = responses.calls[3] assert json.loads(post.request.body) == {'body': '```\ntest-usage\n```'}
def test_issue_comment_with_commands(load_fixture, responses, command, reaction): responses.add(responses.GET, github_url('/repositories/169101701/issues/26'), json=load_fixture('issue-26.json'), status=200) responses.add(responses.GET, github_url('/repos/ursa-labs/ursabot/pulls/26'), json=load_fixture('pull-request-26.json'), status=200) responses.add( responses.GET, github_url('/repos/ursa-labs/ursabot/issues/comments/480248726'), json=load_fixture('issue-comment-480248726.json')) responses.add( responses.POST, github_url( '/repos/ursa-labs/ursabot/issues/comments/480248726/reactions'), json={}) def handler(command, **kwargs): if command == 'build': return True else: raise ValueError('Only `build` command is supported.') payload = load_fixture('event-issue-comment-build-command.json') payload["comment"]["body"] = command bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) post = responses.calls[3] assert json.loads(post.request.body) == {'content': reaction}
def test_noop_events(fixture_name): payload = load_fixture(fixture_name) handler = Mock() bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) handler.assert_not_called()
def test_issue_comment_with_commands_bot_not_first(load_fixture, responses): # when the @-mention is not first, this is a no-op handler = Mock() payload = load_fixture('event-issue-comment-build-command.json') payload["comment"]["body"] = 'with a comment\n@ursabot build' bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) handler.assert_not_called()
def test_unathorized_user_comment(load_fixture, responses): responses.add(responses.GET, github_url('/repositories/169101701/issues/26'), json=load_fixture('issue-26.json'), status=200) responses.add(responses.GET, github_url('/repos/ursa-labs/ursabot/pulls/26'), json=load_fixture('pull-request-26.json'), status=200) responses.add( responses.GET, github_url('/repos/ursa-labs/ursabot/issues/comments/480243815'), json=load_fixture('pull-request-26.json'), status=200) responses.add(responses.POST, github_url('/repos/ursa-labs/ursabot/issues/26/comments'), json={}) responses.add(responses.POST, github_url('/repos/ursa-labs/ursabot/pulls/26/reactions'), json=()) def handler(command, **kwargs): pass payload = load_fixture('event-issue-comment-by-non-authorized-user.json') payload["comment"]["body"] = '@ursabot crossbow submit -g nightly' bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) print([c.request.body for c in responses.calls]) post = responses.calls[-2] reaction = responses.calls[-1] comment = ("```\nOnly contributors can submit requests to this bot. " "Please ask someone from the community for help with getting " "the first commit in.\n" "The Archery job run can be found at: " "https://github.com/apache/arrow/actions/runs/1463784188\n" "```") assert json.loads(post.request.body) == {"body": f'{comment}'} assert json.loads(reaction.request.body) == {'content': '-1'}
def test_respond_with_usage(load_fixture, responses): responses.add(responses.GET, github_url('/repositories/169101701/issues/26'), json=load_fixture('issue-26.json'), status=200) responses.add(responses.GET, github_url('/repos/ursa-labs/ursabot/pulls/26'), json=load_fixture('pull-request-26.json'), status=200) responses.add( responses.GET, github_url('/repos/ursa-labs/ursabot/issues/comments/480243811'), json=load_fixture('issue-comment-480243811.json')) responses.add(responses.POST, github_url('/repos/ursa-labs/ursabot/issues/26/comments'), json={}) responses.add( responses.POST, github_url( '/repos/ursa-labs/ursabot/issues/comments/479081273/reactions'), json=()) def handler(command, **kwargs): raise CommandError('test-usage') payload = load_fixture('event-issue-comment-with-empty-command.json') bot = CommentBot(name='ursabot', token='', handler=handler) bot.handle('issue_comment', payload) post = responses.calls[3] assert json.loads(post.request.body) == \ {'body': ("```\ntest-usage\n" "The Archery job run can be found at: " "https://github.com/apache/arrow/actions/runs/1463784188\n" "```") }