3838from tools .commands import EESSIBotCommand , EESSIBotCommandError , \
3939 contains_any_bot_command , get_bot_command
4040from tools .permissions import check_command_permission
41- from tools .pr_comments import create_comment
41+ from tools .pr_comments import ChatLevels , create_comment
4242
4343
4444REQUIRED_CONFIG = {
4545 config .SECTION_ARCHITECTURETARGETS : [
4646 config .ARCHITECTURETARGETS_SETTING_ARCH_TARGET_MAP ], # required
4747 config .SECTION_BOT_CONTROL : [
48+ # config.BOT_CONTROL_SETTING_CHATLEVEL, # optional
4849 config .BOT_CONTROL_SETTING_COMMAND_PERMISSION , # required
4950 config .BOT_CONTROL_SETTING_COMMAND_RESPONSE_FMT ], # required
5051 config .SECTION_BUILDENV : [
@@ -193,6 +194,8 @@ def handle_issue_comment_event(self, event_info, log_file=None):
193194 return
194195 # at this point we know that we are handling a new comment
195196
197+ issue_comment = None
198+
196199 # check if comment does not contain a bot command
197200 if not contains_any_bot_command (comment_received ):
198201 self .log ("comment does not contain a bot comment; not processing it further" )
@@ -229,7 +232,7 @@ def handle_issue_comment_event(self, event_info, log_file=None):
229232 comment_response = comment_response ,
230233 comment_result = ''
231234 )
232- issue_comment = create_comment (repo_name , pr_number , comment_body )
235+ issue_comment = create_comment (repo_name , pr_number , comment_body , ChatLevels . CHATTY )
233236 else :
234237 self .log (f"account `{ sender } ` seems to be a bot instance itself, hence not creating a new PR comment" )
235238 return
@@ -263,6 +266,11 @@ def handle_issue_comment_event(self, event_info, log_file=None):
263266 # including a bot command; the logging should only be done when log
264267 # level is set to debug
265268
269+ if 'help' in (x .command for x in commands ):
270+ req_chatlevel = ChatLevels .MINIMAL
271+ else :
272+ req_chatlevel = ChatLevels .CHATTY
273+
266274 if comment_response == '' :
267275 # no update to be added, just log and return
268276 self .log ("comment response is empty" )
@@ -281,7 +289,7 @@ def handle_issue_comment_event(self, event_info, log_file=None):
281289 comment_response = comment_response ,
282290 comment_result = ''
283291 )
284- issue_comment = create_comment (repo_name , pr_number , comment_body )
292+ issue_comment = create_comment (repo_name , pr_number , comment_body , req_chatlevel )
285293 else :
286294 self .log (f"update '{ comment_response } ' is considered to contain bot command ... not creating PR comment" )
287295 # TODO we may want to report this back to the PR on GitHub, e.g.,
@@ -306,24 +314,26 @@ def handle_issue_comment_event(self, event_info, log_file=None):
306314 continue
307315 except Exception as err :
308316 log (f"Unexpected err={ err } , type(err)={ type (err )} " )
309- if comment_result :
317+ if comment_result and issue_comment :
310318 comment_body = command_response_fmt .format (
311319 app_name = app_name ,
312320 comment_response = comment_response ,
313321 comment_result = comment_result
314322 )
315323 issue_comment .edit (comment_body )
316324 raise
317- # only update PR comment once, that is, a single call to
318- # issue_comment.edit is made in the entire function
319- comment_body = command_response_fmt .format (
320- app_name = app_name ,
321- comment_response = comment_response ,
322- comment_result = comment_result
323- )
324- issue_comment .edit (comment_body )
325325
326- self .log (f"issue_comment event (url { issue_url } ) handled!" )
326+ if issue_comment :
327+ # only update PR comment once, that is, a single call to
328+ # issue_comment.edit is made in the entire function
329+ comment_body = command_response_fmt .format (
330+ app_name = app_name ,
331+ comment_response = comment_response ,
332+ comment_result = comment_result
333+ )
334+ issue_comment .edit (comment_body )
335+
336+ self .log (f"issue_comment event (url { issue_url } ) handled!" )
327337
328338 def handle_installation_event (self , event_info , log_file = None ):
329339 """
@@ -373,14 +383,14 @@ def handle_pull_request_labeled_event(self, event_info, pr):
373383 comment_response = msg ,
374384 comment_result = ''
375385 )
376- create_comment (repo_name , pr_number , comment_body )
386+ create_comment (repo_name , pr_number , comment_body , ChatLevels . BASIC )
377387 elif label == "bot:deploy" :
378388 # run function to deploy built artefacts
379389 deploy_built_artefacts (pr , event_info )
380390 else :
381391 self .log ("handle_pull_request_labeled_event: no handler for label '%s'" , label )
382392
383- def handle_pull_request_opened_event (self , event_info , pr ):
393+ def handle_pull_request_opened_event (self , event_info , pr , req_chatlevel = ChatLevels . CHATTY ):
384394 """
385395 Handle events of type pull_request with the action opened. Main action
386396 is to report for which architectures and repositories a bot instance is
@@ -420,10 +430,7 @@ def handle_pull_request_opened_event(self, event_info, pr):
420430
421431 # create comment to pull request
422432 repo_name = pr .base .repo .full_name
423- gh = github .get_instance ()
424- repo = gh .get_repo (repo_name )
425- pull_request = repo .get_pull (pr .number )
426- issue_comment = pull_request .create_issue_comment (comment )
433+ issue_comment = create_comment (repo_name , pr .number , comment , req_chatlevel )
427434 return issue_comment
428435
429436 def handle_pull_request_event (self , event_info , log_file = None ):
@@ -554,8 +561,9 @@ def handle_bot_command_show_config(self, event_info, bot_command):
554561 repo_name = event_info ['raw_request_body' ]['repository' ]['full_name' ]
555562 pr_number = event_info ['raw_request_body' ]['issue' ]['number' ]
556563 pr = gh .get_repo (repo_name ).get_pull (pr_number )
557- issue_comment = self .handle_pull_request_opened_event (event_info , pr )
558- return f"\n - added comment { issue_comment .html_url } to show configuration"
564+ issue_comment = self .handle_pull_request_opened_event (event_info , pr , req_chatlevel = ChatLevels .MINIMAL )
565+ if issue_comment :
566+ return f"\n - added comment { issue_comment .html_url } to show configuration"
559567
560568 def handle_bot_command_status (self , event_info , bot_command ):
561569 """
@@ -571,7 +579,6 @@ def handle_bot_command_status(self, event_info, bot_command):
571579 PyGithub, not the github from the internal connections module)
572580 """
573581 self .log ("processing bot command 'status'" )
574- gh = github .get_instance ()
575582 repo_name = event_info ['raw_request_body' ]['repository' ]['full_name' ]
576583 pr_number = event_info ['raw_request_body' ]['issue' ]['number' ]
577584 status_table = request_bot_build_issue_comments (repo_name , pr_number )
@@ -588,9 +595,7 @@ def handle_bot_command_status(self, event_info, bot_command):
588595 comment_status += f"{ status_table ['url' ][x ]} |"
589596
590597 self .log (f"Overview of finished builds: comment '{ comment_status } '" )
591- repo = gh .get_repo (repo_name )
592- pull_request = repo .get_pull (pr_number )
593- issue_comment = pull_request .create_issue_comment (comment_status )
598+ issue_comment = create_comment (repo_name , pr_number , comment_status , ChatLevels .MINIMAL )
594599 return issue_comment
595600
596601 def start (self , app , port = 3000 ):
@@ -669,12 +674,9 @@ def handle_pull_request_closed_event(self, event_info, pr):
669674 # 4) report move to pull request
670675
671676 repo_name = pr .base .repo .full_name
672- gh = github .get_instance ()
673- repo = gh .get_repo (repo_name )
674- pull_request = repo .get_pull (pr .number )
675677 clean_up_comment = self .cfg [config .SECTION_CLEAN_UP ][config .CLEAN_UP_SETTING_MOVED_JOB_DIRS_COMMENT ]
676678 moved_comment = clean_up_comment .format (job_dirs = job_dirs , trash_bin_dir = trash_bin_dir )
677- issue_comment = pull_request . create_issue_comment ( moved_comment )
679+ issue_comment = create_comment ( repo_name , pr . number , moved_comment , ChatLevels . CHATTY )
678680 return issue_comment
679681
680682
0 commit comments