Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish comment on pull request of event, or of commit otherwise #438

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion python/publish/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@ def get_pull_from_event(self) -> Optional[PullRequest]:
except UnknownObjectException:
return None

def get_pulls_from_commit(self, commit: str) -> List[PullRequest]:
try:
# 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()
return list(self._repo.get_commit(commit).get_pulls())
except UnknownObjectException:
return []

def get_all_pulls(self, commit: str) -> List[PullRequest]:
if self._settings.search_pull_requests:
# totalCount of PaginatedList calls the GitHub API just to get the total number
Expand All @@ -225,7 +233,7 @@ def get_all_pulls(self, commit: str) -> List[PullRequest]:
pull_requests = [issue.as_pull_request() for issue in issues]
else:
pull_request = self.get_pull_from_event()
pull_requests = [pull_request] if pull_request is not None else []
pull_requests = [pull_request] if pull_request is not None else self.get_pulls_from_commit(commit)
EnricoMi marked this conversation as resolved.
Show resolved Hide resolved

logger.debug(f'found {len(pull_requests)} pull requests in repo {self._settings.repo} containing commit {commit}')
return pull_requests
Expand Down
8 changes: 5 additions & 3 deletions python/test/test_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,21 +874,23 @@ 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()
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)
commit.get_pulls.assert_not_called()
else:
repo.get_pull.assert_not_called()
commit.get_pulls.assert_not_called()
commit.get_pulls.assert_called_once_with()
return gha

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, [])
gha = self.do_test_get_pulls(settings, pull_requests, None, [pr])
gha.warning.assert_not_called()
gha.error.assert_not_called()

Expand All @@ -906,7 +908,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, [])
gha = self.do_test_get_pulls(settings, pull_requests, event_pr, [pr])
gha.warning.assert_not_called()
gha.error.assert_not_called()

Expand Down