1+ #! /usr/bin/env python3
2+
3+ # The purpose of this file is to adapt the output from
4+ # Clang's static analyzer into a format suitable for GitHub
5+ # Actions. The problem is that Clang outputs a separate "run"
6+ # per file in its SARIF output, but GitHub requires a single
7+ # run per tool (Clang is wrong here).
8+
9+ import sys
10+ import json
11+
12+ if len (sys .argv ) < 2 :
13+ print ("Usage: munge-sarif.py INPUT" , file = sys .stderr )
14+ sys .exit (1 )
15+
16+ data = None
17+ with open (sys .argv [1 ], 'rb' ) as f :
18+ data = json .load (f )
19+
20+ # Arbitrarily pick the first run as the one from which to copy all the properties
21+ base_run = data ['runs' ][0 ]
22+
23+ # We don't need these, GitHub ignores them
24+ base_run ['artifacts' ] = []
25+
26+ # Concatenate results
27+ for r in data ['runs' ][1 :]:
28+ base_run ['results' ].extend (r ['results' ])
29+
30+ data ['runs' ] = [base_run ]
31+
32+ def fix_region (region ):
33+ startLine = region .get ('startLine' , None )
34+ startColumn = region .get ('startColumn' , 1 )
35+ endLine = region .get ('endLine' , None )
36+ endColumn = region .get ('endColumn' , None )
37+ if startLine is None :
38+ raise ValueError ("Region must have startLine" )
39+ if endLine is not None and endLine < startLine :
40+ region ['endLine' ] = startLine
41+ del region ['endColumn' ]
42+ endLine = startLine
43+ endColumn = None
44+ if endColumn is not None and (endLine == startLine or endLine is None ) and endColumn < startColumn :
45+ region ['endColumn' ] = startColumn
46+ endColumn = startColumn
47+
48+ # Recursively scan the data dictionary, and apply the fix_region() function
49+ # to all "region":Region key-value pairs.
50+ def fix_regions (data ):
51+ if isinstance (data , dict ):
52+ if 'region' in data :
53+ fix_region (data ['region' ])
54+ for key , value in data .items ():
55+ fix_regions (value )
56+ elif isinstance (data , list ):
57+ for item in data :
58+ fix_regions (item )
59+
60+ fix_regions (data )
61+
62+ with open (sys .argv [1 ], 'w' ) as f :
63+ json .dump (data , f , indent = 2 )
0 commit comments