|
5 | 5 | from typing import Callable |
6 | 6 |
|
7 | 7 | from django import forms |
| 8 | +from django.conf import settings |
8 | 9 | from django.core.handlers.wsgi import WSGIRequest |
9 | 10 | from django.http import HttpRequest, JsonResponse |
| 11 | +from django.template.loader import render_to_string |
| 12 | +from django.urls import reverse |
10 | 13 | from django.utils.decorators import method_decorator |
11 | 14 | from django.utils.html import escape |
12 | 15 | from django.views import View |
13 | 16 | from django.views.decorators.csrf import csrf_exempt |
14 | 17 |
|
15 | | -from lando.api.legacy.commit_message import replace_reviewers |
| 18 | +from lando.api.legacy.commit_message import parse_bugs, replace_reviewers |
16 | 19 | from lando.main.auth import require_authenticated_user |
17 | 20 | from lando.main.models import ( |
18 | 21 | CommitMap, |
|
25 | 28 | from lando.main.models.landing_job import get_jobs_for_pull |
26 | 29 | from lando.main.models.revision import DiffWarning, DiffWarningStatus |
27 | 30 | from lando.main.scm import SCMType |
28 | | -from lando.utils.github import GitHubAPIClient, PullRequest, PullRequestPatchHelper |
| 31 | +from lando.utils.github import ( |
| 32 | + SPECIAL_DELIMITER, |
| 33 | + GitHubAPIClient, |
| 34 | + PullRequest, |
| 35 | + PullRequestPatchHelper, |
| 36 | +) |
29 | 37 | from lando.utils.github_checks import ( |
30 | 38 | ALL_PULL_REQUEST_BLOCKERS, |
31 | 39 | ALL_PULL_REQUEST_WARNINGS, |
@@ -372,3 +380,45 @@ def post( |
372 | 380 | job.save() |
373 | 381 |
|
374 | 382 | return JsonResponse({"id": job.id}, status=201) |
| 383 | + |
| 384 | + |
| 385 | +@method_decorator(csrf_exempt, name="dispatch") |
| 386 | +class PullRequestEnhancedDescriptionAPIView(PullRequestAPIView): |
| 387 | + """API methods to modify the pull request description box.""" |
| 388 | + |
| 389 | + @method_decorator(require_authenticated_user) |
| 390 | + def get( |
| 391 | + self, request: WSGIRequest, repo_name: str, pull_number: int |
| 392 | + ) -> JsonResponse: |
| 393 | + """Update PR description box with parsed content.""" |
| 394 | + |
| 395 | + if not request.user.has_perm("main.can_change_landing_job"): |
| 396 | + raise PermissionError() |
| 397 | + |
| 398 | + context = {} |
| 399 | + context.update( |
| 400 | + generate_warnings_and_blockers(self.target_repo, self.pull_request, request) |
| 401 | + ) |
| 402 | + |
| 403 | + path = reverse( |
| 404 | + "pull-request", |
| 405 | + kwargs={ |
| 406 | + "repo_name": self.target_repo.name, |
| 407 | + "number": self.pull_request.number, |
| 408 | + }, |
| 409 | + ) |
| 410 | + |
| 411 | + context["lando_url"] = f"{settings.SITE_URL}{path}" |
| 412 | + context["special_delimiter"] = SPECIAL_DELIMITER |
| 413 | + bugs = parse_bugs(self.pull_request.title) |
| 414 | + context["bugs"] = [] |
| 415 | + |
| 416 | + for bug in bugs: |
| 417 | + context["bugs"].append((bug, f"{settings.BUGZILLA_URL}/{bug}")) |
| 418 | + |
| 419 | + context["title"] = self.pull_request.title |
| 420 | + context["body"] = self.pull_request.parsed_body |
| 421 | + |
| 422 | + rendered = render_to_string("pr_description.md", context) |
| 423 | + self.client.update_pull_request_body(pull_number, rendered) |
| 424 | + return JsonResponse({"context": context, "md": rendered}) |
0 commit comments