2023-11-27 12:59:57 +05:00
|
|
|
|
import csv
|
2023-11-21 15:54:48 +05:00
|
|
|
|
import json
|
2023-11-23 00:11:49 +05:00
|
|
|
|
import os
|
2023-11-23 21:44:55 +05:00
|
|
|
|
import sys
|
2023-12-07 17:53:18 +05:00
|
|
|
|
import time
|
2023-11-21 15:54:48 +05:00
|
|
|
|
|
|
|
|
|
import requests
|
2023-11-24 13:33:17 +05:00
|
|
|
|
|
2023-11-21 15:54:48 +05:00
|
|
|
|
|
2023-11-30 12:38:52 +05:00
|
|
|
|
from logging_config import setup_logging
|
2023-12-07 17:53:18 +05:00
|
|
|
|
from urllib.parse import urlencode
|
2023-11-30 12:38:52 +05:00
|
|
|
|
|
2023-12-01 12:06:03 +05:00
|
|
|
|
logger = setup_logging(__name__)
|
2023-11-27 21:22:57 +05:00
|
|
|
|
|
2023-11-23 00:11:49 +05:00
|
|
|
|
base_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
asana_data_json = os.path.join(base_dir, './data/data_asana.json')
|
2023-11-27 12:59:57 +05:00
|
|
|
|
users_data = os.path.join(base_dir, './data/users.mapping.asana2ya.csv')
|
2023-11-21 15:54:48 +05:00
|
|
|
|
|
2023-12-01 12:06:03 +05:00
|
|
|
|
sys.stdin.reconfigure(encoding='utf-8')
|
2023-11-23 21:44:55 +05:00
|
|
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
2023-12-01 12:06:03 +05:00
|
|
|
|
|
2023-12-04 11:23:30 +05:00
|
|
|
|
QUEUE_NAME = 'TESTIMPORT'
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
2023-12-08 17:55:38 +05:00
|
|
|
|
headers = {
|
|
|
|
|
'Host': 'api.tracker.yandex.net',
|
|
|
|
|
'Authorization': 'OAuth y0_AgAEA7qkB3KjAArkXwAAAADzMlP9oR1lwMzBS2e94jHzdnII8Laxi7k',
|
|
|
|
|
'X-Org-ID': '7095769',
|
|
|
|
|
'Content-Type': 'appication/json',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
2023-11-27 17:18:51 +05:00
|
|
|
|
def get_assignee_data():
|
2023-11-27 12:59:57 +05:00
|
|
|
|
""" Получение данных для поля assignee """
|
|
|
|
|
|
|
|
|
|
mapping = {}
|
|
|
|
|
|
|
|
|
|
with open(users_data, 'r', encoding="utf8") as csv_file:
|
|
|
|
|
reader = csv.DictReader(csv_file)
|
|
|
|
|
for row in reader:
|
2023-11-27 17:18:51 +05:00
|
|
|
|
asana_id = row['ID']
|
|
|
|
|
ya_login = row['ya_login']
|
2023-11-27 12:59:57 +05:00
|
|
|
|
mapping[asana_id] = ya_login
|
|
|
|
|
return mapping
|
|
|
|
|
|
2023-11-30 20:47:05 +05:00
|
|
|
|
|
2023-11-27 17:18:51 +05:00
|
|
|
|
assignee = get_assignee_data()
|
2023-11-27 12:59:57 +05:00
|
|
|
|
|
|
|
|
|
|
2023-11-29 14:08:36 +05:00
|
|
|
|
def get_task_status(task):
|
|
|
|
|
""" Получение данных о статусе задачи """
|
|
|
|
|
|
|
|
|
|
if task.get('completed') is True:
|
2023-12-12 16:14:44 +05:00
|
|
|
|
status = 'closed'
|
2023-11-29 14:08:36 +05:00
|
|
|
|
completed_at = task.get('completed_at', '')
|
|
|
|
|
else:
|
|
|
|
|
status = 'open'
|
|
|
|
|
completed_at = ''
|
|
|
|
|
return status, completed_at
|
|
|
|
|
|
2023-12-05 18:34:12 +05:00
|
|
|
|
|
2023-12-05 17:04:11 +05:00
|
|
|
|
# здесь функция для дальнейшей работы с parent
|
2023-12-05 18:34:12 +05:00
|
|
|
|
def get_parent_task_ids(task, ya_imported_task_response):
|
2023-12-05 17:04:11 +05:00
|
|
|
|
""" Получение соответствий между идентификаторами подзадач в Асане и Яндекс Трекере """
|
|
|
|
|
|
|
|
|
|
parent_task_ids = {}
|
|
|
|
|
|
2023-12-06 16:08:36 +05:00
|
|
|
|
if task.get('parent') and 'gid' in task['parent']:
|
2023-12-05 18:34:12 +05:00
|
|
|
|
parent_gid = task['parent']['gid']
|
|
|
|
|
if parent_gid in ya_imported_task_response:
|
|
|
|
|
parent_id = ya_imported_task_response[parent_gid]
|
|
|
|
|
parent_task_ids[task['gid']] = parent_id
|
2023-12-05 17:04:11 +05:00
|
|
|
|
|
|
|
|
|
return parent_task_ids
|
|
|
|
|
|
2023-11-29 14:08:36 +05:00
|
|
|
|
|
2023-11-22 11:51:22 +05:00
|
|
|
|
def transform_data(data):
|
2023-11-22 12:13:13 +05:00
|
|
|
|
""" Преобразование данных из Asana в ЯндексТрекер в совместимом формате """
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
|
|
|
|
transformed_data = []
|
2023-12-08 17:55:38 +05:00
|
|
|
|
for task in data:
|
2023-11-29 14:08:36 +05:00
|
|
|
|
status, completed_at = get_task_status(task)
|
2023-11-22 11:51:22 +05:00
|
|
|
|
transformed_task = {
|
2023-12-08 15:04:51 +05:00
|
|
|
|
'gid':task['gid'],
|
2023-11-22 11:51:22 +05:00
|
|
|
|
'summary': task['name'],
|
|
|
|
|
'description': task['notes'],
|
|
|
|
|
'createdAt': task['created_at'],
|
|
|
|
|
'deadline': task['due_on'],
|
2023-11-29 10:31:23 +05:00
|
|
|
|
'assignee': task['assignee'],
|
2023-11-29 14:08:36 +05:00
|
|
|
|
'status': status,
|
|
|
|
|
'completedAt': completed_at,
|
2023-12-08 17:55:38 +05:00
|
|
|
|
'parent': task['parent'],
|
|
|
|
|
'subtasks' : transform_data(task['subtasks'])
|
2023-12-07 17:53:18 +05:00
|
|
|
|
# 'parent': task['parent'] # это я добавила, чтоб parent добавить
|
2023-11-22 11:51:22 +05:00
|
|
|
|
}
|
|
|
|
|
transformed_data.append(transformed_task)
|
|
|
|
|
return transformed_data
|
|
|
|
|
|
2023-12-08 17:55:38 +05:00
|
|
|
|
def create_task(task):
|
|
|
|
|
base_url = 'https://api.tracker.yandex.net/v2/issues/_import'
|
|
|
|
|
print(task)
|
|
|
|
|
if task.get('assignee') and 'gid' in task['assignee']:
|
|
|
|
|
ya_assignee = assignee.get(task['assignee']['gid'], 'dr.cyrill')
|
|
|
|
|
else:
|
|
|
|
|
ya_assignee = 'dr.cyrill'
|
|
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
|
'queue': QUEUE_NAME,
|
|
|
|
|
'summary': task['summary'],
|
|
|
|
|
'description': task['description'],
|
|
|
|
|
'createdAt': task['createdAt'],
|
|
|
|
|
'createdBy': 'dr.cyrill',
|
|
|
|
|
'deadline': task['deadline'],
|
|
|
|
|
'assignee': ya_assignee,
|
|
|
|
|
'status': task['status'],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.debug('Request: %s', json.dumps(payload))
|
|
|
|
|
|
|
|
|
|
response = requests.post(
|
|
|
|
|
base_url,
|
|
|
|
|
headers=headers,
|
|
|
|
|
data=json.dumps(payload),
|
|
|
|
|
params=urlencode({'parent': task['parent']}) # добавила параметр для parent в url-запрос
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if response.status_code == 201:
|
|
|
|
|
print('Задача успешно создана в ЯндексТрекер')
|
|
|
|
|
|
|
|
|
|
# здесь добавлено про родительскую задачу
|
|
|
|
|
return response.json()['id']
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
print(
|
|
|
|
|
'Ошибка при создании задачи в ЯндексТрекер:',
|
|
|
|
|
response.content,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger.debug('Response: %s', response.content)
|
|
|
|
|
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
2023-12-07 16:44:29 +05:00
|
|
|
|
def create_tasks_in_tracker(data, limit=10):
|
2023-11-22 12:13:13 +05:00
|
|
|
|
""" Создание задач в ЯндексТрекер """
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
2023-12-08 17:55:38 +05:00
|
|
|
|
|
2023-12-08 15:04:51 +05:00
|
|
|
|
datalen = (len(data) if limit==-1 else min(limit, len(data)))
|
2023-12-01 12:06:03 +05:00
|
|
|
|
logger.info('Import started. Task count: %d', datalen)
|
2023-12-05 17:04:11 +05:00
|
|
|
|
|
|
|
|
|
# этот датасет тоже для parent добавлен
|
|
|
|
|
ya_imported_task_response = {}
|
|
|
|
|
|
2023-12-07 16:44:29 +05:00
|
|
|
|
for i in range(datalen):
|
|
|
|
|
task = data[i]
|
2023-12-08 17:55:38 +05:00
|
|
|
|
task_id = create_task(task)
|
|
|
|
|
logger.info('Created task id = %s',task_id)
|
|
|
|
|
for subtask in task['subtasks']:
|
|
|
|
|
logger.info('Found subtask gid = %s for id = %s',subtask['gid'], task_id)
|
|
|
|
|
subtask_id = create_task(subtask)
|
|
|
|
|
assign_parent_task(subtask_id, task_id)
|
|
|
|
|
|
|
|
|
|
|
2023-12-05 17:04:11 +05:00
|
|
|
|
# вот здесь тоже про родительскую задачу добавлено
|
2023-12-08 17:55:38 +05:00
|
|
|
|
#parent_task_ids = get_parent_task_ids(task, ya_imported_task_response)
|
2023-12-05 17:04:11 +05:00
|
|
|
|
|
2023-12-08 17:55:38 +05:00
|
|
|
|
#for gid, parent_id in parent_task_ids.items():
|
|
|
|
|
# assign_parent_task(ya_imported_task_response[gid], parent_id)
|
2023-12-05 17:04:11 +05:00
|
|
|
|
|
2023-12-05 18:26:18 +05:00
|
|
|
|
|
2023-12-05 17:04:11 +05:00
|
|
|
|
#и эта функция для родительской задачи написана
|
2023-12-07 17:53:18 +05:00
|
|
|
|
def assign_parent_task(task_id, parent_id):
|
2023-12-05 17:04:11 +05:00
|
|
|
|
""" Назначение родительской задачи """
|
|
|
|
|
|
2023-12-07 17:53:18 +05:00
|
|
|
|
base_url = f'https://api.tracker.yandex.net/v2/issues/{task_id}'
|
2023-12-05 17:04:11 +05:00
|
|
|
|
headers = {
|
|
|
|
|
'Host': 'api.tracker.yandex.net',
|
|
|
|
|
'Authorization': 'OAuth y0_AgAEA7qkB3KjAArkXwAAAADzMlP9oR1lwMzBS2e94jHzdnII8Laxi7k',
|
|
|
|
|
'X-Org-ID': '7095769',
|
|
|
|
|
'Content-Type': 'application/json',
|
2023-11-30 20:47:05 +05:00
|
|
|
|
}
|
2023-12-05 17:04:11 +05:00
|
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
|
'parent': parent_id,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.debug('Request: %s', json.dumps(payload))
|
|
|
|
|
|
|
|
|
|
response = requests.patch(
|
|
|
|
|
base_url,
|
|
|
|
|
headers=headers,
|
|
|
|
|
data=json.dumps(payload),
|
|
|
|
|
)
|
2023-11-30 20:47:05 +05:00
|
|
|
|
|
2023-12-05 12:50:02 +05:00
|
|
|
|
if response.status_code == 200:
|
2023-12-05 17:04:11 +05:00
|
|
|
|
print('Родительская задача успешно назначена')
|
2023-12-08 17:55:38 +05:00
|
|
|
|
logger.info('Task %s successfully assigned a parent',task_id)
|
2023-12-05 12:50:02 +05:00
|
|
|
|
else:
|
2023-12-05 17:04:11 +05:00
|
|
|
|
print('Ошибка при назначении родительской задачи:', response.content)
|
2023-12-05 02:08:07 +05:00
|
|
|
|
|
2023-12-05 17:04:11 +05:00
|
|
|
|
logger.debug('Response: %s', response.content)
|
2023-12-07 17:53:18 +05:00
|
|
|
|
|
|
|
|
|
time.sleep(1) # добавляем задержку перед следующим запросом к API
|
2023-12-05 17:04:11 +05:00
|
|
|
|
# здесь я закончила добавлять про родительскую задачу с большой надеждой
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
2023-12-05 18:26:18 +05:00
|
|
|
|
|
2023-11-23 21:44:55 +05:00
|
|
|
|
file = open(asana_data_json, "r", encoding="utf8")
|
|
|
|
|
json_data = json.loads(file.read())
|
2023-11-21 15:54:48 +05:00
|
|
|
|
|
2023-12-08 17:55:38 +05:00
|
|
|
|
yandex_tracker_data = transform_data(json_data['data'])
|
2023-12-08 15:04:51 +05:00
|
|
|
|
create_tasks_in_tracker(yandex_tracker_data, limit=3)
|