From 23f43320da7de1568a654bcc4ef9f7453551f200 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 21 Mar 2023 11:22:18 +0100 Subject: [PATCH 1/6] Publish to PR in event file --- python/publish/publisher.py | 18 +++++++- python/test/test_publisher.py | 86 +++++++++++++++++++++++++++-------- 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index a15ee9ee..de55e6d2 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -206,6 +206,16 @@ def publish(self, else: logger.info('Commenting on pull requests disabled') + def get_pull_from_event(self) -> Optional[PullRequest]: + number = self._settings.event.get('pull_request', {}).get('number') + if number is None: + return None + + try: + return self._repo.get_pull(number) + except UnknownObjectException: + return None + def get_all_pulls(self, commit: str) -> List[PullRequest]: # totalCount of PaginatedList 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() @@ -214,7 +224,12 @@ def get_all_pulls(self, commit: str) -> List[PullRequest]: pull_requests = [issue.as_pull_request() for issue in issues] else: try: - pull_requests = list(self._repo.get_commit(commit).get_pulls()) + pull_request = self.get_pull_from_event() + pull_requests = [pull + for pull in list(self._repo.get_commit(commit).get_pulls()) + if pull_request is None or pull.number != pull_request.number] + if pull_request is not None: + pull_requests.append(pull_request) except UnknownObjectException: pull_requests = [] @@ -223,6 +238,7 @@ def get_all_pulls(self, commit: str) -> List[PullRequest]: def get_pulls(self, commit: str) -> List[PullRequest]: # get all pull requests associated with this commit + # TODO: simplify to event pr only, breaking change for version 3.0 pull_requests = self.get_all_pulls(commit) if logger.isEnabledFor(logging.DEBUG): diff --git a/python/test/test_publisher.py b/python/test/test_publisher.py index 833212f0..af3c0e2b 100644 --- a/python/test/test_publisher.py +++ b/python/test/test_publisher.py @@ -832,14 +832,37 @@ def test_reuse_comment_existing_updated(self): self.do_test_reuse_comment(earlier_body='comment already updated\n:recycle: Has been updated', expected_body='comment already updated\n:recycle: Has been updated') + def test_get_pull_from_event(self): + settings = self.create_settings() + gh, gha, req, repo, commit = self.create_mocks() + pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit) + repo.get_pull = mock.Mock(return_value=pr) + + publisher = Publisher(settings, gh, gha) + + actual = publisher.get_pull_from_event() + self.assertIsNone(actual) + repo.get_pull.assert_not_called() + + # test with pull request in event file + settings = self.create_settings(event={'pull_request': {'number': 1234}}) + publisher = Publisher(settings, gh, gha) + + actual = publisher.get_pull_from_event() + self.assertIs(actual, pr) + repo.get_pull.assert_called_once_with(1234) + def do_test_get_pulls(self, - settings: Settings, - pull_requests: mock.Mock, - expected: List[mock.Mock]) -> mock.Mock: + settings: Settings, + pull_requests: mock.Mock, + event_pull_request: Optional[mock.Mock], + expected: List[mock.Mock]) -> mock.Mock: gh, gha, req, repo, commit = self.create_mocks() gh.search_issues = mock.Mock(return_value=pull_requests) commit.get_pulls = mock.Mock(return_value=pull_requests) + if event_pull_request is not None: + repo.get_pull = mock.Mock(return_value=event_pull_request) publisher = Publisher(settings, gh, gha) @@ -857,46 +880,71 @@ def test_get_pulls(self): settings = self.create_settings() pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit) pull_requests = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, pull_requests, [pr]) + gha = self.do_test_get_pulls(settings, pull_requests, None, [pr]) + gha.warning.assert_not_called() + gha.error.assert_not_called() + + def test_get_pulls_with_same_event_pr(self): + settings = self.create_settings(event={'pull_request': {'number': 1234}}) + pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) + pull_requests = self.create_github_collection([pr]) + gha = self.do_test_get_pulls(settings, pull_requests, pr, [pr]) + gha.warning.assert_not_called() + gha.error.assert_not_called() + + def test_get_pulls_with_other_event_pr(self): + settings = self.create_settings(event={'pull_request': {'number': 1234}}) + event_pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) + pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=5678) + pull_requests = self.create_github_collection([pr]) + gha = self.do_test_get_pulls(settings, pull_requests, event_pr, [pr, event_pr]) + gha.warning.assert_not_called() + gha.error.assert_not_called() + + def test_get_pulls_only_with_event_pr(self): + settings = self.create_settings(event={'pull_request': {'number': 1234}}) + pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) + pull_requests = self.create_github_collection([]) + gha = self.do_test_get_pulls(settings, pull_requests, pr, [pr]) gha.warning.assert_not_called() gha.error.assert_not_called() - def test_get_pulls_no_search_results(self): + def test_get_pulls_no_pulls(self): settings = self.create_settings() pull_requests = self.create_github_collection([]) - gha = self.do_test_get_pulls(settings, pull_requests, []) + gha = self.do_test_get_pulls(settings, pull_requests, None, []) gha.warning.assert_not_called() gha.error.assert_not_called() - def test_get_pulls_one_closed_matches(self): + def test_get_pulls_one_closed_pull(self): settings = self.create_settings() pr = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) pull_requests = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, pull_requests, []) + gha = self.do_test_get_pulls(settings, pull_requests, None, []) gha.warning.assert_not_called() gha.error.assert_not_called() - def test_get_pulls_multiple_closed_matches(self): + def test_get_pulls_multiple_closed_pulls(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) pull_requests = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, pull_requests, []) + gha = self.do_test_get_pulls(settings, pull_requests, None, []) gha.warning.assert_not_called() gha.error.assert_not_called() - def test_get_pulls_one_closed_one_open_matches(self): + def test_get_pulls_one_closed_one_open_pulls(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) pull_requests = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, pull_requests, [pr2]) + gha = self.do_test_get_pulls(settings, pull_requests, None, [pr2]) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -907,7 +955,7 @@ def test_get_pulls_multiple_open_one_matches_head_commit(self): pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='other head commit', merge_commit_sha='merge two') pull_requests = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, pull_requests, [pr1]) + gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1]) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -918,7 +966,7 @@ def test_get_pulls_multiple_open_one_matches_merge_commit(self): pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha='other merge commit') pull_requests = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, pull_requests, [pr1]) + gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1]) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -929,7 +977,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') pull_requests = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, pull_requests, [pr1, pr2]) + gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1, pr2]) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -940,7 +988,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) pull_requests = self.create_github_collection([pr1, pr2]) - gha = self.do_test_get_pulls(settings, pull_requests, [pr1, pr2]) + gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1, pr2]) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -948,7 +996,7 @@ def test_get_pulls_forked_repo(self): settings = self.create_settings() fork = self.create_github_pr('other/fork', head_commit_sha=settings.commit) pull_requests = self.create_github_collection([fork]) - self.do_test_get_pulls(settings, pull_requests, []) + self.do_test_get_pulls(settings, pull_requests, None, []) def test_get_pulls_forked_repos_and_own_repo(self): settings = self.create_settings() @@ -958,13 +1006,13 @@ def test_get_pulls_forked_repos_and_own_repo(self): fork2 = self.create_github_pr('{}.fork'.format(settings.repo), head_commit_sha=settings.commit) pull_requests = self.create_github_collection([own, fork1, fork2]) - self.do_test_get_pulls(settings, pull_requests, [own]) + self.do_test_get_pulls(settings, pull_requests, None, [own]) def test_get_pulls_via_search(self): settings = self.create_settings(search_pull_requests=True) 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 = self.do_test_get_pulls(settings, search_issues, None, [pr]) gha.warning.assert_not_called() gha.error.assert_not_called() From 4e6df759e152d2fc73782d64e09b33eff88013c0 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 21 Mar 2023 11:24:27 +0100 Subject: [PATCH 2/6] Add some debugging around get_all_pulls --- python/publish/publisher.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index de55e6d2..c9d685ad 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -225,15 +225,18 @@ def get_all_pulls(self, commit: str) -> List[PullRequest]: else: try: pull_request = self.get_pull_from_event() + logger.debug(f'event pr: {pull_request}') pull_requests = [pull for pull in list(self._repo.get_commit(commit).get_pulls()) if pull_request is None or pull.number != pull_request.number] + logger.debug(f"commit's prs: {pull_requests}") if pull_request is not None: pull_requests.append(pull_request) except UnknownObjectException: pull_requests = [] logger.debug(f'found {len(pull_requests)} pull requests in repo {self._settings.repo} containing commit {commit}') + logger.debug(f'all prs: {pull_requests}') return pull_requests def get_pulls(self, commit: str) -> List[PullRequest]: From b7d77d2c8cbced671b2cab3dc32dd10c9355a8f0 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 21 Mar 2023 11:38:05 +0100 Subject: [PATCH 3/6] Do not publish to PRs from other repos' events --- python/publish/publisher.py | 3 ++- python/test/test_publisher.py | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index c9d685ad..e4567790 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -208,7 +208,8 @@ def publish(self, def get_pull_from_event(self) -> Optional[PullRequest]: number = self._settings.event.get('pull_request', {}).get('number') - if number is None: + repo = self._settings.event.get('pull_request', {}).get('base', {}).get('repo', {}).get('full_name') + if number is None or repo is None or repo != self._settings.repo: return None try: diff --git a/python/test/test_publisher.py b/python/test/test_publisher.py index af3c0e2b..54cefe32 100644 --- a/python/test/test_publisher.py +++ b/python/test/test_publisher.py @@ -845,7 +845,7 @@ def test_get_pull_from_event(self): repo.get_pull.assert_not_called() # test with pull request in event file - settings = self.create_settings(event={'pull_request': {'number': 1234}}) + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) publisher = Publisher(settings, gh, gha) actual = publisher.get_pull_from_event() @@ -885,7 +885,7 @@ def test_get_pulls(self): gha.error.assert_not_called() def test_get_pulls_with_same_event_pr(self): - settings = self.create_settings(event={'pull_request': {'number': 1234}}) + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) pull_requests = self.create_github_collection([pr]) gha = self.do_test_get_pulls(settings, pull_requests, pr, [pr]) @@ -893,7 +893,7 @@ def test_get_pulls_with_same_event_pr(self): gha.error.assert_not_called() def test_get_pulls_with_other_event_pr(self): - settings = self.create_settings(event={'pull_request': {'number': 1234}}) + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) event_pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=5678) pull_requests = self.create_github_collection([pr]) @@ -901,8 +901,17 @@ def test_get_pulls_with_other_event_pr(self): gha.warning.assert_not_called() gha.error.assert_not_called() + def test_get_pulls_with_other_repo_event_pr(self): + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'fork/repo'}}}}) + event_pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) + pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=5678) + pull_requests = self.create_github_collection([pr]) + gha = self.do_test_get_pulls(settings, pull_requests, event_pr, [pr]) + gha.warning.assert_not_called() + gha.error.assert_not_called() + def test_get_pulls_only_with_event_pr(self): - settings = self.create_settings(event={'pull_request': {'number': 1234}}) + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) pull_requests = self.create_github_collection([]) gha = self.do_test_get_pulls(settings, pull_requests, pr, [pr]) From 50d66dcb00fdd8d550060b1f32dff1f4ae54ae06 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 21 Mar 2023 11:51:33 +0100 Subject: [PATCH 4/6] Revert "Add some debugging around get_all_pulls" This reverts commit 4e6df759e152d2fc73782d64e09b33eff88013c0. --- python/publish/publisher.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index e4567790..eb85fcc8 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -226,18 +226,15 @@ def get_all_pulls(self, commit: str) -> List[PullRequest]: else: try: pull_request = self.get_pull_from_event() - logger.debug(f'event pr: {pull_request}') pull_requests = [pull for pull in list(self._repo.get_commit(commit).get_pulls()) if pull_request is None or pull.number != pull_request.number] - logger.debug(f"commit's prs: {pull_requests}") if pull_request is not None: pull_requests.append(pull_request) except UnknownObjectException: pull_requests = [] logger.debug(f'found {len(pull_requests)} pull requests in repo {self._settings.repo} containing commit {commit}') - logger.debug(f'all prs: {pull_requests}') return pull_requests def get_pulls(self, commit: str) -> List[PullRequest]: From a9e26e5add941fca84fb706d9b14164149555216 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 21 Mar 2023 12:04:16 +0100 Subject: [PATCH 5/6] Either use pull from event, or pulls from commit --- python/publish/publisher.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index eb85fcc8..07bf34ea 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -226,11 +226,10 @@ def get_all_pulls(self, commit: str) -> List[PullRequest]: else: try: pull_request = self.get_pull_from_event() - pull_requests = [pull - for pull in list(self._repo.get_commit(commit).get_pulls()) - if pull_request is None or pull.number != pull_request.number] - if pull_request is not None: - pull_requests.append(pull_request) + if pull_request is None: + pull_requests = list(self._repo.get_commit(commit).get_pulls()) + else: + pull_requests = [pull_request] except UnknownObjectException: pull_requests = [] From 67ca283a918217eb529dcd08116df57582f75560 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 21 Mar 2023 12:06:05 +0100 Subject: [PATCH 6/6] Publish only to event pull request, not all commits pull requests --- python/publish/publisher.py | 14 ++-- python/test/test_publisher.py | 118 +++++++++------------------------- 2 files changed, 33 insertions(+), 99 deletions(-) diff --git a/python/publish/publisher.py b/python/publish/publisher.py index 07bf34ea..ccf63c17 100644 --- a/python/publish/publisher.py +++ b/python/publish/publisher.py @@ -218,20 +218,14 @@ def get_pull_from_event(self) -> Optional[PullRequest]: return None def get_all_pulls(self, commit: str) -> List[PullRequest]: - # totalCount of PaginatedList 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() if self._settings.search_pull_requests: + # totalCount of PaginatedList 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}')) pull_requests = [issue.as_pull_request() for issue in issues] else: - try: - pull_request = self.get_pull_from_event() - if pull_request is None: - pull_requests = list(self._repo.get_commit(commit).get_pulls()) - else: - pull_requests = [pull_request] - except UnknownObjectException: - pull_requests = [] + pull_request = self.get_pull_from_event() + pull_requests = [pull_request] if pull_request is not None else [] logger.debug(f'found {len(pull_requests)} pull requests in repo {self._settings.repo} containing commit {commit}') return pull_requests diff --git a/python/test/test_publisher.py b/python/test/test_publisher.py index 54cefe32..2a875ef1 100644 --- a/python/test/test_publisher.py +++ b/python/test/test_publisher.py @@ -870,25 +870,21 @@ def do_test_get_pulls(self, self.assertEqual(expected, actual) if settings.search_pull_requests: gh.search_issues.assert_called_once_with('type:pr repo:"{}" {}'.format(settings.repo, settings.commit)) - commit.get_pulls.assert_not_called() else: gh.search_issues.assert_not_called() - commit.get_pulls.assert_called_once_with() + if event_pull_request is not None and \ + settings.repo == settings.event.get('pull_request', {}).get('base', {}).get('repo', {}).get('full_name'): + repo.get_pull.assert_called_once_with(event_pull_request.number) + else: + repo.get_pull.assert_not_called() + commit.get_pulls.assert_not_called() return gha - def test_get_pulls(self): + def test_get_pulls_without_event(self): settings = self.create_settings() pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit) pull_requests = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, pull_requests, None, [pr]) - gha.warning.assert_not_called() - gha.error.assert_not_called() - - def test_get_pulls_with_same_event_pr(self): - settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) - pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) - pull_requests = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, pull_requests, pr, [pr]) + gha = self.do_test_get_pulls(settings, pull_requests, None, []) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -897,7 +893,7 @@ def test_get_pulls_with_other_event_pr(self): event_pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=5678) pull_requests = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, pull_requests, event_pr, [pr, event_pr]) + gha = self.do_test_get_pulls(settings, pull_requests, event_pr, [event_pr]) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -906,7 +902,7 @@ def test_get_pulls_with_other_repo_event_pr(self): event_pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=1234) pr = self.create_github_pr(settings.repo, head_commit_sha=settings.commit, number=5678) pull_requests = self.create_github_collection([pr]) - gha = self.do_test_get_pulls(settings, pull_requests, event_pr, [pr]) + gha = self.do_test_get_pulls(settings, pull_requests, event_pr, []) gha.warning.assert_not_called() gha.error.assert_not_called() @@ -925,97 +921,41 @@ def test_get_pulls_no_pulls(self): gha.warning.assert_not_called() gha.error.assert_not_called() - def test_get_pulls_one_closed_pull(self): - settings = self.create_settings() - - pr = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit) - pull_requests = self.create_github_collection([pr]) - - gha = self.do_test_get_pulls(settings, pull_requests, None, []) - gha.warning.assert_not_called() - gha.error.assert_not_called() - - def test_get_pulls_multiple_closed_pulls(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) - pull_requests = self.create_github_collection([pr1, pr2]) - - gha = self.do_test_get_pulls(settings, pull_requests, None, []) - gha.warning.assert_not_called() - gha.error.assert_not_called() - - def test_get_pulls_one_closed_one_open_pulls(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) - pull_requests = self.create_github_collection([pr1, pr2]) - - gha = self.do_test_get_pulls(settings, pull_requests, None, [pr2]) - gha.warning.assert_not_called() - gha.error.assert_not_called() - - 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') - pull_requests = self.create_github_collection([pr1, pr2]) - - gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1]) + def test_get_pulls_closed_pull(self): + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) + pr = self.create_github_pr(settings.repo, state='closed', head_commit_sha=settings.commit, number=1234) + pull_requests = self.create_github_collection([]) + gha = self.do_test_get_pulls(settings, pull_requests, pr, []) gha.warning.assert_not_called() gha.error.assert_not_called() - 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') - pull_requests = self.create_github_collection([pr1, pr2]) - - gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1]) + def test_get_pulls_head_commit(self): + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) + pr = self.create_github_pr(settings.repo, state='open', head_commit_sha=settings.commit, merge_commit_sha='merge', number=1234) + pull_requests = self.create_github_collection([]) + gha = self.do_test_get_pulls(settings, pull_requests, pr, [pr]) gha.warning.assert_not_called() gha.error.assert_not_called() - 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') - pull_requests = self.create_github_collection([pr1, pr2]) + def test_get_pulls_merge_commit(self): + settings = self.create_settings(event={'pull_request': {'number': 1234, 'base': {'repo': {'full_name': 'owner/repo'}}}}) + pr1 = self.create_github_pr(settings.repo, state='open', head_commit_sha='one head commit', merge_commit_sha=settings.commit, number=1234) + pr2 = self.create_github_pr(settings.repo, state='open', head_commit_sha='two head commit', merge_commit_sha='other merge commit', number=1234) + pull_requests = self.create_github_collection([]) - gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1, pr2]) + gha = self.do_test_get_pulls(settings, pull_requests, pr1, [pr1]) gha.warning.assert_not_called() gha.error.assert_not_called() - 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) - pull_requests = self.create_github_collection([pr1, pr2]) - - gha = self.do_test_get_pulls(settings, pull_requests, None, [pr1, pr2]) + gha = self.do_test_get_pulls(settings, pull_requests, pr2, []) gha.warning.assert_not_called() gha.error.assert_not_called() def test_get_pulls_forked_repo(self): settings = self.create_settings() fork = self.create_github_pr('other/fork', head_commit_sha=settings.commit) - pull_requests = self.create_github_collection([fork]) - self.do_test_get_pulls(settings, pull_requests, None, []) - - 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) - fork1 = self.create_github_pr('other/fork', head_commit_sha=settings.commit) - fork2 = self.create_github_pr('{}.fork'.format(settings.repo), head_commit_sha=settings.commit) - pull_requests = self.create_github_collection([own, fork1, fork2]) - - self.do_test_get_pulls(settings, pull_requests, None, [own]) + pull_requests = self.create_github_collection([]) + self.do_test_get_pulls(settings, pull_requests, fork, []) def test_get_pulls_via_search(self): settings = self.create_settings(search_pull_requests=True)