diff --git a/phabricator_tasks/tasks.py b/phabricator_tasks/tasks.py index 78ce2c7..c5603b1 100644 --- a/phabricator_tasks/tasks.py +++ b/phabricator_tasks/tasks.py @@ -85,102 +85,107 @@ def close_task(task_id, phab): except Exception as e: print(f'T{task_id} Error: {e}') - -parser = argparse.ArgumentParser() -parser.add_argument("-t", "--token", type=str, help="API token", required=True) -args = parser.parse_args() - -phab = Phabricator(host='https://vyos.dev/api/', token=args.token) -phab.maniphest.update(id=6053, status='resolved') - -workboards = phab_search(phab.project.column.search) -project_hirarchy = {} - -# get sub-project hirarchy from proxyPHID in workboards -for workboard in workboards: - if workboard['fields']['proxyPHID']: - proxy_phid = workboard['fields']['proxyPHID'] - project_phid = workboard['fields']['project']['phid'] - - if project_phid not in project_hirarchy.keys(): - project_hirarchy[project_phid] = [] - project_hirarchy[project_phid].append(proxy_phid) - -finished_boards = [] - - -for workboard in workboards: - project_id = workboard['fields']['project']['phid'] - if project_id in project_hirarchy.keys(): - # skip projects with sub-projects - continue - if workboard['fields']['name'] == 'Finished': - project_tasks = phab_search(phab.maniphest.search, constraints={ - 'projects': [project_id], - 'statuses': ['open'], - }) - finished_boards.append({ - 'project_id': project_id, - 'project_name': workboard['fields']['project']['name'], - 'project_tasks': project_tasks, - 'should_board_id': workboard['phid'], - }) - -# get unique tasks -# tasks = { -# 9999: { -# 'PHID-PROJ-xxxxx': 'PHID-PCOL-xxxxx', -# 'PHID-PROJ-yyyyy': 'PHID-PCOL-yyyyy' -# } -# } -tasks = {} -for project in finished_boards: - project_id = project['project_id'] - board_id = project['should_board_id'] - for task in project['project_tasks']: - task_id = task['id'] - if task_id not in tasks.keys(): - tasks[task_id] = {} - if project_id not in tasks[task_id].keys(): - tasks[task_id][project_id] = board_id - -tasks = dict(sorted(tasks.items())) - -# get transactions for each task and compare if the task is in the Finished column -for task_id, projects in tasks.items(): - fisnish_timestamp = 0 - project_ids = list(projects.keys()) - # don't use own pagination function, because endpoint without pagination - transactions = phab.maniphest.gettasktransactions(ids=[task_id]) - transactions = transactions.response[str(task_id)] - finished = False - for transaction in transactions: - if transaction['transactionType'] == 'core:columns': - # test if projectid is in transaction - if transaction['newValue'][0]['boardPHID'] in project_ids: - # remove project_id from project_ids to use only last transaction in this - # project - project_ids.remove(transaction['newValue'][0]['boardPHID']) - # test if boardid is the "Finished" board - if fisnish_timestamp < int(transaction['dateCreated']): - fisnish_timestamp = int(transaction['dateCreated']) - if projects[transaction['newValue'][0]['boardPHID']] == transaction['newValue'][0]['columnPHID']: - finished = True - for project in finished_boards: - if project['project_id'] == transaction['newValue'][0]['boardPHID']: - project_name = project['project_name'] - # print(f'T{task_id} is Finished in {project_name}') - if len(project_ids) == 0: - print(f'T{task_id} is Finished in all projects') - close_task(task_id, phab) - break - - #if len(project_ids) > 0 and finished: - # collect project names for output - # project_names = [] - # for project_id in project_ids: - # for project in finished_boards: - # if project['project_id'] == project_id: - # project_names.append(project['project_name']) - # print(f'T{task_id} is in a different column: {' and '.join(project_names)}') - \ No newline at end of file +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("-t", "--token", type=str, help="API token", required=True) + parser.add_argument("-d", "--dry", help="dry run", action="store_true", default=False) + args = parser.parse_args() + + if args.dry: + print("This is a dry run") + + phab = Phabricator(host='https://vyos.dev/api/', token=args.token) + + workboards = phab_search(phab.project.column.search) + project_hirarchy = {} + + # get sub-project hirarchy from proxyPHID in workboards + for workboard in workboards: + if workboard['fields']['proxyPHID']: + proxy_phid = workboard['fields']['proxyPHID'] + project_phid = workboard['fields']['project']['phid'] + + if project_phid not in project_hirarchy.keys(): + project_hirarchy[project_phid] = [] + project_hirarchy[project_phid].append(proxy_phid) + + finished_boards = [] + + + for workboard in workboards: + project_id = workboard['fields']['project']['phid'] + if project_id in project_hirarchy.keys(): + # skip projects with sub-projects + continue + if workboard['fields']['name'] == 'Finished': + project_tasks = phab_search(phab.maniphest.search, constraints={ + 'projects': [project_id], + 'statuses': ['open'], + }) + finished_boards.append({ + 'project_id': project_id, + 'project_name': workboard['fields']['project']['name'], + 'project_tasks': project_tasks, + 'should_board_id': workboard['phid'], + }) + + # get unique tasks + # tasks = { + # 9999: { + # 'PHID-PROJ-xxxxx': 'PHID-PCOL-xxxxx', + # 'PHID-PROJ-yyyyy': 'PHID-PCOL-yyyyy' + # } + # } + tasks = {} + for project in finished_boards: + project_id = project['project_id'] + board_id = project['should_board_id'] + for task in project['project_tasks']: + task_id = task['id'] + if task_id not in tasks.keys(): + tasks[task_id] = {} + if project_id not in tasks[task_id].keys(): + tasks[task_id][project_id] = board_id + + tasks = dict(sorted(tasks.items())) + + # get transactions for each task and compare if the task is in the Finished column + for task_id, projects in tasks.items(): + project_ids = list(projects.keys()) + # don't use own pagination function, because endpoint without pagination + transactions = phab.maniphest.gettasktransactions(ids=[task_id]) + transactions = transactions.response[str(task_id)] + + finished = {} + for p in project_ids: + finished[p] = False + for transaction in transactions: + if transaction['transactionType'] == 'core:columns': + # test if projectid is in transaction + if transaction['newValue'][0]['boardPHID'] in project_ids: + # remove project_id from project_ids to use only last transaction from this project + project_ids.remove(transaction['newValue'][0]['boardPHID']) + # test if boardid is the "Finished" board of project + if projects[transaction['newValue'][0]['boardPHID']] == transaction['newValue'][0]['columnPHID']: + finished[transaction['newValue'][0]['boardPHID']] = True + + # if all core:columns typy of each project_ids is handled. + # deside to close task or not + if len(project_ids) == 0: + if task_id == 6211: + pass + task_finish = True + for project_id, is_finished in finished.items(): + if not is_finished: + task_finish = False + if task_finish: + print(f'T{task_id} is Finished in all projects') + if not args.dry: + close_task(task_id, phab) + else: + print(f'T{task_id} would be closed') + break + + +if __name__ == '__main__': + main() \ No newline at end of file