Skip to content

Commit ad6f2ef

Browse files
authored
Handle the exception when the policies.yml is not valid #1560 (#1561)
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent a7f1de9 commit ad6f2ef

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

scanpipe/policies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def load_policies_yaml(policies_yaml):
3030
try:
3131
return saneyaml.load(policies_yaml)
3232
except saneyaml.YAMLError as e:
33-
raise ValidationError(f"Policies format error: {e}")
33+
raise ValidationError(f"Policies file format error: {e}")
3434

3535

3636
def load_policies_file(policies_file, validate=True):

scanpipe/templates/scanpipe/project_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
<div hx-get="{% url 'project_resource_license_summary' project.slug %}" hx-trigger="load" hx-swap="outerHTML"></div>
108108
</div>
109109

110-
{% if project.policies_enabled %}
110+
{% if policies_enabled %}
111111
<div class="columns">
112112
<div hx-get="{% url 'project_compliance_panel' project.slug %}" hx-trigger="load" hx-swap="outerHTML"></div>
113113
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[broken

scanpipe/tests/test_views.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,3 +1286,13 @@ def test_scanpipe_views_project_dependency_tree(self):
12861286
response = self.client.get(url)
12871287
self.assertTrue(response.context["recursion_error"])
12881288
self.assertContains(response, "The dependency tree cannot be rendered")
1289+
1290+
def test_scanpipe_policies_broken_policies_project_details(self):
1291+
broken_policies = self.data / "policies" / "broken_policies.yml"
1292+
project1 = make_project()
1293+
shutil.copyfile(broken_policies, project1.input_path / "policies.yml")
1294+
1295+
url = project1.get_absolute_url()
1296+
response = self.client.get(url)
1297+
self.assertEqual(200, response.status_code)
1298+
self.assertContains(response, "Policies file format error")

scanpipe/views.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,12 @@ def get_context_data(self, **kwargs):
747747
pipeline_runs = project.runs.all()
748748
self.check_run_scancode_version(pipeline_runs)
749749

750+
policies_enabled = False
751+
try:
752+
policies_enabled = project.policies_enabled
753+
except ValidationError as e:
754+
messages.error(self.request, str(e))
755+
750756
context.update(
751757
{
752758
"input_sources": project.get_inputs_with_source(),
@@ -763,6 +769,7 @@ def get_context_data(self, **kwargs):
763769
"pipeline_runs": pipeline_runs,
764770
"codebase_root": codebase_root,
765771
"file_filter": self.request.GET.get("file-filter", "all"),
772+
"policies_enabled": policies_enabled,
766773
}
767774
)
768775

0 commit comments

Comments
 (0)