Skip to content

Commit 124af1f

Browse files
committed
own sarif class
Signed-off-by: Anas Nashif <[email protected]>
1 parent 3f1a15a commit 124af1f

File tree

1 file changed

+83
-36
lines changed

1 file changed

+83
-36
lines changed

scripts/ci/guideline_check.py

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import argparse
88
import re
99
from unidiff import PatchSet
10-
import simple_sarif
10+
import json
11+
import sys
12+
1113

1214
if "ZEPHYR_BASE" not in os.environ:
1315
exit("$ZEPHYR_BASE environment variable undefined.")
@@ -50,25 +52,83 @@ def parse_args():
5052
help="Genrate sarif file")
5153
return parser.parse_args()
5254

55+
class CocciToSarif:
56+
57+
def __init__(self, tags=[]):
58+
self.tags = tags
59+
self.results = []
60+
self.rules = {}
61+
62+
63+
def add_rule(self, rule_id, short_description, full_description,
64+
help_text, level, tags=[]):
65+
self.rules[rule_id] = {
66+
"id": rule_id,
67+
# This appears as the title on the list and individual issue view
68+
"shortDescription": {"text": short_description},
69+
# This appears as a sub heading on the individual issue view
70+
"fullDescription": {"text": full_description},
71+
# This appears on the individual issue view in an expandable box
72+
"help": {
73+
"markdown": full_description,
74+
# This property is not used if markdown is provided, but is required
75+
"text": "",
76+
},
77+
"defaultConfiguration": {"level": level},
78+
"properties": {"tags": tags},
79+
}
80+
81+
def add_result(self, rule_id, target_file, line, message):
82+
result = {
83+
"ruleId": rule_id,
84+
# This appears in the line by line highlight on the individual issue view
85+
"message": {"text": message},
86+
"locations": [
87+
{
88+
"physicalLocation": {
89+
"artifactLocation": {
90+
"uri": target_file
91+
},
92+
"region": {
93+
"startLine": line
94+
},
95+
}
96+
}
97+
],
98+
}
99+
self.results.append(result)
100+
101+
def get_sarif(self):
102+
output = {
103+
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
104+
"version": "2.1.0",
105+
"runs": [
106+
{
107+
"tool": {
108+
"driver": {
109+
"name": "Cocci",
110+
"rules": self.rules
111+
}
112+
},
113+
"results": self.results,
114+
}
115+
],
116+
}
117+
return output
118+
53119

54120
def main():
55121
args = parse_args()
56122

57123
if args.sarif:
58-
var = simple_sarif.Sarif(file=f"{args.sarif}", validate=True, recreate=True)
59-
var.add_rule(
60-
"MISRA 21.2",
61-
"zephyr.rule_21.1",
62-
"Should not used a reserved identifier",
63-
"Should not used a reserved identifier.",
64-
{
65-
"note": {
66-
"text": "Rule passed."
67-
},
68-
"error": {
69-
"text": "Rule failed."
70-
}
71-
}
124+
csarif = CocciToSarif()
125+
csarif.add_rule(
126+
rule_id="zephyr.rule_21.1",
127+
short_description="Should not used a reserved identifier",
128+
full_description="Should not used a reserved identifier",
129+
help_text="Should not used a reserved identifier",
130+
level="error",
131+
tags=["cocci", "zephyr"]
72132
)
73133

74134
if not args.commits:
@@ -130,26 +190,11 @@ def main():
130190
numViolations += 1
131191

132192
if args.sarif:
133-
print(
134-
"{}:{}".format(
135-
violation, "\t\n".join(
136-
violations[violation])))
137-
var.add_result(
138-
ruleId="zephyr.rule_21.1",
139-
message_id="Should not used a reserved identifier",
140-
arguments=[],
141-
locations=[{
142-
"physicalLocation": {
143-
"artifactLocation": {
144-
"uri": f.path
145-
},
146-
"region": {
147-
"startLine": line.target_line_no
148-
}
149-
}
150-
}],
151-
level="error"
152-
)
193+
csarif.add_result(
194+
rule_id="zephyr.rule_21.1",
195+
target_file=f.path,
196+
line=line.target_line_no,
197+
message="\t\n".join(violations[violation]))
153198
elif args.output:
154199
with open(args.output, "a+") as fp:
155200
fp.write("{}:{}\n".format(
@@ -162,7 +207,9 @@ def main():
162207
violations[violation])))
163208

164209
if args.sarif:
165-
var.save()
210+
csarif_output = csarif.get_sarif()
211+
with open(args.sarif, "w") as fp:
212+
json.dump(csarif_output, fp, indent=4)
166213
print("sarif file generated")
167214
return numViolations
168215

0 commit comments

Comments
 (0)