1
+ import logging
1
2
import sys
2
3
import json
3
- from typing import Any
4
+ from typing import Any , IO
4
5
import pipenv .vendor .click as click
5
6
6
7
from dataclasses import dataclass
7
8
9
+ from pipenv .patched .safety .constants import CONTEXT_COMMAND_TYPE
10
+
8
11
from . import github
9
12
from pipenv .patched .safety .util import SafetyPolicyFile
13
+ from pipenv .patched .safety .scan .constants import CLI_ALERT_COMMAND_HELP
14
+
15
+ LOG = logging .getLogger (__name__ )
16
+
17
+
18
+ def get_safety_cli_legacy_group ():
19
+ from pipenv .patched .safety .cli_util import SafetyCLILegacyGroup
20
+ return SafetyCLILegacyGroup
21
+
22
+ def get_context_settings ():
23
+ from pipenv .patched .safety .cli_util import CommandType
24
+ return {CONTEXT_COMMAND_TYPE : CommandType .UTILITY }
10
25
11
26
@dataclass
12
27
class Alert :
28
+ """
29
+ Data class for storing alert details.
30
+
31
+ Attributes:
32
+ report (Any): The report data.
33
+ key (str): The API key for the safetycli.com vulnerability database.
34
+ policy (Any): The policy data.
35
+ requirements_files (Any): The requirements files data.
36
+ """
13
37
report : Any
14
38
key : str
15
39
policy : Any = None
16
40
requirements_files : Any = None
17
41
18
- @click .group (help = "Send alerts based on the results of a Safety scan." )
19
- @click .option ('--check-report' , help = 'JSON output of Safety Check to work with.' , type = click .File ('r' ), default = sys .stdin )
20
- @click .option ("--policy-file" , type = SafetyPolicyFile (), default = '.safety-policy.yml' ,
21
- help = "Define the policy file to be used" )
42
+ @click .group (cls = get_safety_cli_legacy_group (), help = CLI_ALERT_COMMAND_HELP ,
43
+ deprecated = True , context_settings = get_context_settings ())
44
+ @click .option ('--check-report' , help = 'JSON output of Safety Check to work with.' , type = click .File ('r' ), default = sys .stdin , required = True )
22
45
@click .option ("--key" , envvar = "SAFETY_API_KEY" ,
23
- help = "API Key for pyup.io 's vulnerability database. Can be set as SAFETY_API_KEY "
46
+ help = "API Key for safetycli.com 's vulnerability database. Can be set as SAFETY_API_KEY "
24
47
"environment variable." , required = True )
48
+ @click .option ("--policy-file" , type = SafetyPolicyFile (), default = '.safety-policy.yml' ,
49
+ help = "Define the policy file to be used" )
25
50
@click .pass_context
26
- def alert (ctx , check_report , policy_file , key ):
51
+ def alert (ctx : click .Context , check_report : IO [str ], policy_file : SafetyPolicyFile , key : str ) -> None :
52
+ """
53
+ Command for processing the Safety Check JSON report.
54
+
55
+ Args:
56
+ ctx (click.Context): The Click context object.
57
+ check_report (IO[str]): The file containing the JSON report.
58
+ policy_file (SafetyPolicyFile): The policy file to be used.
59
+ key (str): The API key for the safetycli.com vulnerability database.
60
+ """
61
+ LOG .info ('alert started' )
62
+ LOG .info (f'check_report is using stdin: { check_report == sys .stdin } ' )
63
+
27
64
with check_report :
28
65
# TODO: This breaks --help for subcommands
29
66
try :
30
67
safety_report = json .load (check_report )
31
68
except json .decoder .JSONDecodeError as e :
69
+ LOG .info ('Error in the JSON report.' )
32
70
click .secho ("Error decoding input JSON: {}" .format (e .msg ), fg = 'red' )
33
71
sys .exit (1 )
34
72
@@ -38,5 +76,6 @@ def alert(ctx, check_report, policy_file, key):
38
76
39
77
ctx .obj = Alert (report = safety_report , policy = policy_file if policy_file else {}, key = key )
40
78
79
+ # Adding subcommands for GitHub integration
41
80
alert .add_command (github .github_pr )
42
81
alert .add_command (github .github_issue )
0 commit comments