[GHF] Add co-authors to PR (#118347)

Mention co-authors in PR body

Modify `CommitAuthors` to include query first two commit `authors`, which makes sure that authors from suggested commits are recognized.

Test plan: CI + check `get_authors()` on a few PRs
Pull Request resolved: https://github.com/pytorch/pytorch/pull/118347
Approved by: https://github.com/kit1980
This commit is contained in:
Nikita Shulga
2024-01-27 01:02:49 +00:00
committed by PyTorch MergeBot
parent 4d771c56de
commit 7cc7bf9dda
3 changed files with 28 additions and 18 deletions

Binary file not shown.

View File

@ -434,6 +434,13 @@ class TestTryMerge(TestCase):
assert pr._reviews is not None # to pacify mypy
self.assertGreater(len(pr._reviews), 100)
def get_co_authors(self, *args: Any) -> None:
"""Tests that co-authors are recognized"""
pr = GitHubPR("pytorch", "pytorch", 118347)
authors = pr.get_authors()
self.assertIn("kit1980", authors)
self.assertIn("Co-authored-by:", pr.gen_commit_message())
def test_get_checkruns_many_runs(self, *args: Any) -> None:
"""Tests that all checkruns can be fetched"""
pr = GitHubPR("pytorch", "pytorch", 105260)

View File

@ -152,12 +152,14 @@ GH_COMMIT_AUTHORS_FRAGMENT = """
fragment CommitAuthors on PullRequestCommitConnection {
nodes {
commit {
author {
user {
login
authors(first: 2) {
nodes {
user {
login
}
email
name
}
email
name
}
oid
}
@ -846,14 +848,14 @@ class GitHubPR:
def add_authors(info: Dict[str, Any]) -> None:
for node in info["commits_with_authors"]["nodes"]:
author_node = node["commit"]["author"]
user_node = author_node["user"]
author = f"{author_node['name']} <{author_node['email']}>"
if user_node is None:
# If author is not github user, user node will be null
authors.append(("", author))
else:
authors.append((cast(str, user_node["login"]), author))
for author_node in node["commit"]["authors"]["nodes"]:
user_node = author_node["user"]
author = f"{author_node['name']} <{author_node['email']}>"
if user_node is None:
# If author is not github user, user node will be null
authors.append(("", author))
else:
authors.append((cast(str, user_node["login"]), author))
info = self.info
for _ in range(100):
@ -949,11 +951,6 @@ class GitHubPR:
def get_authors(self) -> Dict[str, str]:
rc = {}
# TODO: replace with `self.get_commit_count()` when GraphQL pagination can be used
# to fetch all commits, see https://gist.github.com/malfet/4f35321b0c9315bcd7116c7b54d83372
# and https://support.github.com/ticket/enterprise/1642/1659119
if self.get_commit_count() <= 250:
assert len(self._fetch_authors()) == self.get_commit_count()
for idx in range(len(self._fetch_authors())):
rc[self.get_committer_login(idx)] = self.get_committer_author(idx)
@ -1115,6 +1112,12 @@ class GitHubPR:
msg_body = re.sub(RE_GHSTACK_DESC, "", msg_body)
msg = self.get_title() + f" (#{self.pr_num})\n\n"
msg += msg_body
# Mention PR co-authors
for author_login, author_name in self.get_authors().items():
if author_login != self.get_pr_creator_login():
msg += f"\nCo-authored-by: {author_name}"
msg += f"\nPull Request resolved: {self.get_pr_url()}\n"
msg += f"Approved by: {approved_by_urls}\n"
if ghstack_deps: