Skip to content

Commit ee114fc

Browse files
committed
pull_requests: return 404 on invalid repo or PR (bug 2002104) r=zeid
This PR updates the base GitHub utility to raise any HTTP error as exceptions, so they bubble up to higher layers. It then captures those in the Pull Request UI view to detect non-existent PRs, and return a cleaner 404 with appropriate message. It also does the same for non-existent Repos. The Jinja template is also updated to include the exception message, if present, to present more context to the user. Pull request: #741
1 parent 82054fa commit ee114fc

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/lando/ui/pull_requests.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.core.handlers.wsgi import WSGIRequest
44
from django.http import Http404
55
from django.template.response import TemplateResponse
6+
from requests import HTTPError
67

78
from lando.main.models import Repo
89
from lando.main.models.landing_job import get_jobs_for_pull
@@ -19,13 +20,27 @@ def get(
1920
self, request: WSGIRequest, repo_name: str, number: int, *args, **kwargs
2021
) -> TemplateResponse:
2122
"""Handle the GET request for the pull request view."""
22-
target_repo = Repo.objects.get(name=repo_name)
23+
try:
24+
target_repo = Repo.objects.get(name=repo_name)
25+
except Repo.DoesNotExist:
26+
raise Http404(f"Repository {repo_name} doesn't exist.")
2327

2428
if not target_repo.pr_enabled:
25-
raise Http404("Pull Requests are not supported for this repository.")
29+
raise Http404(
30+
f"Pull Requests are not supported for repository {repo_name}."
31+
)
2632

2733
client = GitHubAPIClient(target_repo.url)
28-
pull_request = client.build_pull_request(number)
34+
35+
try:
36+
pull_request = client.build_pull_request(number)
37+
except HTTPError as e:
38+
if e.response.status_code == 404:
39+
raise Http404(
40+
f"Pull request {repo_name}#{number} doesn't exist."
41+
) from e
42+
raise e
43+
2944
landing_jobs = get_jobs_for_pull(target_repo, number)
3045

3146
context = {

src/lando/utils/github.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def _repo_get(self, subpath: str, *args, **kwargs) -> dict | list:
184184

185185
def _get(self, path: str, *args, **kwargs) -> dict | list | str | None:
186186
result = self._api.get(path, *args, **kwargs)
187+
result.raise_for_status()
187188
content_type = result.headers["content-type"]
188189
if content_type == "application/json; charset=utf-8":
189190
return result.json()

0 commit comments

Comments
 (0)