From c4d3f38d2f79869d33c3d217541317345488569d Mon Sep 17 00:00:00 2001 From: Louis-Amaury Chaib Date: Sat, 2 Apr 2022 18:18:33 +0200 Subject: [PATCH 1/5] Transform get_pull to return full list of open PRs matching commit --- python/.gitignore | 1 + python/publish/publisher.py | 41 +++++++++--------- python/test/test_publisher.py | 78 +++++++++++++++++------------------ 3 files changed, 61 insertions(+), 59 deletions(-) create mode 100644 python/.gitignore diff --git a/python/.gitignore b/python/.gitignore new file mode 100644 index 00000000..0d20b648 --- /dev/null +++ b/python/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/python/publish/publisher.py b/python/publish/publisher.py index da513b51..4e05eba7 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -97,21 +97,22 @@ def publish(self, check_run = self.publish_check(stats, cases, conclusion) if self._settings.comment_mode != comment_mode_off: - pull = self.get_pull(self._settings.commit) - if pull is not None: - self.publish_comment(self._settings.comment_title, stats, pull, check_run, cases) - if self._settings.hide_comment_mode == hide_comments_mode_orphaned: - self.hide_orphaned_commit_comments(pull) - elif self._settings.hide_comment_mode == hide_comments_mode_all_but_latest: - self.hide_all_but_latest_comments(pull) - else: - logger.info('hide_comments disabled, not hiding any comments') + pulls = self.get_pulls(self._settings.commit) + if pulls: + for pull in pulls: + self.publish_comment(self._settings.comment_title, stats, pull, check_run, cases) + if self._settings.hide_comment_mode == hide_comments_mode_orphaned: + self.hide_orphaned_commit_comments(pull) + elif self._settings.hide_comment_mode == hide_comments_mode_all_but_latest: + self.hide_all_but_latest_comments(pull) + else: + logger.info('hide_comments disabled, not hiding any comments') else: logger.info(f'there is no pull request for commit {self._settings.commit}') else: logger.info('comment_on_pr disabled, not commenting on any pull requests') - def get_pull(self, commit: str) -> Optional[PullRequest]: + def get_pulls(self, commit: str) -> List[PullRequest]: # totalCount calls the GitHub API just to get the total number # we have to retrieve them all anyway so better do this once by materialising the PaginatedList via list() issues = list(self._gh.search_issues(f'type:pr repo:"{self._settings.repo}" {commit}')) @@ -134,30 +135,30 @@ def get_pull(self, commit: str) -> Optional[PullRequest]: if len(pulls) == 0: logger.debug(f'found no pull requests in repo {self._settings.repo} for commit {commit}') - return None + return [] # we only comment on PRs that have the commit as their current head or merge commit pulls = [pull for pull in pulls if commit in [pull.head.sha, pull.merge_commit_sha]] if len(pulls) == 0: logger.debug(f'found no pull request in repo {self._settings.repo} with ' f'commit {commit} as current head or merge commit') - return None + return [] - # if we still have multiple PRs, only comment on the open one + # if we still have multiple PRs, only comment on the open ones if len(pulls) > 1: pulls = [pull for pull in pulls if pull.state == 'open'] if len(pulls) == 0: logger.debug(f'found multiple pull requests in repo {self._settings.repo} with ' f'commit {commit} as current head or merge commit but none is open') - return None + return [] if len(pulls) > 1: - self._gha.error(f'Found multiple open pull requests in repo {self._settings.repo} with ' - f'commit {commit} as current head or merge commit') - return None + logger.warning(f'Found multiple open pull requests in repo {self._settings.repo} with ' + f'commit {commit} as current head or merge commit, ' + f'all of them will be decorated') - pull = pulls[0] - logger.debug(f'found pull request #{pull.number} with commit {commit} as current head or merge commit') - return pull + for pull in pulls: + logger.debug(f'found pull request #{pull.number} with commit {commit} as current head or merge commit') + return pulls def get_stats_from_commit(self, commit_sha: str) -> Optional[UnitTestRunResults]: check_run = self.get_check_run(commit_sha) diff --git a/python/test/test_publisher.py b/python/test/test_publisher.py index 2917942c..10d81f6e 100644 --- a/python/test/test_publisher.py +++ b/python/test/test_publisher.py @@ -226,7 +226,7 @@ def get_stats(base: str) -> UnitTestRunResults: def call_mocked_publish(settings: Settings, stats: UnitTestRunResults = stats, cases: UnitTestCaseResults = cases, - pr: object = None, + prs: List[object] = [], cr: object = None): # UnitTestCaseResults is mutable, always copy it cases = UnitTestCaseResults(cases) @@ -234,7 +234,7 @@ def call_mocked_publish(settings: Settings, # mock Publisher and call publish publisher = mock.MagicMock(Publisher) publisher._settings = settings - publisher.get_pull = mock.Mock(return_value=pr) + publisher.get_pulls = mock.Mock(return_value=prs) publisher.publish_check = mock.Mock(return_value=cr) Publisher.publish(publisher, stats, cases, 'success') @@ -246,7 +246,7 @@ def call_mocked_publish(settings: Settings, def test_publish_without_comment(self): settings = self.create_settings(comment_mode=comment_mode_off, hide_comment_mode=hide_comments_mode_off) - mock_calls = self.call_mocked_publish(settings, pr=object()) + mock_calls = self.call_mocked_publish(settings, prs=[object()]) self.assertEqual(1, len(mock_calls)) (method, args, kwargs) = mock_calls[0] @@ -256,7 +256,7 @@ def test_publish_without_comment(self): def test_publish_without_comment_with_hiding(self): settings = self.create_settings(comment_mode=comment_mode_off, hide_comment_mode=hide_comments_mode_all_but_latest) - mock_calls = self.call_mocked_publish(settings, pr=object()) + mock_calls = self.call_mocked_publish(settings, prs=[object()]) self.assertEqual(1, len(mock_calls)) (method, args, kwargs) = mock_calls[0] @@ -266,7 +266,7 @@ def test_publish_without_comment_with_hiding(self): def test_publish_with_comment_without_pr(self): settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_comments_mode_off) - mock_calls = self.call_mocked_publish(settings, pr=None) + mock_calls = self.call_mocked_publish(settings, prs=[]) self.assertEqual(2, len(mock_calls)) @@ -276,7 +276,7 @@ def test_publish_with_comment_without_pr(self): self.assertEqual({}, kwargs) (method, args, kwargs) = mock_calls[1] - self.assertEqual('get_pull', method) + self.assertEqual('get_pulls', method) self.assertEqual((settings.commit, ), args) self.assertEqual({}, kwargs) @@ -284,7 +284,7 @@ def test_publish_with_comment_without_hiding(self): pr = object() cr = object() settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_comments_mode_off) - mock_calls = self.call_mocked_publish(settings, pr=pr, cr=cr) + mock_calls = self.call_mocked_publish(settings, prs=[pr], cr=cr) self.assertEqual(3, len(mock_calls)) @@ -294,7 +294,7 @@ def test_publish_with_comment_without_hiding(self): self.assertEqual({}, kwargs) (method, args, kwargs) = mock_calls[1] - self.assertEqual('get_pull', method) + self.assertEqual('get_pulls', method) self.assertEqual((settings.commit, ), args) self.assertEqual({}, kwargs) @@ -307,7 +307,7 @@ def do_test_publish_with_comment_with_hide(self, hide_mode: str, hide_method: st pr = object() cr = object() settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_mode) - mock_calls = self.call_mocked_publish(settings, pr=pr, cr=cr) + mock_calls = self.call_mocked_publish(settings, prs=[pr], cr=cr) self.assertEqual(4, len(mock_calls)) @@ -317,7 +317,7 @@ def do_test_publish_with_comment_with_hide(self, hide_mode: str, hide_method: st self.assertEqual({}, kwargs) (method, args, kwargs) = mock_calls[1] - self.assertEqual('get_pull', method) + self.assertEqual('get_pulls', method) self.assertEqual((settings.commit, ), args) self.assertEqual({}, kwargs) @@ -347,7 +347,7 @@ def test_publish_without_compare(self): pr = object() cr = object() settings = self.create_settings(comment_mode=comment_mode_create, hide_comment_mode=hide_comments_mode_all_but_latest, compare_earlier=False) - mock_calls = self.call_mocked_publish(settings, pr=pr, cr=cr) + mock_calls = self.call_mocked_publish(settings, prs=[pr], cr=cr) self.assertEqual(4, len(mock_calls)) @@ -357,7 +357,7 @@ def test_publish_without_compare(self): self.assertEqual({}, kwargs) (method, args, kwargs) = mock_calls[1] - self.assertEqual('get_pull', method) + self.assertEqual('get_pulls', method) self.assertEqual((settings.commit, ), args) self.assertEqual({}, kwargs) @@ -579,109 +579,109 @@ def test_reuse_comment_existing_updated(self): body='comment already updated\n:recycle: Has been updated', expected_body='comment already updated\n:recycle: Has been updated') - def do_test_get_pull(self, + def do_test_get_pulls(self, settings: Settings, search_issues: mock.Mock, - expected: Optional[mock.Mock]) -> mock.Mock: + expected: List[mock.Mock]) -> mock.Mock: gh, gha, req, repo, commit = self.create_mocks() gh.search_issues = mock.Mock(return_value=search_issues) publisher = Publisher(settings, gh, gha) - actual = publisher.get_pull(settings.commit) + actual = publisher.get_pulls(settings.commit) self.assertEqual(expected, actual) gh.search_issues.assert_called_once_with('type:pr repo:"{}" {}'.format(settings.repo, settings.commit)) return gha - def test_get_pull(self): + def test_get_pulls(self): settings = self.create_settings() pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit) search_issues = self.create_github_collection([pr]) - gha = self.do_test_get_pull(settings, search_issues, pr) + gha = self.do_test_get_pulls(settings, search_issues, [pr]) gha.error.assert_not_called() - def test_get_pull_no_search_results(self): + def test_get_pulls_no_search_results(self): settings = self.create_settings() search_issues = self.create_github_collection([]) - gha = self.do_test_get_pull(settings, search_issues, None) + gha = self.do_test_get_pulls(settings, search_issues, []) gha.error.assert_not_called() - def test_get_pull_one_closed_matches(self): + def test_get_pulls_one_closed_matches(self): settings = self.create_settings() pr = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) search_issues = self.create_github_collection([pr]) - gha = self.do_test_get_pull(settings, search_issues, pr) + gha = self.do_test_get_pulls(settings, search_issues, [pr]) gha.error.assert_not_called() - def test_get_pull_multiple_closed_matches(self): + def test_get_pulls_multiple_closed_matches(self): settings = self.create_settings() pr1 = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) pr2 = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pull(settings, search_issues, None) + gha = self.do_test_get_pulls(settings, search_issues, []) gha.error.assert_not_called() - def test_get_pull_one_closed_one_open_matches(self): + def test_get_pulls_one_closed_one_open_matches(self): settings = self.create_settings() pr1 = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit) search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pull(settings, search_issues, pr2) + gha = self.do_test_get_pulls(settings, search_issues, [pr2]) gha.error.assert_not_called() - def test_get_pull_multiple_open_one_matches_head_commit(self): + def test_get_pulls_multiple_open_one_matches_head_commit(self): settings = self.create_settings() pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge one') pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='other head commit', merge_commit_sha='merge two') search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pull(settings, search_issues, pr1) + gha = self.do_test_get_pulls(settings, search_issues, [pr1]) gha.error.assert_not_called() - def test_get_pull_multiple_open_one_matches_merge_commit(self): + def test_get_pulls_multiple_open_one_matches_merge_commit(self): settings = self.create_settings() pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha='one head commit', merge_commit_sha=settings.commit) pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha='other merge commit') search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pull(settings, search_issues, pr1) + gha = self.do_test_get_pulls(settings, search_issues, [pr1]) gha.error.assert_not_called() - def test_get_pull_multiple_open_both_match_head_commit(self): + def test_get_pulls_multiple_open_both_match_head_commit(self): settings = self.create_settings() pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge one') pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge two') search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pull(settings, search_issues, None) - gha.error.assert_called_once_with('Found multiple open pull requests in repo owner/repo with commit commit as current head or merge commit') + gha = self.do_test_get_pulls(settings, search_issues, [pr1,pr2]) + gha.error.assert_not_called() - def test_get_pull_multiple_open_both_match_merge_commit(self): + def test_get_pulls_multiple_open_both_match_merge_commit(self): settings = self.create_settings() pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha='one head commit', merge_commit_sha=settings.commit) pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha=settings.commit) search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pull(settings, search_issues, None) - gha.error.assert_called_once_with('Found multiple open pull requests in repo owner/repo with commit commit as current head or merge commit') + gha = self.do_test_get_pulls(settings, search_issues, [pr1,pr2]) + gha.error.assert_not_called() - def test_get_pull_forked_repo(self): + def test_get_pulls_forked_repo(self): settings = self.create_settings() fork = self.create_github_pr('other/fork', head_commit_sha=settings.commit) search_issues = self.create_github_collection([fork]) - self.do_test_get_pull(settings, search_issues, None) + self.do_test_get_pulls(settings, search_issues, []) - def test_get_pull_forked_repos_and_own_repo(self): + def test_get_pulls_forked_repos_and_own_repo(self): settings = self.create_settings() own = self.create_github_pr(settings.repo, head_commit_sha=settings.commit) @@ -689,7 +689,7 @@ def test_get_pull_forked_repos_and_own_repo(self): fork2 = self.create_github_pr('{}.fork'.format(settings.repo), head_commit_sha=settings.commit) search_issues = self.create_github_collection([own, fork1, fork2]) - self.do_test_get_pull(settings, search_issues, own) + self.do_test_get_pulls(settings, search_issues, [own]) def do_test_get_check_run_from_list(self, runs: List[github.CheckRun.CheckRun], expected: Optional[github.CheckRun.CheckRun]): settings = self.create_settings() From 394ee50a87b6959e1ff5dfb823b5ad176759eff8 Mon Sep 17 00:00:00 2001 From: Louis-Amaury Chaib Date: Sun, 3 Apr 2022 18:19:52 +0200 Subject: [PATCH 2/5] Changes following code review on #244 --- python/publish/publisher.py | 18 ++++++------------ python/test/test_publisher.py | 4 ++-- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index 4e05eba7..081410de 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -99,13 +99,13 @@ def publish(self, if self._settings.comment_mode != comment_mode_off: pulls = self.get_pulls(self._settings.commit) if pulls: - for pull in pulls: + for index, pull in enumerate(pulls): self.publish_comment(self._settings.comment_title, stats, pull, check_run, cases) if self._settings.hide_comment_mode == hide_comments_mode_orphaned: self.hide_orphaned_commit_comments(pull) elif self._settings.hide_comment_mode == hide_comments_mode_all_but_latest: self.hide_all_but_latest_comments(pull) - else: + elif index==0: # only log for the first PR logger.info('hide_comments disabled, not hiding any comments') else: logger.info(f'there is no pull request for commit {self._settings.commit}') @@ -145,16 +145,10 @@ def get_pulls(self, commit: str) -> List[PullRequest]: return [] # if we still have multiple PRs, only comment on the open ones - if len(pulls) > 1: - pulls = [pull for pull in pulls if pull.state == 'open'] - if len(pulls) == 0: - logger.debug(f'found multiple pull requests in repo {self._settings.repo} with ' - f'commit {commit} as current head or merge commit but none is open') - return [] - if len(pulls) > 1: - logger.warning(f'Found multiple open pull requests in repo {self._settings.repo} with ' - f'commit {commit} as current head or merge commit, ' - f'all of them will be decorated') + pulls = [pull for pull in pulls if pull.state == 'open'] + if len(pulls) == 0: + logger.debug(f'found multiple pull requests in repo {self._settings.repo} with ' + f'commit {commit} as current head or merge commit but none is open') for pull in pulls: logger.debug(f'found pull request #{pull.number} with commit {commit} as current head or merge commit') diff --git a/python/test/test_publisher.py b/python/test/test_publisher.py index 10d81f6e..fe56d547 100644 --- a/python/test/test_publisher.py +++ b/python/test/test_publisher.py @@ -662,7 +662,7 @@ def test_get_pulls_multiple_open_both_match_head_commit(self): pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge two') search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, search_issues, [pr1,pr2]) + gha = self.do_test_get_pulls(settings, search_issues, [pr1, pr2]) gha.error.assert_not_called() def test_get_pulls_multiple_open_both_match_merge_commit(self): @@ -672,7 +672,7 @@ def test_get_pulls_multiple_open_both_match_merge_commit(self): pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha=settings.commit) search_issues = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, search_issues, [pr1,pr2]) + gha = self.do_test_get_pulls(settings, search_issues, [pr1, pr2]) gha.error.assert_not_called() def test_get_pulls_forked_repo(self): From 46de223a3bfc229a223463d5d9b53ddc5c84c395 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Sun, 3 Apr 2022 20:16:28 +0200 Subject: [PATCH 3/5] Simplify logging the fact that we are not hiding any comments --- python/publish/publisher.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index 081410de..7879f0b8 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -11,7 +11,7 @@ from github.CheckRunAnnotation import CheckRunAnnotation from github.PullRequest import PullRequest -from publish import hide_comments_mode_orphaned, hide_comments_mode_all_but_latest, \ +from publish import hide_comments_mode_orphaned, hide_comments_mode_all_but_latest, hide_comments_mode_off, \ comment_mode_off, comment_mode_create, comment_mode_update, digest_prefix, \ get_stats_from_digest, digest_header, get_short_summary, get_long_summary_md, \ get_long_summary_with_digest_md, get_error_annotations, get_case_annotations, \ @@ -99,14 +99,14 @@ def publish(self, if self._settings.comment_mode != comment_mode_off: pulls = self.get_pulls(self._settings.commit) if pulls: - for index, pull in enumerate(pulls): + for pull in pulls: self.publish_comment(self._settings.comment_title, stats, pull, check_run, cases) if self._settings.hide_comment_mode == hide_comments_mode_orphaned: self.hide_orphaned_commit_comments(pull) elif self._settings.hide_comment_mode == hide_comments_mode_all_but_latest: self.hide_all_but_latest_comments(pull) - elif index==0: # only log for the first PR - logger.info('hide_comments disabled, not hiding any comments') + if self._settings.hide_comment_mode == hide_comments_mode_off: + logger.info('hide_comments disabled, not hiding any comments') else: logger.info(f'there is no pull request for commit {self._settings.commit}') else: From 2235a680b3494d8d80cb6a382e70ab18436f82f6 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Sun, 3 Apr 2022 20:53:36 +0200 Subject: [PATCH 4/5] Rework logging messages --- python/publish/publisher.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index 7879f0b8..8607ce8d 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -144,14 +144,14 @@ def get_pulls(self, commit: str) -> List[PullRequest]: f'commit {commit} as current head or merge commit') return [] - # if we still have multiple PRs, only comment on the open ones + # only comment on the open PRs pulls = [pull for pull in pulls if pull.state == 'open'] if len(pulls) == 0: logger.debug(f'found multiple pull requests in repo {self._settings.repo} with ' - f'commit {commit} as current head or merge commit but none is open') + f'commit {commit} as current head or merge commit but none is open') for pull in pulls: - logger.debug(f'found pull request #{pull.number} with commit {commit} as current head or merge commit') + logger.debug(f'found open pull request #{pull.number} with commit {commit} as current head or merge commit') return pulls def get_stats_from_commit(self, commit_sha: str) -> Optional[UnitTestRunResults]: @@ -386,8 +386,8 @@ def publish_comment(self, # reuse existing commend when comment_mode == comment_mode_update # if none exists or comment_mode != comment_mode_update, create new comment if self._settings.comment_mode != comment_mode_update or not self.reuse_comment(pull_request, body): - logger.info('creating comment') - pull_request.create_issue_comment(body) + comment = pull_request.create_issue_comment(body) + logger.info(f'created comment for pull request #{pull_request.number}: {comment.html_url}') return pull_request @@ -404,12 +404,13 @@ def reuse_comment(self, pull: PullRequest, body: str) -> bool: # edit last comment comment_id = comments[-1].get("databaseId") - logger.info(f'editing comment {comment_id}') if ':recycle:' not in body: body = f'{body}\n:recycle: This comment has been updated with latest results.' try: - pull.get_issue_comment(comment_id).edit(body) + comment = pull.get_issue_comment(comment_id) + comment.edit(body) + logger.info(f'edited comment for pull request #{pull.number}: {comment.html_url}') except Exception as e: self._gha.warning(f'Failed to edit existing comment #{comment_id}') logger.debug('editing existing comment failed', exc_info=e) From 4bcd0b475d505fa0be5b8949d0da2585b9e2d8c2 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Sun, 3 Apr 2022 21:25:53 +0200 Subject: [PATCH 5/5] Fix tests --- python/test/test_publisher.py | 49 +++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/python/test/test_publisher.py b/python/test/test_publisher.py index fe56d547..025cd3bb 100644 --- a/python/test/test_publisher.py +++ b/python/test/test_publisher.py @@ -31,7 +31,7 @@ def create_github_pr(repo: str, head_commit_sha: Optional[str] = 'head', merge_commit_sha: Optional[str] = 'merge', number: Optional[int] = None, - state: Optional[str] = None): + state: str = 'open'): pr = mock.MagicMock() pr.as_pull_request = mock.Mock(return_value=pr) pr.base.repo.full_name = repo @@ -413,13 +413,23 @@ def test_publish_comment_compare_earlier(self): self.assertEqual({}, kwargs) mock_calls = pr.mock_calls - self.assertEqual(1, len(mock_calls)) + self.assertEqual(3, len(mock_calls)) (method, args, kwargs) = mock_calls[0] self.assertEqual('create_issue_comment', method) self.assertEqual(('## title\nbody', ), args) self.assertEqual({}, kwargs) + (method, args, kwargs) = mock_calls[1] + self.assertEqual('number.__str__', method) + self.assertEqual((), args) + self.assertEqual({}, kwargs) + + (method, args, kwargs) = mock_calls[2] + self.assertEqual('create_issue_comment().html_url.__str__', method) + self.assertEqual((), args) + self.assertEqual({}, kwargs) + def test_publish_comment_compare_with_itself(self): pr = mock.MagicMock() cr = mock.MagicMock() @@ -478,13 +488,23 @@ def test_publish_comment_compare_with_None(self): self.assertEqual({}, kwargs) mock_calls = pr.mock_calls - self.assertEqual(1, len(mock_calls)) + self.assertEqual(3, len(mock_calls)) (method, args, kwargs) = mock_calls[0] self.assertEqual('create_issue_comment', method) self.assertEqual(('## title\nbody', ), args) self.assertEqual({}, kwargs) + (method, args, kwargs) = mock_calls[1] + self.assertEqual('number.__str__', method) + self.assertEqual((), args) + self.assertEqual({}, kwargs) + + (method, args, kwargs) = mock_calls[2] + self.assertEqual('create_issue_comment().html_url.__str__', method) + self.assertEqual((), args) + self.assertEqual({}, kwargs) + def do_test_publish_comment_with_reuse_comment(self, one_exists: bool): pr = mock.MagicMock() cr = mock.MagicMock() @@ -512,7 +532,7 @@ def do_test_publish_comment_with_reuse_comment(self, one_exists: bool): self.assertEqual({}, kwargs) mock_calls = pr.mock_calls - self.assertEqual(0 if one_exists else 1, len(mock_calls)) + self.assertEqual(0 if one_exists else 3, len(mock_calls)) if not one_exists: (method, args, kwargs) = mock_calls[0] @@ -520,6 +540,16 @@ def do_test_publish_comment_with_reuse_comment(self, one_exists: bool): self.assertEqual(('## title\nbody', ), args) self.assertEqual({}, kwargs) + (method, args, kwargs) = mock_calls[1] + self.assertEqual('number.__str__', method) + self.assertEqual((), args) + self.assertEqual({}, kwargs) + + (method, args, kwargs) = mock_calls[2] + self.assertEqual('create_issue_comment().html_url.__str__', method) + self.assertEqual((), args) + self.assertEqual({}, kwargs) + def test_publish_comment_with_reuse_comment_none_existing(self): self.do_test_publish_comment_with_reuse_comment(one_exists=False) @@ -598,12 +628,14 @@ def test_get_pulls(self): pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit) search_issues = self.create_github_collection([pr]) gha = self.do_test_get_pulls(settings, search_issues, [pr]) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_no_search_results(self): settings = self.create_settings() search_issues = self.create_github_collection([]) gha = self.do_test_get_pulls(settings, search_issues, []) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_one_closed_matches(self): @@ -612,7 +644,8 @@ def test_get_pulls_one_closed_matches(self): pr = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) search_issues = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, search_issues, [pr]) + gha = self.do_test_get_pulls(settings, search_issues, []) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_multiple_closed_matches(self): @@ -623,6 +656,7 @@ def test_get_pulls_multiple_closed_matches(self): search_issues = self.create_github_collection([pr1, pr2]) gha = self.do_test_get_pulls(settings, search_issues, []) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_one_closed_one_open_matches(self): @@ -633,6 +667,7 @@ def test_get_pulls_one_closed_one_open_matches(self): search_issues = self.create_github_collection([pr1, pr2]) gha = self.do_test_get_pulls(settings, search_issues, [pr2]) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_multiple_open_one_matches_head_commit(self): @@ -643,6 +678,7 @@ def test_get_pulls_multiple_open_one_matches_head_commit(self): search_issues = self.create_github_collection([pr1, pr2]) gha = self.do_test_get_pulls(settings, search_issues, [pr1]) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_multiple_open_one_matches_merge_commit(self): @@ -653,6 +689,7 @@ def test_get_pulls_multiple_open_one_matches_merge_commit(self): search_issues = self.create_github_collection([pr1, pr2]) gha = self.do_test_get_pulls(settings, search_issues, [pr1]) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_multiple_open_both_match_head_commit(self): @@ -663,6 +700,7 @@ def test_get_pulls_multiple_open_both_match_head_commit(self): search_issues = self.create_github_collection([pr1, pr2]) gha = self.do_test_get_pulls(settings, search_issues, [pr1, pr2]) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_multiple_open_both_match_merge_commit(self): @@ -673,6 +711,7 @@ def test_get_pulls_multiple_open_both_match_merge_commit(self): search_issues = self.create_github_collection([pr1, pr2]) gha = self.do_test_get_pulls(settings, search_issues, [pr1, pr2]) + gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_forked_repo(self):