Skip to content

Commit

Permalink
add bot: status command
Browse files Browse the repository at this point in the history
  • Loading branch information
vsc46128 vscuser committed Jan 25, 2024
1 parent e883264 commit f4441ac
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
30 changes: 30 additions & 0 deletions eessi_bot_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions tasks/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<len(keys)-1})

# add date, status, url to status_table if
for row in ret:
if row['job status'] == 'finished':
status_table['date'].append(row['date'])
status_table['status'].append(row['job status'])
status_table['url'].append(comment['html_url'])
if 'FAILURE' in row['comment']:
status_table['result'].append(':cry: FAILURE')
elif 'SUCCES' in row['comment']:
status_table['result'].append(':grin: SUCCESS')
elif 'UNKNOWN' in row['comment']:
status_table['result'].append(':shrug: UNKNOWN')
else:
status_table['result'].append(row['comment'])
if len(comments) != 100:
break
return status_table

0 comments on commit f4441ac

Please sign in to comment.