Skip to content
Draft
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
14 changes: 14 additions & 0 deletions src/lando/api/legacy/api/landing_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from django.utils.decorators import method_decorator
from django.views import View

from lando.api.views import generate_enhanced_pr_description
from lando.main.auth import require_authenticated_user
from lando.main.models import JobAction, JobStatus, LandingJob
from lando.utils.exceptions import NotFoundProblemException
from lando.utils.github import GitHubAPIClient

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -99,6 +101,18 @@ def put(self, request: WSGIRequest, job_id: int) -> JsonResponse:
if landing_job.status in (JobStatus.SUBMITTED, JobStatus.DEFERRED):
landing_job.transition_status(JobAction.CANCEL)
landing_job.save()
if landing_job.is_pull_request_job:
client = GitHubAPIClient(landing_job.target_repo.url)
for revision in landing_job.revisions:
pull_request = client.build_pull_request(revision.pull_number)
description = generate_enhanced_pr_description(
pull_request,
landing_job.target_repo,
template="pr_description_landing.md",
)
client.update_pull_request_body(
revision.pull_number, description
)
return JsonResponse({"id": landing_job.id})
else:
data = {
Expand Down
9 changes: 9 additions & 0 deletions src/lando/api/legacy/workers/landing_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def run_job(self, job: LandingJob) -> bool:
True: The job finished processing and is in a permanent state.
False: The job encountered a temporary failure and should be tried again.
"""
from lando.api.views import generate_enhanced_pr_description

repo: Repo = job.target_repo
scm = repo.scm

Expand Down Expand Up @@ -139,6 +141,13 @@ def run_job(self, job: LandingJob) -> bool:
pull_number = job.revisions.first().pull_number
message = f"Pull request closed by commit {commit_id}"
client = GitHubAPIClient(job.target_repo.url)
pull_request = client.build_pull_request(pull_number)
description = generate_enhanced_pr_description(
pull_request,
job.target_repo,
template="pr_description_landing.md",
)
client.update_pull_request_body(pull_number, description)
client.add_comment_to_pull_request(pull_number, message)
client.close_pull_request(pull_number)

Expand Down
81 changes: 54 additions & 27 deletions src/lando/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,45 @@ def generate_warnings_and_blockers(
return {"warnings": warnings, "blockers": blockers}


def generate_enhanced_pr_description(
pull_request: PullRequest,
target_repo: Repo,
request: WSGIRequest = None,
template: str = "pr_description.md",
) -> str:
context = {}
if request:
context.update(
generate_warnings_and_blockers(target_repo, pull_request, request)
)

context["landing_status"] = str(
get_pull_request_last_landing_job_status(target_repo.name, pull_request.number)
).lower()

path = reverse(
"pull-request",
kwargs={
"repo_name": target_repo.name,
"number": pull_request.number,
},
)

context["lando_url"] = f"{settings.SITE_URL}{path}"
context["special_delimiter"] = SPECIAL_DELIMITER
bugs = parse_bugs(pull_request.title)
context["bugs"] = []

for bug in bugs:
context["bugs"].append((bug, f"{settings.BUGZILLA_URL}/{bug}"))

context["title"] = pull_request.title
context["body"] = pull_request.parsed_body

rendered = render_to_string(template, context)
return rendered


@method_decorator(csrf_exempt, name="dispatch")
class LegacyDiffWarningView(View):
"""
Expand Down Expand Up @@ -233,10 +272,10 @@ def get(
self, request: WSGIRequest, repo_name: int, pull_number: int
) -> JsonResponse:
"""Return the status of a pull request based on landing job counts."""
status = str(
landing_status = str(
get_pull_request_last_landing_job_status(repo_name, pull_number)
).lower()
return JsonResponse({"status": status}, status=200)
return JsonResponse({"status": landing_status}, status=200)

@method_decorator(require_authenticated_user)
def post(
Expand Down Expand Up @@ -302,6 +341,14 @@ class Form(forms.Form):
job.status = JobStatus.SUBMITTED
job.save()

description = generate_enhanced_pr_description(
self.pull_request,
self.target_repo,
request,
template="pr_description_landing.md",
)
self.client.update_pull_request_body(pull_number, description)

return JsonResponse({"id": job.id}, status=201)


Expand Down Expand Up @@ -378,30 +425,10 @@ def get(
if not request.user.has_perm("main.can_change_landing_job"):
raise PermissionError()

context = {}
context.update(
generate_warnings_and_blockers(self.target_repo, self.pull_request, request)
description = generate_enhanced_pr_description(
self.pull_request, self.target_repo, request
)

path = reverse(
"pull-request",
kwargs={
"repo_name": self.target_repo.name,
"number": self.pull_request.number,
},
self.client.update_pull_request_body(pull_number, description)
return JsonResponse(
{"status": f"Pull request {pull_number} description updated."}
)

context["lando_url"] = f"{settings.SITE_URL}{path}"
context["special_delimiter"] = SPECIAL_DELIMITER
bugs = parse_bugs(self.pull_request.title)
context["bugs"] = []

for bug in bugs:
context["bugs"].append((bug, f"{settings.BUGZILLA_URL}/{bug}"))

context["title"] = self.pull_request.title
context["body"] = self.pull_request.parsed_body

rendered = render_to_string("pr_description.md", context)
self.client.update_pull_request_body(pull_number, rendered)
return JsonResponse({"context": context, "md": rendered})
2 changes: 1 addition & 1 deletion src/lando/ui/jinja2/pr_description.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
{% endfor %}
{% endmacro %}

{% block upper %}
Lando: [link]({{ lando_url }})
{% if bugs %}Bugzilla: {% for bug in bugs %}[bug {{ bug.0 }}]({{ bug.1 }}){% endfor %}{% endif %}

{% block upper %}
{% if not warnings and not blockers %}
:white_check_mark: All Lando checks passed
{% endif %}
Expand Down
4 changes: 4 additions & 0 deletions src/lando/ui/jinja2/pr_description_landing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% extends "pr_description.md" %}
{% block upper %}
**Landing request {{ landing_status }}**
{% endblock %}
Loading