2626from datetime import datetime
2727from typing import Any , Dict , List , Optional , TypedDict
2828
29- from ...services .dependency_track_service import DependencyTrackService
30- from ..policy_check import PolicyCheck , PolicyStatus
31- from ..utils . markdown_utils import generate_jira_table , generate_table
29+ from .... services .dependency_track_service import DependencyTrackService
30+ from ... utils . markdown_utils import generate_jira_table , generate_table
31+ from ..policy_check import PolicyCheck , PolicyOutput , PolicyStatus
3232
3333# Constants
3434PROCESSING_RETRY_DELAY = 5 # seconds
@@ -171,7 +171,7 @@ def __init__( # noqa: PLR0913
171171 self .url = url .strip ().rstrip ('/' ) if url else None
172172 self .dep_track_service = DependencyTrackService (self .api_key , self .url , debug = debug , trace = trace , quiet = quiet )
173173
174- def _json (self , project_violations : list [PolicyViolationDict ]) -> Dict [ str , Any ] :
174+ def _json (self , project_violations : list [PolicyViolationDict ]) -> PolicyOutput :
175175 """
176176 Format project violations as JSON.
177177
@@ -181,12 +181,12 @@ def _json(self, project_violations: list[PolicyViolationDict]) -> Dict[str, Any]
181181 Returns:
182182 Dictionary containing JSON formatted results and summary
183183 """
184- return {
185- " details" : json .dumps (project_violations , indent = 2 ),
186- " summary" : f'{ len (project_violations )} policy violations were found.\n ' ,
187- }
184+ return PolicyOutput (
185+ details = json .dumps (project_violations , indent = 2 ),
186+ summary = f'{ len (project_violations )} policy violations were found.\n ' ,
187+ )
188188
189- def _markdown (self , project_violations : list [PolicyViolationDict ]) -> Dict [ str , Any ] :
189+ def _markdown (self , project_violations : list [PolicyViolationDict ]) -> PolicyOutput :
190190 """
191191 Format Dependency Track violations to Markdown format.
192192
@@ -198,7 +198,7 @@ def _markdown(self, project_violations: list[PolicyViolationDict]) -> Dict[str,
198198 """
199199 return self ._md_summary_generator (project_violations , generate_table )
200200
201- def _jira_markdown (self , data : list [PolicyViolationDict ]) -> Dict [ str , Any ] :
201+ def _jira_markdown (self , data : list [PolicyViolationDict ]) -> PolicyOutput :
202202 """
203203 Format project violations for Jira Markdown.
204204
@@ -357,8 +357,7 @@ def _set_project_id(self) -> None:
357357 self .print_stderr (f'Error: Failed to get project uuid from: { dt_project } ' )
358358 raise ValueError (f'Error: Project { self .project_name } @{ self .project_version } does not have a valid UUID' )
359359
360- @staticmethod
361- def _sort_project_violations (violations : List [PolicyViolationDict ]) -> List [PolicyViolationDict ]:
360+ def _sort_project_violations (self ,violations : List [PolicyViolationDict ]) -> List [PolicyViolationDict ]:
362361 """
363362 Sort project violations by priority.
364363
@@ -377,7 +376,7 @@ def _sort_project_violations(violations: List[PolicyViolationDict]) -> List[Poli
377376 key = lambda x : - type_priority .get (x .get ('type' , 'OTHER' ), 1 )
378377 )
379378
380- def _md_summary_generator (self , project_violations : list [PolicyViolationDict ], table_generator ):
379+ def _md_summary_generator (self , project_violations : list [PolicyViolationDict ], table_generator ) -> PolicyOutput :
381380 """
382381 Generates a Markdown summary of project policy violations.
383382
@@ -396,10 +395,10 @@ def _md_summary_generator(self, project_violations: list[PolicyViolationDict], t
396395 """
397396 if project_violations is None :
398397 self .print_stderr ('Warning: No project violations found. Returning empty results.' )
399- return {
400- " details" : "h3. Dependency Track Project Violations\n \n No policy violations found.\n " ,
401- " summary" : "0 policy violations were found.\n " ,
402- }
398+ return PolicyOutput (
399+ details = "h3. Dependency Track Project Violations\n \n No policy violations found.\n " ,
400+ summary = "0 policy violations were found.\n " ,
401+ )
403402 headers = ['State' , 'Risk Type' , 'Policy Name' , 'Component' , 'Date' ]
404403 c_cols = [0 , 1 ]
405404 rows : List [List [str ]] = []
@@ -424,11 +423,11 @@ def _md_summary_generator(self, project_violations: list[PolicyViolationDict], t
424423 ]
425424 rows .append (row )
426425 # End for loop
427- return {
428- " details" : f'### Dependency Track Project Violations\n { table_generator (headers , rows , c_cols )} \n \n '
426+ return PolicyOutput (
427+ details = f'### Dependency Track Project Violations\n { table_generator (headers , rows , c_cols )} \n \n '
429428 f'View project in Dependency Track [here]({ self .url } /projects/{ self .project_id } ).\n ' ,
430- " summary" : f'{ len (project_violations )} policy violations were found.\n '
431- }
429+ summary = f'{ len (project_violations )} policy violations were found.\n '
430+ )
432431
433432 def run (self ) -> int :
434433 """
@@ -470,10 +469,11 @@ def run(self) -> int:
470469 self .print_stderr ('Error: Invalid format specified.' )
471470 return PolicyStatus .ERROR .value
472471 # Format and output data - handle empty results gracefully
473- data = formatter (self ._sort_project_violations (dt_project_violations ))
474- self .print_to_file_or_stdout (data [ ' details' ] , self .output )
475- self .print_to_file_or_stderr (data [ ' summary' ] , self .status )
472+ policy_output = formatter (self ._sort_project_violations (dt_project_violations ))
473+ self .print_to_file_or_stdout (policy_output . details , self .output )
474+ self .print_to_file_or_stderr (policy_output . summary , self .status )
476475 # Return appropriate status based on violation count
477476 if len (dt_project_violations ) > 0 :
478477 return PolicyStatus .POLICY_FAIL .value
479478 return PolicyStatus .POLICY_SUCCESS .value
479+
0 commit comments