diff --git a/eessi_bot_event_handler.py b/eessi_bot_event_handler.py index e750f5e5..c265e5fd 100644 --- a/eessi_bot_event_handler.py +++ b/eessi_bot_event_handler.py @@ -476,6 +476,36 @@ def handle_bot_command_show_config(self, event_info, bot_command): issue_comment = self.handle_pull_request_opened_event(event_info, pr) return f"\n - added comment {issue_comment.html_url} to show configuration" + def handle_bot_command_status(self, event_info, bot_command): + """ + Handles bot command 'status' by running the handler for events of + type pull_request with the action opened. + + Args: + event_info (dict): event received by event_handler + bot_command (EESSIBotCommand): command to be handled + + Returns: + (string): table which collects the status of each target + """ + self.log("processing bot command 'status'") + gh = github.get_instance() + repo_name = event_info['raw_request_body']['repository']['full_name'] + pr_number = event_info['raw_request_body']['issue']['number'] + status_table = request_bot_build_issue_comments(repo_name, pr_number) + + comment = f"This is the status of all the `bot: build` commands:" + comment += f"\n|arch|result|date|status|url|" + comment += f"\n|----|------|----|------|---|" + for x in range(0,len(status_table['date'])): + comment += f"\n|{status_table['arch'][x]}|{status_table['result'][x]}|{status_table['date'][x]}|{status_table['status'][x]}|{status_table['url'][x]}|" + + self.log(f"PR opened: comment '{comment}'") + repo = gh.get_repo(repo_name) + pull_request = repo.get_pull(pr_number) + issue_comment = pull_request.create_issue_comment(comment) + return issue_comment + def start(self, app, port=3000): """ Logs startup information to shell and log file and starts the app using diff --git a/tasks/build.py b/tasks/build.py index e3f0bc8c..3a1374dd 100644 --- a/tasks/build.py +++ b/tasks/build.py @@ -760,3 +760,59 @@ def check_build_permission(pr, event_info): else: log(f"{fn}(): GH account '{build_labeler}' is authorized to build") return True + +def request_bot_build_issue_comments(repo_name, pr_number): + status_table = {'arch': [], 'date': [], 'status': [], 'url': [], 'result': []} + cfg = config.read_config() + + # for loop because github has max 100 items per request. + # if the pr has more than 100 comments we need to use per_page + # argument at the moment the for loop is for a max of 400 comments could bump this up + for x in range(1,5): + curl_cmd = f'curl -L https://api.github.com/repos/{repo_name}/issues/{pr_number}/comments?per_page=100&page={x}' + curl_output, curl_error, curl_exit_code = run_cmd(curl_cmd, "fetch all comments") + + comments = json.loads(curl_output) + + for comment in comments: #iterate through the comments to find the one where the status of the build was in + if config.read_config()["submitted_job_comments"]['initial_comment'][:20] in comment['body']: + + #get archictecture from comment['body'] + first_line = comment['body'][:comment['body'].find('/n')] + arch_map = get_architecture_targets(cfg) + for arch in arch_map.keys(): + target_arch = '/'.join(arch.split('/')[1:]) + if target_arch in first_line: + status_table['arch'].append(target_arch) + + #get date, status, url and result from the markdown table + comment_table = comment['body'][comment['body'].find('|'):comment['body'].rfind('|')+1] + + # Convert markdown table to a dictionary + lines = comment_table.split('\n') + ret = [] + keys = [] + for i,l in enumerate(lines): + if i==0: + keys=[_i.strip() for _i in l.split('|')] + elif i==1: continue + else: + ret.append({keys[_i]:v.strip() for _i,v in enumerate(l.split('|')) if _i>0 and _i