7
7
from abc import abstractmethod
8
8
9
9
from macaron .slsa_analyzer .analyze_context import AnalyzeContext
10
- from macaron .slsa_analyzer .checks .check_result import CheckResult , CheckResultType , SkippedInfo , get_result_as_bool
11
- from macaron .slsa_analyzer .slsa_req import ReqName , get_requirements_dict
10
+ from macaron .slsa_analyzer .checks .check_result import (
11
+ CheckInfo ,
12
+ CheckResult ,
13
+ CheckResultData ,
14
+ CheckResultType ,
15
+ SkippedInfo ,
16
+ get_result_as_bool ,
17
+ )
18
+ from macaron .slsa_analyzer .slsa_req import ReqName
12
19
13
20
logger : logging .Logger = logging .getLogger (__name__ )
14
21
15
22
16
23
class BaseCheck :
17
24
"""This abstract class is used to implement Checks in Macaron."""
18
25
19
- # The dictionary that contains the data of all SLSA requirements.
20
- SLSA_REQ_DATA = get_requirements_dict ()
21
-
22
26
def __init__ (
23
27
self ,
24
28
check_id : str = "" ,
@@ -44,20 +48,34 @@ def __init__(
44
48
result_on_skip : CheckResultType
45
49
The status for this check when it's skipped based on another check's result.
46
50
"""
47
- self .check_id = check_id
48
- self .description = description
51
+ self ._check_info = CheckInfo (
52
+ check_id = check_id , check_description = description , eval_reqs = eval_reqs if eval_reqs else []
53
+ )
49
54
50
55
if not depends_on :
51
- self .depends_on = []
56
+ self ._depends_on = []
52
57
else :
53
- self .depends_on = depends_on
58
+ self ._depends_on = depends_on
54
59
55
- if not eval_reqs :
56
- self .eval_reqs = []
57
- else :
58
- self .eval_reqs = eval_reqs
60
+ self ._result_on_skip = result_on_skip
61
+
62
+ @property
63
+ def check_info (self ) -> CheckInfo :
64
+ """Get the information identifying/describing this check."""
65
+ return self ._check_info
66
+
67
+ @property
68
+ def depends_on (self ) -> list [tuple [str , CheckResultType ]]:
69
+ """Get the list of parent checks that this check depends on.
59
70
60
- self .result_on_skip = result_on_skip
71
+ Each member of the list is a tuple of the parent's id and the status of that parent check.
72
+ """
73
+ return self ._depends_on
74
+
75
+ @property
76
+ def result_on_skip (self ) -> CheckResultType :
77
+ """Get the status for this check when it's skipped based on another check's result."""
78
+ return self ._result_on_skip
61
79
62
80
def run (self , target : AnalyzeContext , skipped_info : SkippedInfo | None = None ) -> CheckResult :
63
81
"""Run the check and return the results.
@@ -75,66 +93,58 @@ def run(self, target: AnalyzeContext, skipped_info: SkippedInfo | None = None) -
75
93
The result of the check.
76
94
"""
77
95
logger .info ("----------------------------------" )
78
- logger .info ("BEGIN CHECK: %s" , self .check_id )
96
+ logger .info ("BEGIN CHECK: %s" , self .check_info . check_id )
79
97
logger .info ("----------------------------------" )
80
98
81
- check_result = CheckResult (
82
- check_id = self .check_id ,
83
- check_description = self .description ,
84
- slsa_requirements = [str (self .SLSA_REQ_DATA .get (req )) for req in self .eval_reqs ],
85
- justification = [],
86
- result_type = CheckResultType .SKIPPED ,
87
- result_tables = [],
88
- )
99
+ check_result_data : CheckResultData
89
100
90
101
if skipped_info :
91
- check_result ["result_type" ] = self .result_on_skip
92
- check_result ["justification" ].append (skipped_info ["suppress_comment" ])
102
+ check_result_data = CheckResultData (
103
+ justification = [skipped_info ["suppress_comment" ]], result_tables = [], result_type = self .result_on_skip
104
+ )
93
105
logger .info (
94
106
"Check %s is skipped on target %s, comment: %s" ,
95
- self .check_id ,
107
+ self .check_info . check_id ,
96
108
target .component .purl ,
97
109
skipped_info ["suppress_comment" ],
98
110
)
99
111
else :
100
- check_result [ "result_type" ] = self .run_check (target , check_result )
112
+ check_result_data = self .run_check (target )
101
113
logger .info (
102
114
"Check %s run %s on target %s, result: %s" ,
103
- self .check_id ,
104
- check_result [ " result_type" ] .value ,
115
+ self .check_info . check_id ,
116
+ check_result_data . result_type .value ,
105
117
target .component .purl ,
106
- check_result [ " justification" ] ,
118
+ check_result_data . justification ,
107
119
)
108
120
109
121
justification_str = ""
110
- for ele in check_result [ " justification" ] :
122
+ for ele in check_result_data . justification :
111
123
if isinstance (ele , dict ):
112
124
for key , val in ele .items ():
113
125
justification_str += f"{ key } : { val } . "
114
126
justification_str += f"{ str (ele )} . "
115
127
116
128
target .bulk_update_req_status (
117
- self .eval_reqs ,
118
- get_result_as_bool (check_result [ " result_type" ] ),
129
+ self .check_info . eval_reqs ,
130
+ get_result_as_bool (check_result_data . result_type ),
119
131
justification_str ,
120
132
)
121
133
122
- return check_result
134
+ return CheckResult ( check = self . check_info , result = check_result_data )
123
135
124
136
@abstractmethod
125
- def run_check (self , ctx : AnalyzeContext , check_result : CheckResult ) -> CheckResultType :
137
+ def run_check (self , ctx : AnalyzeContext ) -> CheckResultData :
126
138
"""Implement the check in this method.
127
139
128
140
Parameters
129
141
----------
130
142
ctx : AnalyzeContext
131
143
The object containing processed data for the target repo.
132
- check_result : CheckResult
133
- The object containing result data of a check.
134
144
135
145
Returns
136
146
-------
137
- CheckResultType
138
- The result type of the check (e.g. PASSED) .
147
+ CheckResultData
148
+ The result of the check.
139
149
"""
140
150
raise NotImplementedError
0 commit comments