sink_task.py (2396B)
1 # mypy: allow-untyped-defs 2 3 import argparse 4 import logging 5 import os 6 7 import taskcluster 8 9 from .github_checks_output import get_gh_checks_outputter 10 11 12 logging.basicConfig() 13 logger = logging.getLogger() 14 15 16 def check_task_statuses(task_ids, github_checks_outputter): 17 """Verifies whether a set of Taskcluster tasks completed successfully or not. 18 19 Returns 0 if all tasks passed completed successfully, 1 otherwise.""" 20 21 queue = taskcluster.Queue({'rootUrl': os.environ['TASKCLUSTER_ROOT_URL']}) 22 failed_tasks = [] 23 for task in task_ids: 24 status = queue.status(task) 25 state = status['status']['state'] 26 if state == 'failed' or state == 'exception': 27 logger.error(f'Task {task} failed with state "{state}"') 28 failed_tasks.append(status) 29 elif state != 'completed': 30 logger.error(f'Task {task} had unexpected state "{state}"') 31 failed_tasks.append(status) 32 33 if failed_tasks and github_checks_outputter: 34 github_checks_outputter.output('Failed tasks:') 35 for task in failed_tasks: 36 # We need to make an additional call to get the task name. 37 task_id = task['status']['taskId'] 38 task_name = queue.task(task_id)['metadata']['name'] 39 github_checks_outputter.output('* `{}` failed with status `{}`'.format(task_name, task['status']['state'])) 40 else: 41 logger.info('All tasks completed successfully') 42 if github_checks_outputter: 43 github_checks_outputter.output('All tasks completed successfully') 44 return 1 if failed_tasks else 0 45 46 47 def get_parser(): 48 parser = argparse.ArgumentParser() 49 parser.add_argument("--github-checks-text-file", type=str, 50 help="Path to GitHub checks output file for Taskcluster runs") 51 parser.add_argument("tasks", nargs="+", 52 help="A set of Taskcluster task ids to verify the state of.") 53 return parser 54 55 56 def run(venv, **kwargs): 57 github_checks_outputter = get_gh_checks_outputter(kwargs["github_checks_text_file"]) 58 59 if github_checks_outputter: 60 github_checks_outputter.output( 61 "This check acts as a 'sink' for all other Taskcluster-based checks. " 62 "A failure here means that some other check has failed, which is the " 63 "real blocker.\n" 64 ) 65 return check_task_statuses(kwargs['tasks'], github_checks_outputter)