From 2eef243293eaeac687caba34827b0ce734d3bc51 Mon Sep 17 00:00:00 2001 From: Sanchit Date: Tue, 17 Nov 2026 05:54:16 +0000 Subject: [PATCH] fix(views): render pipeline run pages for unregistered pipelines Schedules persist in the database, so a pipeline that was removed or renamed leaves behind a schedule whose id is no longer registered. PipelineSchedule.pipeline_class raised KeyError for such ids, and the pipeline run list and run detail pages crashed with a 500 instead of rendering the stored runs. Return None from pipeline_class for unknown ids and fall back to the raw pipeline id as the page name in both views; pipeline_url also returns None when the class is unknown, since the run detail template links the commit through it. Add regression tests covering both pages with an unregistered pipeline id. Signed-off-by: Sanchit --- vulnerabilities/models.py | 10 +++++++--- vulnerabilities/tests/test_view.py | 24 ++++++++++++++++++++++++ vulnerabilities/views.py | 14 ++++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/vulnerabilities/models.py b/vulnerabilities/models.py index 521f0ca6a..21e4a7c2f 100644 --- a/vulnerabilities/models.py +++ b/vulnerabilities/models.py @@ -2139,7 +2139,7 @@ def runtime(self): @property def pipeline_url(self): """Return pipeline URL based on commit and class module path.""" - if not self.vulnerablecode_commit: + if not self.vulnerablecode_commit or not self.pipeline_class: return None base_url = "https://github.com/aboutcode-org/vulnerablecode/blob/" @@ -2362,7 +2362,11 @@ def save(self, *args, **kwargs): @property def pipeline_class(self): - """Return the pipeline class.""" + """Return the pipeline class, or None if the pipeline is not registered. + + Schedules persist in the database, so a pipeline that was removed or + renamed leaves behind a schedule whose id is no longer registered. + """ from vulnerabilities.importers import IMPORTERS_REGISTRY from vulnerabilities.improvers import IMPROVERS_REGISTRY @@ -2373,7 +2377,7 @@ def pipeline_class(self): IMPORTERS_REGISTRY | IMPROVERS_REGISTRY | EXPORTERS_REGISTRY | MANAGEMENT_REGISTRY ) - return pipeline_registry[self.pipeline_id] + return pipeline_registry.get(self.pipeline_id) @property def description(self): diff --git a/vulnerabilities/tests/test_view.py b/vulnerabilities/tests/test_view.py index bcee104ac..e476ac6cb 100644 --- a/vulnerabilities/tests/test_view.py +++ b/vulnerabilities/tests/test_view.py @@ -23,6 +23,8 @@ from vulnerabilities.models import FixingPackageRelatedVulnerability from vulnerabilities.models import Package from vulnerabilities.models import PackageV2 +from vulnerabilities.models import PipelineRun +from vulnerabilities.models import PipelineSchedule from vulnerabilities.models import Vulnerability from vulnerabilities.models import VulnerabilitySeverity from vulnerabilities.templatetags.url_filters import url_quote_filter @@ -379,3 +381,25 @@ def test_throttle_after_15_requests(self): HTTP_USER_AGENT="test-agent", ) assert response.status_code == 429 + + +class PipelineRunViewsUnregisteredPipelineTestCase(TestCase): + def setUp(self): + self.client = Client() + session = self.client.session + session["altcha_verified_at"] = time.time() + session.save() + cache.clear() + self.schedule = PipelineSchedule.objects.create(pipeline_id="hunt_probe_no_such_pipeline") + self.run = PipelineRun.objects.create(pipeline=self.schedule) + + def test_pipeline_run_list_renders_for_unregistered_pipeline(self): + response = self.client.get(f"/pipelines/{self.schedule.pipeline_id}/runs/") + assert response.status_code == 200 + assert response.context["pipeline_name"] == self.schedule.pipeline_id + assert response.context["pipeline_description"] is None + + def test_pipeline_run_detail_renders_for_unregistered_pipeline(self): + response = self.client.get(f"/pipelines/{self.schedule.pipeline_id}/run/{self.run.run_id}/") + assert response.status_code == 200 + assert response.context["pipeline_name"] == self.schedule.pipeline_id diff --git a/vulnerabilities/views.py b/vulnerabilities/views.py index b5ba74214..61949c94c 100644 --- a/vulnerabilities/views.py +++ b/vulnerabilities/views.py @@ -1048,7 +1048,12 @@ def get_context_data(self, **kwargs): PipelineSchedule, pipeline_id=self.kwargs["pipeline_id"], ) - context["pipeline_name"] = pipeline.pipeline_class.__name__ + pipeline_class = pipeline.pipeline_class + # A schedule can outlive its pipeline (removed or renamed), so the + # class may be unknown; fall back to the raw pipeline id as the name. + context["pipeline_name"] = ( + pipeline_class.__name__ if pipeline_class else pipeline.pipeline_id + ) context["pipeline_description"] = pipeline.description context["pipeline_run_retention_days"] = VULNERABLECODE_PIPELINE_RUN_RETENTION_DAYS context["minimum_pipeline_runs_retain"] = VULNERABLECODE_MINIMUM_PIPELINE_RUNS_TO_RETAIN @@ -1078,7 +1083,12 @@ def get_context_data(self, **kwargs): pipeline__pipeline_id=pipeline_id, run_id=run_id, ) - context["pipeline_name"] = run.pipeline_class.__name__ + pipeline_class = run.pipeline_class + # A schedule can outlive its pipeline (removed or renamed), so the + # class may be unknown; fall back to the raw pipeline id as the name. + context["pipeline_name"] = ( + pipeline_class.__name__ if pipeline_class else run.pipeline.pipeline_id + ) return context