Skip to content
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
28 changes: 27 additions & 1 deletion src/lando/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@
from django.views import View
from django.views.decorators.csrf import csrf_exempt

from lando.main.models import CommitMap
from lando.main.models import CommitMap, Repo
from lando.main.models.revision import DiffWarning, DiffWarningStatus
from lando.main.scm import (
SCM_TYPE_GIT,
SCM_TYPE_HG,
)
from lando.utils.github import GitHubAPIClient, PullRequest
from lando.utils.phabricator import get_phabricator_client


class APIView(View):
"""A base class for API views."""

pass


def phabricator_api_key_required(func: callable) -> Callable:
"""A simple wrapper that checks for a valid Phabricator API token."""

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

scm = SCM_TYPE_HG


class PullRequestAPIView(APIView):
"""Handle pull requests in the API."""

def get(self, request: WSGIRequest, repo_name: str, number: int) -> JsonResponse:
"""Return a serialized JSON representation of a pull request."""
target_repo = Repo.objects.get(name=repo_name)
client = GitHubAPIClient(target_repo)
pull_request = PullRequest(client.get_pull_request(number))
return JsonResponse(pull_request.serialize(), status=200)


class LandingJobAPIView(View):
"""Handle landing jobs in the API."""

def post(self, request: WSGIRequest, *args, **kwargs):
"""Placeholder for creating new landing jobs."""
pass
Comment thread
zzzeid marked this conversation as resolved.
9 changes: 9 additions & 0 deletions src/lando/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from lando.api.legacy.api import landing_jobs
from lando.api.views import (
LegacyDiffWarningView,
PullRequestAPIView,
git2hgCommitMapView,
hg2gitCommitMapView,
)
Expand Down Expand Up @@ -85,6 +86,14 @@
),
]

urlpatterns += [
path(
"api/pulls/<str:repo_name>/<int:number>",
Comment thread
shtrom marked this conversation as resolved.
PullRequestAPIView.as_view(),
name="api-pull-request",
),
]

# "API" endpoints ported from legacy API app.
urlpatterns += [
path(
Expand Down
33 changes: 33 additions & 0 deletions src/lando/utils/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,36 @@ def __init__(self, data: dict):
self.user_id = data["user"]["id"]
self.user_html_url = data["user"]["html_url"]
self.user_login = data["user"]["login"]

def serialize(self) -> dict[str, str]:
"""Return a dictionary with various pull request data."""
return {
"url": self.url,
"base_ref": self.base_ref,
"base_sha": self.base_sha,
"base_user_login": self.base_user_login,
"base_user_id": self.base_user_id,
"created_at": self.created_at,
"updated_at": self.updated_at,
"closed_at": self.closed_at,
"merged_at": self.merged_at,
"diff_url": self.diff_url,
"patch_url": self.patch_url,
"body": self.body,
"is_draft": self.is_draft,
"comments_url": self.comments_url,
"commits_url": self.commits_url,
"head_ref": self.head_ref,
"head_sha": self.head_sha,
"head_repo_git_url": self.head_repo_git_url,
"html_url": self.html_url,
"id": self.id,
"number": self.number,
"requested_reviewers": self.requested_reviewers,
"requested_teams": self.requested_teams,
"state": self.state,
"title": self.title,
"user_id": self.user_id,
"user_html_url": self.user_html_url,
"user_login": self.user_login,
}