3
3
from sys import exit
4
4
from typing import Optional , cast
5
5
6
+
6
7
@click .group ()
7
8
def cli () -> None :
8
9
pass
9
10
11
+
10
12
@cli .group ()
11
13
def database () -> None :
12
14
pass
13
15
16
+
14
17
@database .command ("add-vcs-provenance" )
15
- @click .option ("-u" , "--repository-uri" , required = True , help = "An absolute URI that specifies the location of the repository." )
16
- @click .option ("-r" , "--revision-id" , required = True , help = "A string that uniquely and permanently identifies the revision." )
18
+ @click .option (
19
+ "-u" ,
20
+ "--repository-uri" ,
21
+ required = True ,
22
+ help = "An absolute URI that specifies the location of the repository." ,
23
+ )
24
+ @click .option (
25
+ "-r" ,
26
+ "--revision-id" ,
27
+ required = True ,
28
+ help = "A string that uniquely and permanently identifies the revision." ,
29
+ )
17
30
@click .argument ("database" , type = click .Path (exists = True , path_type = Path ), required = True )
18
- def database_add_provenance (repository_uri : str , revision_id : str , database : Path ) -> None :
31
+ def database_add_provenance (
32
+ repository_uri : str , revision_id : str , database : Path
33
+ ) -> None :
19
34
from codeql_postproc .helpers .codeql import CodeQLDatabase , InvalidCodeQLDatabase
20
35
21
36
try :
22
37
codeql_db = CodeQLDatabase (database )
23
- vcs_provenance = [{
24
- 'repositoryUri' : repository_uri ,
25
- 'revisionId' : revision_id
26
- }]
38
+ vcs_provenance = [{"repositoryUri" : repository_uri , "revisionId" : revision_id }]
27
39
codeql_db .set_property (versionControlProvenance = vcs_provenance )
28
40
except InvalidCodeQLDatabase as e :
29
41
click .echo (e , err = True )
30
42
exit (1 )
31
43
44
+
32
45
@database .command ("get-property" )
33
- @click .option ("-f" , "--format" , "output_format" , type = click .Choice (["json" , "yaml" ]), default = "json" )
46
+ @click .option (
47
+ "-f" ,
48
+ "--format" ,
49
+ "output_format" ,
50
+ type = click .Choice (["json" , "yaml" ]),
51
+ default = "json" ,
52
+ )
34
53
@click .argument ("key" , required = True )
35
54
@click .argument ("database" , type = click .Path (exists = True , path_type = Path ), required = True )
36
55
def get_property (output_format : str , key : str , database : Path ) -> None :
@@ -42,57 +61,99 @@ def get_property(output_format: str, key: str, database: Path) -> None:
42
61
try :
43
62
codeql_db = CodeQLDatabase (database )
44
63
value = codeql_db .get_property (key )
45
- if output_format == "yaml" :
46
- yaml .dump (value , sys .stdout )
47
- elif output_format == "json" :
48
- json .dump (value , sys .stdout )
64
+ if value :
65
+ if output_format == "yaml" :
66
+ yaml .dump (value , sys .stdout )
67
+ elif output_format == "json" :
68
+ json .dump (value , sys .stdout )
69
+ else :
70
+ click .echo (f"Unimplemented output format { output_format } !" )
71
+ exit (1 )
49
72
else :
50
- click .echo (f"Unimplemented output format { output_format } !" )
73
+ click .echo (
74
+ f"The database does not have a property with key { key } ." , err = True
75
+ )
51
76
exit (1 )
52
- except KeyError :
53
- click .echo (f"The database does not have a property with key { key } ." , err = True )
54
- exit (1 )
55
77
except InvalidCodeQLDatabase as e :
56
78
click .echo (e , err = True )
57
79
exit (1 )
58
80
81
+
59
82
@cli .group ("sarif" )
60
83
def sarif () -> None :
61
84
pass
62
85
86
+
63
87
@sarif .command ("add-vcs-provenance" )
64
88
@click .option ("-d" , "--from-database" , is_flag = True )
65
- @click .option ("-u" , "--repository-uri" , help = "An absolute URI that specifies the location of the repository." )
66
- @click .option ("-r" , "--revision-id" , help = "A string that uniquely and permanently identifies the revision." )
67
- @click .argument ("sarif_path" , type = click .Path (exists = True , path_type = Path , dir_okay = False ), required = True )
68
- @click .argument ("database_path" , type = click .Path (exists = True , path_type = Path ), required = False )
69
- def sarif_add_provenance (from_database : bool , repository_uri : str , revision_id : str , sarif_path : Path , database_path : Optional [Path ]) -> None :
89
+ @click .option (
90
+ "-u" ,
91
+ "--repository-uri" ,
92
+ help = "An absolute URI that specifies the location of the repository." ,
93
+ )
94
+ @click .option (
95
+ "-r" ,
96
+ "--revision-id" ,
97
+ help = "A string that uniquely and permanently identifies the revision." ,
98
+ )
99
+ @click .argument (
100
+ "sarif_path" ,
101
+ type = click .Path (exists = True , path_type = Path , dir_okay = False ),
102
+ required = True ,
103
+ )
104
+ @click .argument (
105
+ "database_path" , type = click .Path (exists = True , path_type = Path ), required = False
106
+ )
107
+ def sarif_add_provenance (
108
+ from_database : bool ,
109
+ repository_uri : str ,
110
+ revision_id : str ,
111
+ sarif_path : Path ,
112
+ database_path : Optional [Path ],
113
+ ) -> None :
70
114
from codeql_postproc .helpers .codeql import CodeQLDatabase , InvalidCodeQLDatabase
71
115
from codeql_postproc .helpers .sarif import Sarif , InvalidSarif
72
116
73
117
if from_database and not database_path :
74
- raise click .BadArgumentUsage ("A database must be specified when using the --from-database option!" )
75
-
118
+ raise click .BadArgumentUsage (
119
+ "A database must be specified when using the --from-database option!"
120
+ )
121
+
76
122
if not from_database and not repository_uri :
77
- raise click .BadOptionUsage ("--repository-uri" , "The option '--repository-uri' must be specified if not importing from a database!" )
123
+ raise click .BadOptionUsage (
124
+ "--repository-uri" ,
125
+ "The option '--repository-uri' must be specified if not importing from a database!" ,
126
+ )
78
127
if not from_database and not revision_id :
79
- raise click .BadOptionUsage ("--revision-id" , "The option '--revision-id' must be specified if not importing from a database!" )
128
+ raise click .BadOptionUsage (
129
+ "--revision-id" ,
130
+ "The option '--revision-id' must be specified if not importing from a database!" ,
131
+ )
80
132
81
133
if from_database :
82
134
try :
83
135
codeql_db = CodeQLDatabase (cast (Path , database_path ))
84
136
# Assume there is only one array element for now.
85
137
vcp = codeql_db .get_property ("versionControlProvenance" )[0 ]
86
138
if not "repositoryUri" in vcp :
87
- click .echo (f"The database's version control provenance misses the 'repositoryUri' property!" , err = True )
139
+ click .echo (
140
+ f"The database's version control provenance misses the 'repositoryUri' property!" ,
141
+ err = True ,
142
+ )
88
143
exit (1 )
89
144
repository_uri = vcp ["repositoryUri" ]
90
145
if not "revisionId" in vcp :
91
- click .echo (f"The database's version control provenance misses the 'revisionId' property!" , err = True )
146
+ click .echo (
147
+ f"The database's version control provenance misses the 'revisionId' property!" ,
148
+ err = True ,
149
+ )
92
150
exit (1 )
93
151
revision_id = vcp ["revisionId" ]
94
152
except KeyError :
95
- click .echo (f"The database does not have any version control provenance property." , err = True )
153
+ click .echo (
154
+ f"The database does not have any version control provenance property." ,
155
+ err = True ,
156
+ )
96
157
exit (1 )
97
158
except InvalidCodeQLDatabase as e :
98
159
click .echo (e , err = True )
@@ -105,7 +166,6 @@ def sarif_add_provenance(from_database: bool, repository_uri: str, revision_id:
105
166
click .echo (f"Unable to process invalid Sarif file with reason: { e } " )
106
167
exit (1 )
107
168
108
-
109
-
169
+
110
170
if __name__ == "__main__" :
111
- cli ()
171
+ cli ()
0 commit comments