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-11-21 15:54:48 +05:00
|
|
|
|
|
|
|
import requests
|
|
|
|
from yandex_tracker_client import TrackerClient
|
|
|
|
|
|
|
|
|
2023-11-23 23:41:28 +05:00
|
|
|
YANDEX_TRACKER_TOKEN = 'y0_AgAAAABAmFP8AArTqQAAAADyyFLmOoHqPprnRKag6B8okqjoDx2m9P0'
|
2023-11-21 15:54:48 +05:00
|
|
|
ORG_ID = '35d164dcf81e48f2b6a532f4badddd33'
|
2023-11-24 23:43:18 +05:00
|
|
|
# TESTIMPORT = 'https://tracker.yandex.ru/pages/projects/5'
|
2023-11-24 13:33:17 +05:00
|
|
|
|
2023-11-21 15:54:48 +05:00
|
|
|
|
2023-11-23 21:44:55 +05:00
|
|
|
client = TrackerClient(
|
|
|
|
token='YANDEX_TRACKER_TOKEN',
|
|
|
|
org_id='ORG_ID'
|
|
|
|
)
|
2023-11-21 15:54:48 +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-11-23 21:44:55 +05:00
|
|
|
sys.stdout.reconfigure(encoding='utf-8')
|
2023-11-22 11:51:22 +05:00
|
|
|
|
|
|
|
# ниже функция для случаев, когда не будет готового файла для импорта
|
|
|
|
# def get_data_from_asana():
|
|
|
|
# """ Получение данных из Asana """
|
|
|
|
|
|
|
|
# headers = {
|
|
|
|
# 'Authorization': 'ASANA_TOKEN',
|
|
|
|
# }
|
|
|
|
# response = requests.get('https://api.asana.com/projects/PROJECT_ID/tasks', headers=headers)
|
|
|
|
# data = response.json()
|
|
|
|
# return data
|
|
|
|
|
|
|
|
|
2023-11-27 12:59:57 +05:00
|
|
|
def get_assignee_data(data):
|
|
|
|
""" Получение данных для поля assignee """
|
|
|
|
|
|
|
|
mapping = {}
|
|
|
|
|
|
|
|
with open(users_data, 'r', encoding="utf8") as csv_file:
|
|
|
|
reader = csv.DictReader(csv_file)
|
|
|
|
for row in reader:
|
|
|
|
asana_id = row[1]
|
|
|
|
ya_login = row[2]
|
|
|
|
mapping[asana_id] = ya_login
|
|
|
|
return mapping
|
|
|
|
|
|
|
|
assignee = value for value in mapping[asana_id]
|
|
|
|
|
|
|
|
|
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-11-23 21:44:55 +05:00
|
|
|
for task in data["data"]:
|
2023-11-22 11:51:22 +05:00
|
|
|
transformed_task = {
|
|
|
|
'summary': task['name'],
|
|
|
|
'description': task['notes'],
|
|
|
|
'createdAt': task['created_at'],
|
|
|
|
'deadline': task['due_on'],
|
2023-11-24 23:43:18 +05:00
|
|
|
# 'assignee': task['assignee'],
|
2023-11-23 21:44:55 +05:00
|
|
|
# 'milestone': task['section'],
|
2023-11-22 11:51:22 +05:00
|
|
|
# 'memberships': task['memberships'],
|
|
|
|
# 'parent': task[''],
|
|
|
|
'status': task['completed'],
|
|
|
|
}
|
|
|
|
transformed_data.append(transformed_task)
|
|
|
|
return transformed_data
|
|
|
|
|
|
|
|
|
|
|
|
def create_tasks_in_tracker(data):
|
2023-11-22 12:13:13 +05:00
|
|
|
""" Создание задач в ЯндексТрекер """
|
2023-11-22 11:51:22 +05:00
|
|
|
|
2023-11-24 16:34:54 +05:00
|
|
|
base_url = 'https://api.tracker.yandex.net/v2/issues'
|
2023-11-22 11:51:22 +05:00
|
|
|
headers = {
|
2023-11-24 12:56:46 +05:00
|
|
|
'Host': 'api.tracker.yandex.net',
|
2023-11-24 14:22:19 +05:00
|
|
|
'Authorization': 'OAuth y0_AgAAAABAmFP8AArTqQAAAADyyFLmOoHqPprnRKag6B8okqjoDx2m9P0',
|
|
|
|
'X-Org-ID': '7095769',
|
|
|
|
'Content-Type': 'appication/json',
|
2023-11-21 15:54:48 +05:00
|
|
|
}
|
2023-11-22 11:51:22 +05:00
|
|
|
for task in data:
|
|
|
|
payload = {
|
2023-11-24 13:33:17 +05:00
|
|
|
'queue': 'TESTIMPORT', # Вынести в переменную
|
2023-11-22 11:51:22 +05:00
|
|
|
'summary': task['summary'],
|
|
|
|
'description': task['description'],
|
|
|
|
'createdAt': task['createdAt'],
|
|
|
|
'deadline': task['deadline'],
|
2023-11-27 12:59:57 +05:00
|
|
|
'assignee': assignee,
|
2023-11-23 21:44:55 +05:00
|
|
|
# 'milestone': task['milestone'],
|
2023-11-22 11:51:22 +05:00
|
|
|
# 'memberships': task['memberships'],
|
|
|
|
# 'parent': task['parent'],
|
|
|
|
'status': task['status'],
|
|
|
|
}
|
|
|
|
response = requests.post(
|
|
|
|
base_url,
|
|
|
|
headers=headers,
|
|
|
|
data=json.dumps(payload),
|
|
|
|
)
|
|
|
|
if response.status_code == 201:
|
2023-11-22 12:13:13 +05:00
|
|
|
print('Задача успешно создана в ЯндексТрекер')
|
2023-11-22 11:51:22 +05:00
|
|
|
else:
|
|
|
|
print(
|
2023-11-22 12:13:13 +05:00
|
|
|
'Ошибка при создании задачи в ЯндексТрекер:',
|
2023-11-22 11:51:22 +05:00
|
|
|
response.content,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-11-23 21:44:55 +05:00
|
|
|
# asana_data = get_data_from_asana()
|
|
|
|
file = open(asana_data_json, "r", encoding="utf8")
|
|
|
|
json_data = json.loads(file.read())
|
2023-11-21 15:54:48 +05:00
|
|
|
|
2023-11-23 21:44:55 +05:00
|
|
|
yandex_tracker_data = transform_data(json_data)
|
|
|
|
create_tasks_in_tracker(yandex_tracker_data)
|