1515)
1616from artifactory_cleanup .base_url_session import BaseUrlSession
1717from artifactory_cleanup .context_managers import get_context_managers
18- from artifactory_cleanup .errors import InvalidConfigError
18+ from artifactory_cleanup .errors import InvalidConfigError , NotificationError
1919from artifactory_cleanup .loaders import (
2020 PythonLoader ,
2121 YamlConfigLoader ,
2222)
23+ from artifactory_cleanup .notifiers import ReportNotifier , SlackConfig , TelegramConfig
2324
2425requests .packages .urllib3 .disable_warnings ()
2526
@@ -102,6 +103,44 @@ class ArtifactoryCleanupCLI(cli.Application):
102103 default = False ,
103104 )
104105
106+ _slack_token = cli .SwitchAttr (
107+ "--slack-token" ,
108+ help = "Slack bot token for report upload" ,
109+ mandatory = False ,
110+ envname = "ARTIFACTORY_CLEANUP_SLACK_TOKEN" ,
111+ requires = ["--slack-channel-id" , "--output" ],
112+ )
113+
114+ _slack_channel_id = cli .SwitchAttr (
115+ "--slack-channel-id" ,
116+ help = "Slack channel id for report upload" ,
117+ mandatory = False ,
118+ envname = "ARTIFACTORY_CLEANUP_SLACK_CHANNEL_ID" ,
119+ requires = ["--slack-token" , "--output" ],
120+ )
121+
122+ _telegram_token = cli .SwitchAttr (
123+ "--telegram-token" ,
124+ help = "Telegram bot token for report upload" ,
125+ mandatory = False ,
126+ envname = "ARTIFACTORY_CLEANUP_TELEGRAM_TOKEN" ,
127+ requires = ["--telegram-chat-id" , "--output" ],
128+ )
129+
130+ _telegram_chat_id = cli .SwitchAttr (
131+ "--telegram-chat-id" ,
132+ help = "Telegram chat id for report upload" ,
133+ mandatory = False ,
134+ envname = "ARTIFACTORY_CLEANUP_TELEGRAM_CHAT_ID" ,
135+ requires = ["--telegram-token" , "--output" ],
136+ )
137+
138+ _notify_comment = cli .SwitchAttr (
139+ "--notify-comment" ,
140+ help = "Comment attached to uploaded report" ,
141+ mandatory = False ,
142+ )
143+
105144 @property
106145 def VERSION (self ):
107146 # To prevent circular imports
@@ -153,6 +192,36 @@ def _create_output_file(self, result, filename, format):
153192 with open (filename , "w" , encoding = "utf-8" ) as file :
154193 file .write (text )
155194
195+ def _has_notifiers (self ) -> bool :
196+ return any (
197+ [
198+ self ._slack_token ,
199+ self ._slack_channel_id ,
200+ self ._telegram_token ,
201+ self ._telegram_chat_id ,
202+ ]
203+ )
204+
205+ def _build_notifier (self ) -> ReportNotifier :
206+ slack = None
207+ if self ._slack_token and self ._slack_channel_id :
208+ slack = SlackConfig (
209+ token = self ._slack_token , channel_id = self ._slack_channel_id
210+ )
211+ telegram = None
212+ if self ._telegram_token and self ._telegram_chat_id :
213+ telegram = TelegramConfig (
214+ token = self ._telegram_token , chat_id = self ._telegram_chat_id
215+ )
216+ return ReportNotifier (slack = slack , telegram = telegram )
217+
218+ def _apply_notifier_config (self , config ):
219+ self ._slack_token = self ._slack_token or config .get ("slack_token" , "" )
220+ self ._slack_channel_id = self ._slack_channel_id or config .get ("slack_channel_id" , "" )
221+ self ._telegram_token = self ._telegram_token or config .get ("telegram_token" , "" )
222+ self ._telegram_chat_id = self ._telegram_chat_id or config .get ("telegram_chat_id" , "" )
223+ self ._notify_comment = self ._notify_comment or config .get ("notify_comment" , "" )
224+
156225 def main (self ):
157226 today = self ._get_today ()
158227 if self ._load_rules :
@@ -166,6 +235,8 @@ def main(self):
166235 print (str (err ), file = sys .stderr )
167236 sys .exit (1 )
168237
238+ self ._apply_notifier_config (loader .get_notifications ())
239+
169240 server , user , password , apikey = loader .get_connection ()
170241 session = BaseUrlSession (server )
171242 if apikey :
@@ -218,6 +289,13 @@ def main(self):
218289
219290 if self ._output_file :
220291 self ._create_output_file (result , self ._output_file , self ._output_format )
292+ if self ._has_notifiers ():
293+ try :
294+ notifier = self ._build_notifier ()
295+ notifier .send_file (self ._output_file , comment = self ._notify_comment )
296+ except NotificationError as err :
297+ print (str (err ), file = sys .stderr )
298+ sys .exit (1 )
221299
222300
223301if __name__ == "__main__" :
0 commit comments