Skip to content

Commit

Permalink
Get the task unassigning workflow to work correctly
Browse files Browse the repository at this point in the history
and give the code a clean-up
(more clean-up is coming)
  • Loading branch information
dmbaturin committed Jul 2, 2024
1 parent 4a42e07 commit 5cd9d55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
38 changes: 17 additions & 21 deletions phabricator_tasks/get_task_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ class Phabricator(PhabricatorOriginal):
def __init__(self, **kwargs):
kwargs['interface'] = copy.deepcopy(parse_interfaces(INTERFACES))
super(Phabricator, self).__init__(self, **kwargs)

''' end of extend the original Phabricator class'''


def phab_api(token):
return Phabricator(host='https://vyos.dev/api/', token=token)
Expand Down Expand Up @@ -94,26 +93,29 @@ def get_project_default_column(project_id, workboards):
return workboard['phid'], workboard['fields']['name']
return None, None


def close_task(task_id, token):
phab = phab_api(token)
try:
response = phab.maniphest.update(
id=task_id,
status='resolved'
response = phab.maniphest.edit(
objectIdentifier=task_id,
transactions=[{'type': 'status', 'value': 'resolved'}]
)
if response.response['isClosed']:
print(f'T{task_id} closed')
except Exception as e:
print(f'T{task_id} Error: {e}')


def unassign_task(task_id, token):
phab = phab_api(token)
raise NotImplementedError
try:
response = phab.maniphest.edit(
objectIdentifier=task_id,
transactions=[{'type': 'owner', 'value': None}]
)
except Exception as e:
print(f'T{task_id} Error: {e}')

def get_task_data(token):

phab = phab_api(token)
# get list with all open status namens
open_status_list = phab.maniphest.querystatuses().response
Expand All @@ -124,15 +126,15 @@ def get_task_data(token):
tasks = phab_search(phab.maniphest.search, constraints={
'statuses': open_status_list
})

# get all projects to translate id to name
projects_raw = phab_search(phab.project.search)
projects = {}
for p in projects_raw:
projects[p['phid']] = p['fields']['name']

workboards = phab_search(phab.project.column.search)

# get sub-project hirarchy from proxyPHID in workboards
project_hirarchy = {}
for workboard in workboards:
Expand All @@ -148,10 +150,11 @@ def get_task_data(token):
for task in tasks:
task_data = {
'task_id': task['id'],
'task_phid': task['phid'],
'task_name': task['fields']['name'],
'task_status': task['fields']['status']['value'],
'assigned_user': task['fields']['ownerPHID'],
'assigned_time': None,
'last_modified': task['fields']['dateModified'],
'projects': []
}
if task['fields']['status']['value'] in open_status_list:
Expand All @@ -160,13 +163,6 @@ def get_task_data(token):
task_data['task_open'] = False
transactions = phab.maniphest.gettasktransactions(ids=[task['id']])

# transactionType: reassign to get assigened time
if task_data['assigned_user']:
for transaction in transactions[str(task['id'])]:
if transaction['transactionType'] == 'reassign' and transaction['newValue'] == task['fields']['ownerPHID']:
task_data['assigned_time'] = transaction['dateCreated']
break

# transactionType: core:edge
# loop reversed from oldest to newest transaction
# core:edge transactionType is used if the task is moved to another project but stay in default column
Expand All @@ -181,7 +177,7 @@ def get_task_data(token):
for newValue in transaction['newValue']:
if "PHID-PROJ" in newValue:
task_projects.append(newValue)

# transactionType: core:columns
# use task_projects items as search indicator 'boardPHID' == project_id
# remove project from task_projects if the task is moved from the default column to another column
Expand All @@ -196,7 +192,7 @@ def get_task_data(token):
'project_id': transaction['newValue'][0]['boardPHID'],
})


# handle remaining projects and set the project base default column
for project in task_projects:
default_columnid, default_columnname = get_project_default_column(project, workboards)
Expand Down
40 changes: 19 additions & 21 deletions phabricator_tasks/tasks.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'''
implement Tasks checks and workflows on vyos.dev
1. Close a tasks if the Task is in all "Finished" columns
Phorge task chores:
1. Close a tasks if it's "Finished" in all boards but not yet resolved
2. Unassign tasks that are nominally assigned to someone but had no activity in a long time
'''

Expand All @@ -20,7 +20,7 @@

TOKEN = args.token
DRYRUN = args.dry
# DRYRUN = True

UNASSIGN_AFTER_DAYS = 90
UNASSIGN_AFTER_DAYS = timedelta(days=UNASSIGN_AFTER_DAYS)
NOW = datetime.now()
Expand All @@ -31,30 +31,28 @@
tasks = get_task_data(TOKEN)

for task in tasks:
# close tasks it is in any projects "finished" column
# Close tasks that are in the "Finished" column in all projects
# but aren't marked resolved yet
if len(task['projects']) > 0:
finished = True
for project in task['projects']:
if project['column_name'] != 'Finished':
finished = False
break
if finished:
print(f'Closing task T{task["task_id"]} (finished in all boards)')
if DRYRUN:
print(f'dryrun: T{task["task_id"]} would be closed')
pass
else:
close_task(task['task_id'], TOKEN)
continue


'''
# unassign tasks with no process after UNASSIGN_AFTER_DAYS
if task['assigned_user'] and task['assigned_time']:
delta = NOW - datetime.fromtimestamp(int(task['assigned_time']))
close_task(task['task_phid'], TOKEN)

# Unassign tasks that supposed assignees aren't actively working on in a long time
if task['assigned_user']:
delta = NOW - datetime.fromtimestamp(int(task['last_modified']))
if delta > UNASSIGN_AFTER_DAYS:
if task['task_status'] != 'open':
if DRYRUN:
print(f'dryrun: T{task["task_id"]} with status {task['task_status']} would be unassigned after {delta.days} days')
else:
unassign_task(task['task_id'], TOKEN)
continue
'''
print(f'Unassigning task T{task["task_id"]} after {delta.days} days of inactivity')
if DRYRUN:
pass
else:
unassign_task(task['task_id'], TOKEN)

0 comments on commit 5cd9d55

Please sign in to comment.