Pipelines: a preview endpoint that draws and checks a graph while it is being built

This commit is contained in:
claude committed 2026-10-13 18:58:35 +02:00
1 parent 2ed44ca053
commit faf719ca31
3 files changed
+115

No files matched your search

+36
View File
@@ -556,6 +556,42 @@ def pipeline_check(request):
return JsonResponse({ 'issues' : _check(graph) })
MAX_PREVIEW_EDGES = 400
def _drawable(graph):
""" Whether a graph is sound enough in shape to be laid out. Whatever else is
wrong with it, the editor still wants to see it, so that it can be fixed """
steps, edges = graph.get('steps'), graph.get('edges', [])
return (
isinstance(steps, dict) and 0 < len(steps) <= graphs.LIMIT_STEPS
and all(isinstance(step, dict) for step in steps.values())
and isinstance(edges, list) and len(edges) <= MAX_PREVIEW_EDGES
and all(isinstance(edge, dict) for edge in edges)
)
@require_POST
def pipeline_preview(request):
""" The flow of a graph as it is drawn on a run, and its checks, for the
editor to show while a pipeline is being built. Saves nothing """
if viewer(request) is None:
return JsonResponse({ 'error' : 'Not allowed' }, status=403)
graph = _parse_graph(request.POST.get('graph'))
if graph is None:
return JsonResponse({ 'svg' : '', 'issues' : [graphs.issue('error', 'graph.invalid', 'The pipeline could not be read')] })
svg = ''
if _drawable(graph):
try: svg = drawing.render_svg(graph)
except Exception: svg = '' # A half built graph is not a reason for a 500
return JsonResponse({ 'svg' : svg, 'issues' : _named(_check(graph), graph) })
def pipeline_test_fields(request, test_id):
""" The settings of an existing SPRT or Games test, as the settings of a step """
+78
View File
@@ -24,6 +24,7 @@
# The Pipeline pages, with a real ( test ) database and the real URL routing. As
# in tests_pipelines.py, only the call out to Github is replaced.
import json
import re
from django.contrib.auth.models import User
@@ -493,3 +494,80 @@ class ChipTests(PagesTestCase):
page = self.client_for(self.user).get(url).content.decode()
self.assertNotIn('<script>alert(1)', page)
class PreviewTests(PagesTestCase):
""" The flow and the checks of a pipeline that is still being built, drawn by the server """
def drawing(self, edges=None, name='Alpha'):
return {
'version' : 1, 'start' : 'a', 'edges' : edges or [],
'steps' : {
'a' : { 'name' : name, 'kind' : 'TEST', 'payload' : {} },
'b' : { 'name' : 'Beta', 'kind' : 'TEST', 'payload' : {} },
},
}
def preview(self, graph, user=None, **kwargs):
raw = graph if isinstance(graph, str) else json.dumps(graph)
return self.client_for(user, **kwargs).post('/pipelines/preview/', { 'graph' : raw })
def test_a_graph_is_drawn_with_something_to_click_on(self):
data = self.preview(self.drawing([{ 'from' : 'a', 'on' : 'passed', 'to' : 'b' }]), self.user).json()
self.assertIn('class="pl-graph"', data['svg'])
self.assertIn('data-step="a"', data['svg'])
self.assertIn('data-step="b"', data['svg'])
self.assertIn('data-edge="0"', data['svg'])
def test_a_graph_that_is_still_being_built_is_drawn_and_its_problems_are_named(self):
data = self.preview(self.drawing(), self.user).json()
missing = [issue for issue in data['issues'] if issue['code'] == 'payload.missing']
self.assertIn('data-step="a"', data['svg'])
self.assertEqual(sorted(issue['name'] for issue in missing), ['Alpha', 'Beta'])
def test_nothing_is_saved(self):
before = Pipeline.objects.count()
self.preview(self.drawing(), self.user)
self.assertEqual(Pipeline.objects.count(), before)
def test_only_enabled_accounts_may_ask(self):
for user, status in ((None, 403), (self.dave, 403), (self.bob, 200)):
self.assertEqual(self.preview(self.drawing(), user).status_code, status)
def test_it_needs_the_csrf_token_and_a_post(self):
self.assertEqual(self.preview(self.drawing(), self.user, enforce_csrf_checks=True).status_code, 403)
self.assertEqual(self.client_for(self.user).get('/pipelines/preview/').status_code, 405)
def test_garbage_is_reported_and_is_not_a_crash(self):
for raw in ('nope', '[]', '{"steps": {"x": "not a step"}}',
'{"version": 1, "steps": {"x": {"kind": "TEST", "payload": []}}, "edges": [5]}'):
response = self.preview(raw, self.user)
data = response.json()
self.assertEqual(response.status_code, 200, raw)
self.assertEqual(data['svg'], '', raw)
self.assertTrue(any(issue['level'] == 'error' for issue in data['issues']), raw)
def test_a_graph_too_big_to_be_real_is_not_drawn(self):
steps = { 's%d' % i : { 'name' : 'S', 'kind' : 'TEST', 'payload' : {} } for i in range(31) }
data = self.preview({ 'version' : 1, 'start' : 's0', 'steps' : steps, 'edges' : [] }, self.user).json()
self.assertEqual(data['svg'], '')
def test_names_cannot_inject_markup(self):
data = self.preview(self.drawing(name='<script>alert(1)</script>'), self.user).json()
self.assertIn('class="pl-graph"', data['svg'])
self.assertNotIn('<script>', data['svg'])
+1
View File
@@ -51,6 +51,7 @@ urlpatterns = [
django.urls.path(r'pipelines/<int:id>/edit/', OpenBench.pipeline_views.pipeline_edit),
django.urls.path(r'pipelines/new/', OpenBench.pipeline_views.pipeline_new),
django.urls.path(r'pipelines/check/', OpenBench.pipeline_views.pipeline_check),
django.urls.path(r'pipelines/preview/', OpenBench.pipeline_views.pipeline_preview),
django.urls.path(r'pipelines/test-fields/<int:test_id>/', OpenBench.pipeline_views.pipeline_test_fields),
django.urls.path(r'pipelines/run/<int:run_id>/', OpenBench.pipeline_views.pipeline_run),
django.urls.path(r'pipelines/run/<int:run_id>/cancel/', OpenBench.pipeline_views.pipeline_run_cancel),