diff --git a/pretty_release_notes/models/pull_request.py b/pretty_release_notes/models/pull_request.py index 071834c..5c9a359 100644 --- a/pretty_release_notes/models/pull_request.py +++ b/pretty_release_notes/models/pull_request.py @@ -132,7 +132,10 @@ def set_reviewers(self) -> set[str]: return self.reviewers def get_author(self) -> str: - return self.backport_of.author if self.backport_of else self.author + if self.backport_of: + self.backport_of._set_backport_of() + return self.backport_of.get_author() + return self.author def get_summary_key(self) -> str: # Keep in mind that this needs to work before `self.backport_of` is initialised diff --git a/tests/test_pull_request.py b/tests/test_pull_request.py index 48803d5..f9e5666 100644 --- a/tests/test_pull_request.py +++ b/tests/test_pull_request.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + from pretty_release_notes.github_client import GitHubClient from pretty_release_notes.models.pull_request import PullRequest from pretty_release_notes.models.repository import Repository @@ -62,3 +64,65 @@ def test_revert_with_backport(): assert pr.is_revert is True assert pr.reverted_pr_number == "150" assert pr.backport_no == "200" # Should still detect backport + + +def test_get_author_chained_backports(): + """Test that get_author() traverses the full backport chain. + + Chain: PR4 (backport of PR3) -> PR3 (backport of PR2) -> PR2 (backport of PR1) -> PR1 (original) + get_author() on PR4 should return PR1's author, not PR3's or PR2's author. + """ + github = GitHubClient("test_token") + repo = Repository( + owner="frappe", + name="frappe", + url="https://api.github.com/repos/frappe/frappe", + html_url="https://github.com/frappe/frappe", + ) + + pr1 = PullRequest( + github=github, + repository=repo, + id=1, + title="feat: original feature", + body="", + html_url="https://example.com/1", + author="original_author", + ) + pr2 = PullRequest( + github=github, + repository=repo, + id=2, + title="feat: original feature (backport #1)", + body="", + html_url="https://example.com/2", + author="mergify[bot]", + backport_of=pr1, + ) + pr3 = PullRequest( + github=github, + repository=repo, + id=3, + title="feat: original feature (backport #2)", + body="", + html_url="https://example.com/3", + author="mergify[bot]", + backport_of=pr2, + ) + pr4 = PullRequest( + github=github, + repository=repo, + id=4, + title="feat: original feature (backport #3)", + body="", + html_url="https://example.com/4", + author="mergify[bot]", + backport_of=pr3, + ) + + # Mock _set_backport_of to avoid GitHub API calls; backport_of is pre-set above + with patch.object(PullRequest, "_set_backport_of"): + assert pr1.get_author() == "original_author" + assert pr2.get_author() == "original_author" + assert pr3.get_author() == "original_author" + assert pr4.get_author() == "original_author"