Skip to content

Commit 44f0c25

Browse files
committed
github: add pull request endpoint (bug 1991125)
1 parent f9cc520 commit 44f0c25

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

src/lando/api/views.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@
99
from django.views import View
1010
from django.views.decorators.csrf import csrf_exempt
1111

12-
from lando.main.models import CommitMap
12+
from lando.main.models import CommitMap, Repo
1313
from lando.main.models.revision import DiffWarning, DiffWarningStatus
1414
from lando.main.scm import (
1515
SCM_TYPE_GIT,
1616
SCM_TYPE_HG,
1717
)
18+
from lando.utils.github import GitHubAPIClient, PullRequest
1819
from lando.utils.phabricator import get_phabricator_client
1920

2021

22+
class APIView(View):
23+
"""A base class for API views."""
24+
25+
pass
26+
27+
2128
def phabricator_api_key_required(func: callable) -> Callable:
2229
"""A simple wrapper that checks for a valid Phabricator API token."""
2330

@@ -148,3 +155,22 @@ class hg2gitCommitMapView(CommitMapBaseView):
148155
"""Return corresponding CommitMap given an hg hash."""
149156

150157
scm = SCM_TYPE_HG
158+
159+
160+
class PullRequestAPIView(APIView):
161+
"""Handle pull requests in the API."""
162+
163+
def get(self, request: WSGIRequest, repo_name: str, number: int) -> JsonResponse:
164+
"""Return a serialized JSON representation of a pull request."""
165+
target_repo = Repo.objects.get(name=repo_name)
166+
client = GitHubAPIClient(target_repo)
167+
pull_request = PullRequest(client.get_pull_request(number))
168+
return JsonResponse(pull_request.serialize(), status=200)
169+
170+
171+
class LandingJobAPIView(View):
172+
"""Handle landing jobs in the API."""
173+
174+
def post(self, request: WSGIRequest, *args, **kwargs):
175+
"""Placeholder for creating new landing jobs."""
176+
pass

src/lando/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from lando.api.legacy.api import landing_jobs
2222
from lando.api.views import (
2323
LegacyDiffWarningView,
24+
PullRequestAPIView,
2425
git2hgCommitMapView,
2526
hg2gitCommitMapView,
2627
)
@@ -85,6 +86,14 @@
8586
),
8687
]
8788

89+
urlpatterns += [
90+
path(
91+
"api/pulls/<str:repo_name>/<int:number>",
92+
PullRequestAPIView.as_view(),
93+
name="api-pull-request",
94+
),
95+
]
96+
8897
# "API" endpoints ported from legacy API app.
8998
urlpatterns += [
9099
path(

src/lando/utils/github.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,36 @@ def __init__(self, data: dict):
146146
self.user_id = data["user"]["id"]
147147
self.user_html_url = data["user"]["html_url"]
148148
self.user_login = data["user"]["login"]
149+
150+
def serialize(self) -> dict[str, str]:
151+
"""Return a dictionary with various pull request data."""
152+
return {
153+
"url": self.url,
154+
"base_ref": self.base_ref,
155+
"base_sha": self.base_sha,
156+
"base_user_login": self.base_user_login,
157+
"base_user_id": self.base_user_id,
158+
"created_at": self.created_at,
159+
"updated_at": self.updated_at,
160+
"closed_at": self.closed_at,
161+
"merged_at": self.merged_at,
162+
"diff_url": self.diff_url,
163+
"patch_url": self.patch_url,
164+
"body": self.body,
165+
"is_draft": self.is_draft,
166+
"comments_url": self.comments_url,
167+
"commits_url": self.commits_url,
168+
"head_ref": self.head_ref,
169+
"head_sha": self.head_sha,
170+
"head_repo_git_url": self.head_repo_git_url,
171+
"html_url": self.html_url,
172+
"id": self.id,
173+
"number": self.number,
174+
"requested_reviewers": self.requested_reviewers,
175+
"requested_teams": self.requested_teams,
176+
"state": self.state,
177+
"title": self.title,
178+
"user_id": self.user_id,
179+
"user_html_url": self.user_html_url,
180+
"user_login": self.user_login,
181+
}

0 commit comments

Comments
 (0)