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