Skip to content

Commit 09353de

Browse files
committed
github: add author info to pull request (bug 1995006) (#656)
1 parent 342b6b4 commit 09353de

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

src/lando/api/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,11 @@ class Form(forms.Form):
224224
target_repo=target_repo, requester_email=ldap_username
225225
)
226226
revision = Revision.objects.create(pull_number=pull_request.number)
227+
author_name, author_email = pull_request.author
227228
patch_data = {
228229
# See bug 1995006 (to actually parse authorship info). Use placeholder for now.
229-
"author_name": "Author Name",
230-
"author_email": "Author Email <email@example.org>",
230+
"author_name": author_name,
231+
"author_email": author_email,
231232
"commit_message": pull_request.title,
232233
"timestamp": int(datetime.now().timestamp()),
233234
}

src/lando/utils/github.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import logging
33
import re
4+
from collections import Counter
45

56
import requests
67
from django.conf import settings
@@ -200,6 +201,10 @@ def get_patch(self, pull_number: int) -> str:
200201
headers={"Accept": "application/vnd.github.patch"},
201202
)
202203

204+
def get_pull_request_commits(self, pull_number: int) -> dict:
205+
"""Get all commits from specific pull request from the repo."""
206+
return self._repo_get(f"pulls/{pull_number}/commits")
207+
203208
def open_pull_request(self, pull_number: int) -> dict:
204209
"""Open the given pull request."""
205210
return self._post(
@@ -278,6 +283,21 @@ def __init__(self, client: GitHubAPIClient, data: dict):
278283
self.user_html_url = data["user"]["html_url"]
279284
self.user_login = data["user"]["login"]
280285

286+
def _select_commit_author(
287+
self, commits: list[dict]
288+
) -> tuple[str | None, str | None]:
289+
"""Select the most common author in commits."""
290+
# This method is ported from lando.api.legacy.revisions.select_diff_author.
291+
commits = [commit["commit"] for commit in commits]
292+
if not commits:
293+
return None, None
294+
295+
# Below is copied verbatim from the legacy method.
296+
authors = [c.get("author", {}) for c in commits]
297+
authors = Counter((a.get("name"), a.get("email")) for a in authors)
298+
authors = authors.most_common(1)
299+
return authors[0][0] if authors else (None, None)
300+
281301
@property
282302
def diff(self) -> str:
283303
return self.client.get_diff(self.number)
@@ -286,6 +306,14 @@ def diff(self) -> str:
286306
def patch(self) -> str:
287307
return self.client.get_patch(self.number)
288308

309+
@property
310+
def commits(self) -> str:
311+
return self.client.get_pull_request_commits(self.number)
312+
313+
@property
314+
def author(self) -> str:
315+
return self._select_commit_author(self.commits)
316+
289317
def serialize(self) -> dict[str, str]:
290318
"""Return a dictionary with various pull request data."""
291319
return {

0 commit comments

Comments
 (0)