Skip to content

Commit

Permalink
Handle the exception when the policies.yml is not valid #1560 (#1561)
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez authored Jan 23, 2025
1 parent a7f1de9 commit ad6f2ef
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion scanpipe/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def load_policies_yaml(policies_yaml):
try:
return saneyaml.load(policies_yaml)
except saneyaml.YAMLError as e:
raise ValidationError(f"Policies format error: {e}")
raise ValidationError(f"Policies file format error: {e}")


def load_policies_file(policies_file, validate=True):
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/templates/scanpipe/project_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<div hx-get="{% url 'project_resource_license_summary' project.slug %}" hx-trigger="load" hx-swap="outerHTML"></div>
</div>

{% if project.policies_enabled %}
{% if policies_enabled %}
<div class="columns">
<div hx-get="{% url 'project_compliance_panel' project.slug %}" hx-trigger="load" hx-swap="outerHTML"></div>
</div>
Expand Down
1 change: 1 addition & 0 deletions scanpipe/tests/data/policies/broken_policies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[broken
10 changes: 10 additions & 0 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,3 +1286,13 @@ def test_scanpipe_views_project_dependency_tree(self):
response = self.client.get(url)
self.assertTrue(response.context["recursion_error"])
self.assertContains(response, "The dependency tree cannot be rendered")

def test_scanpipe_policies_broken_policies_project_details(self):
broken_policies = self.data / "policies" / "broken_policies.yml"
project1 = make_project()
shutil.copyfile(broken_policies, project1.input_path / "policies.yml")

url = project1.get_absolute_url()
response = self.client.get(url)
self.assertEqual(200, response.status_code)
self.assertContains(response, "Policies file format error")
7 changes: 7 additions & 0 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,12 @@ def get_context_data(self, **kwargs):
pipeline_runs = project.runs.all()
self.check_run_scancode_version(pipeline_runs)

policies_enabled = False
try:
policies_enabled = project.policies_enabled
except ValidationError as e:
messages.error(self.request, str(e))

context.update(
{
"input_sources": project.get_inputs_with_source(),
Expand All @@ -763,6 +769,7 @@ def get_context_data(self, **kwargs):
"pipeline_runs": pipeline_runs,
"codebase_root": codebase_root,
"file_filter": self.request.GET.get("file-filter", "all"),
"policies_enabled": policies_enabled,
}
)

Expand Down

0 comments on commit ad6f2ef

Please sign in to comment.