예제 #1
0
class TestPullRequestHandler:
    def setup_class(self):
        self.pr = PullRequestHandler('fakerepo/doesnotexist', 1234)

    def test_urls(self):
        assert self.pr._url_pull_request == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234'
        assert self.pr._url_review_comment == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234/reviews'
        assert self.pr._url_commits == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234/commits'
        assert self.pr._url_files == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234/files'

    def test_has_modified(self):
        mock = MagicMock(return_value=[{
            "sha":
            "bbcd538c8e72b8c175046e27cc8f907076331401",
            "filename":
            "file1.txt",
            "status":
            "added",
            "additions":
            103,
            "deletions":
            21,
            "changes":
            124,
            "blob_url":
            "https://github.com/blah/blah/blob/hash/file1.txt",
            "raw_url":
            "https://github.com/blaht/blah/raw/hash/file1.txt",
            "contents_url":
            "https://api.github.com/repos/blah/blah/contents/file1.txt?ref=hash",
            "patch":
            "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
        }])
        with patch('baldrick.github.github_api.paged_github_json_request',
                   mock):  # noqa
            assert self.pr.has_modified(['file1.txt'])
            assert self.pr.has_modified(['file1.txt', 'notthis.txt'])
            assert not self.pr.has_modified(['notthis.txt'])
예제 #2
0
class TestPullRequestHandler:
    def setup_class(self):
        self.pr = PullRequestHandler('fakerepo/doesnotexist', 1234)

    def test_urls(self):
        assert self.pr._url_pull_request == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234'
        assert self.pr._url_review_comment == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234/reviews'
        assert self.pr._url_commits == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234/commits'
        assert self.pr._url_files == 'https://api.github.com/repos/fakerepo/doesnotexist/pulls/1234/files'

    def test_has_modified(self):
        mock = MagicMock(return_value=[{
            "sha":
            "bbcd538c8e72b8c175046e27cc8f907076331401",
            "filename":
            "file1.txt",
            "status":
            "added",
            "additions":
            103,
            "deletions":
            21,
            "changes":
            124,
            "blob_url":
            "https://github.com/blah/blah/blob/hash/file1.txt",
            "raw_url":
            "https://github.com/blaht/blah/raw/hash/file1.txt",
            "contents_url":
            "https://api.github.com/repos/blah/blah/contents/file1.txt?ref=hash",
            "patch":
            "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
        }])
        with patch('baldrick.github.github_api.paged_github_json_request',
                   mock):  # noqa
            assert self.pr.has_modified(['file1.txt'])
            assert self.pr.has_modified(['file1.txt', 'notthis.txt'])
            assert not self.pr.has_modified(['notthis.txt'])

    def test_set_check(self, app):
        with patch("baldrick.github.github_api.PullRequestHandler.json",
                   new_callable=PropertyMock) as json:
            json.return_value = {
                'head': {
                    'sha': 987654321
                },
                'base': {
                    'sha': 123456789
                }
            }
            with patch('requests.post') as post:
                self.pr.set_check("baldrick-1", "hello", name="test")
                expected_json = {
                    'external_id': 'baldrick-1',
                    'name': 'test',
                    'head_sha': 987654321,
                    'status': 'completed',
                    'output': {
                        'title': 'hello',
                        'summary': ''
                    },
                    'conclusion': 'neutral'
                }
                post.assert_called_once_with(
                    'https://api.github.com/repos/fakerepo/doesnotexist/check-runs',
                    headers={
                        'Accept': 'application/vnd.github.antiope-preview+json'
                    },
                    json=expected_json)

                post.reset_mock()

                self.pr.set_check("baldrick-1",
                                  "hello",
                                  name="test",
                                  commit_hash='base',
                                  text="hello world",
                                  summary="why hello")
                expected_json = {
                    'external_id': 'baldrick-1',
                    'name': 'test',
                    'head_sha': 123456789,
                    'status': 'completed',
                    'output': {
                        'title': 'hello',
                        'summary': 'why hello',
                        'text': 'hello world'
                    },
                    'conclusion': 'neutral'
                }
                post.assert_called_once_with(
                    'https://api.github.com/repos/fakerepo/doesnotexist/check-runs',
                    headers={
                        'Accept': 'application/vnd.github.antiope-preview+json'
                    },
                    json=expected_json)

                post.reset_mock()

                self.pr.set_check("baldrick-1",
                                  "hello",
                                  name="test",
                                  commit_hash='hello',
                                  details_url="this_is_a_url")
                expected_json = {
                    'external_id': 'baldrick-1',
                    'name': 'test',
                    'head_sha': 'hello',
                    'details_url': 'this_is_a_url',
                    'status': 'completed',
                    'output': {
                        'title': 'hello',
                        'summary': ''
                    },
                    'conclusion': 'neutral'
                }
                post.assert_called_once_with(
                    'https://api.github.com/repos/fakerepo/doesnotexist/check-runs',
                    headers={
                        'Accept': 'application/vnd.github.antiope-preview+json'
                    },
                    json=expected_json)

                post.reset_mock()

                self.pr.set_check("baldrick-1",
                                  "hello",
                                  name="test",
                                  status="completed",
                                  conclusion=None)
                expected_json = {
                    'external_id': 'baldrick-1',
                    'name': 'test',
                    'head_sha': 987654321,
                    'status': 'completed',
                    'output': {
                        'title': 'hello',
                        'summary': ''
                    },
                    'conclusion': 'neutral'
                }
                post.assert_called_once_with(
                    'https://api.github.com/repos/fakerepo/doesnotexist/check-runs',
                    headers={
                        'Accept': 'application/vnd.github.antiope-preview+json'
                    },
                    json=expected_json)