-
Notifications
You must be signed in to change notification settings - Fork 0
/
asana_parse.py
132 lines (116 loc) · 4.92 KB
/
asana_parse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from datetime import datetime
from typing import List, TypedDict
import asana
from asana.resources.projects import Projects
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--trello-key", dest="trello_key", help="Trello Oauth API Key", required=True)
parser.add_argument("--trello-secret", dest="trello_secret", help="Trello Oauth API Token/Secret", required=True)
parser.add_argument("--asana-pat", dest="asana_pat", help="Asana API Personal Access Token", required=True)
parser.add_argument("--board", dest="board", help="Board name that you want to move from Asana to Trello", required=True)
args = parser.parse_args()
KEY = args.trello_key
TOKEN = args.trello_secret
PAT = args.asana_pat
BOARD = args.board
class Subtask:
ATTR_LIST = ["name", "comments", "subtasks", "done", "description", "parent", "column", "is_separator"]
name: str
comments: List[str]
done: bool
description: str
parent: str
column: str
is_separator: bool
def __str__(self):
string = ['{}="{}"'.format(i, getattr(self, i)) for i in self.ATTR_LIST]
return '"' + ", ".join(string) + '"'
def __init__(self, **kwargs):
for i in self.ATTR_LIST:
if i in kwargs:
setattr(self, i, kwargs[i])
def to_json(self):
dic = {}
for attr in self.ATTR_LIST:
if getattr(self, attr):
if attr == "subtasks":
dic[attr] = [i.to_json() for i in getattr(self, attr)]
continue
dic[attr] = getattr(self, attr)
return dic
def from_json(self, json: dict[str, any]):
for key, value in json.items():
if key not in self.ATTR_LIST:
raise ValueError(f"Key {key} not found in Attribute List")
if key == "subtasks":
self.subtasks = [Subtask().from_json(i) for i in value]
continue
setattr(self, key, value)
return self
class MetaTask(Subtask):
subtasks: List[Subtask]
def __str__(self):
string = ['{}="{}"'.format(i, getattr(self, i)) for i in self.ATTR_LIST]
return '"' + ", ".join(string) + '"'
class Task(TypedDict):
gid: str
assignee: dict
assignee_status: str
completed: bool
completed_at: datetime
created_at: str
due_at: datetime
due_on: datetime
followers: list
hearted: bool
hearts: list
is_rendered_as_separator: bool
liked: bool
likes: list
memberships: list
modified_at: str
name: str
notes: str
num_hearts: int
num_likes: int
parent: dict
permalink_url: str
projects: list
resource_type: str
start_at: datetime
start_on: datetime
subtasks: list
tags: list
resource_subtype: str
workspace: dict
def get_metatasks():
client = asana.Client.access_token(PAT)
me = client.users.me()
workspace_id = me['workspaces'][0]['gid']
projects = client.projects.get_projects_for_workspace(workspace_id)
board_id = [i['gid'] for i in projects if i['name'] == BOARD][0]
fields = ["is_rendered_as_separator", "subtasks", "resource_subtype", "name", "memberships", "memberships.section", "memberships.section.name", "parent", "parent.name", "completed", "notes",
"subtasks.is_rendered_as_separator", "subtasks.subtasks", "subtasks.resource_subtype", "subtasks.name", "subtasks.memberships", "subtasks.memberships.section",
"subtasks.memberships.section.name", "subtasks.parent", "subtasks.parent.name" "subtasks.completed", "subtasks.notes"
]
tasks: list[Task] = client.tasks.get_tasks(params={'project': board_id}, opt_fields=fields)
def create_metatask_from_asana(task: Task):
#print(task)
metatask = MetaTask()
metatask.name = task['name']
metatask.description = task['notes'] if task.get('notes') else ""
metatask.done = task['completed']
metatask.column = [i['section']['name'] for i in task['memberships']][0] if task.get('memberships') else []
metatask.parent = task['parent']['name'] if task.get('parent') else task['memberships'][0]['section']['name']
# Comments are called stories in Asana
stories = client.stories.get_stories_for_task(task['gid'])
metatask.comments = [story['text'] for story in stories if 'comment' in story['resource_subtype']]
metatask.is_separator = task["is_rendered_as_separator"] if 'is_rendered_as_separator' in task else False
metatask.subtasks = [create_metatask_from_asana(client.tasks.get_task(i['gid'], opt_fields=fields)) for i in client.tasks.get_subtasks_for_task(task['gid'])]
return metatask
meta_list: List[MetaTask] = [create_metatask_from_asana(i) for i in tasks]
#meta_list: List[MetaTask] = [create_metatask_from_asana(client.tasks.get_task(i['gid'])) for i in tasks]
return meta_list
if __name__ == "__main__":
data = get_metatasks()
print([str(i) for i in data])