122 lines
5.6 KiB
Python
122 lines
5.6 KiB
Python
import requests
|
|
|
|
MONDAY_TOKEN = "REMOVED"
|
|
MONDAY_URL = "https://api.monday.com/v2"
|
|
|
|
OP_URL = "https://projetos.rede5.com.br"
|
|
OP_API_KEY = "9c704b1c48939f97463fd9d811a9de050317b85224b93694d6e037f0799d37c2"
|
|
OP_AUTH = ('apikey', OP_API_KEY)
|
|
|
|
def monday_query(query):
|
|
headers = {"Authorization": MONDAY_TOKEN, "Content-Type": "application/json"}
|
|
resp = requests.post(MONDAY_URL, json={'query': query}, headers=headers)
|
|
return resp.json()
|
|
|
|
def run():
|
|
# 1. MIGRATE USERS
|
|
print("--- Migrando Usuários ---")
|
|
users_data = monday_query("{ users { id name email } }")
|
|
monday_users = users_data['data']['users']
|
|
|
|
op_users_resp = requests.get(f"{OP_URL}/api/v3/users", auth=OP_AUTH).json()
|
|
user_mapping = {u['email']: u['id'] for u in op_users_resp.get('_embedded', {}).get('elements', [])}
|
|
|
|
for mu in monday_users:
|
|
email = mu['email']
|
|
if email not in user_mapping:
|
|
print(f"Criando usuário: {mu['name']} ({email})")
|
|
name_parts = mu['name'].split(' ', 1)
|
|
first_name = name_parts[0]
|
|
last_name = name_parts[1] if len(name_parts) > 1 else "."
|
|
|
|
user_payload = {
|
|
"login": email.split('@')[0],
|
|
"email": email,
|
|
"firstName": first_name,
|
|
"lastName": last_name,
|
|
"password": "Password@12345!", # Senha padrão obrigatória
|
|
"admin": False,
|
|
"status": "active"
|
|
}
|
|
res = requests.post(f"{OP_URL}/api/v3/users", auth=OP_AUTH, json=user_payload)
|
|
if res.status_code == 201:
|
|
user_mapping[email] = res.json()['id']
|
|
print(f" OK: Usuário criado (ID {user_mapping[email]})")
|
|
else:
|
|
print(f" Erro ao criar usuário {email}: {res.text}")
|
|
|
|
# 2. MIGRATE ALL SPRINTS/PROJECTS AND THEIR TASKS
|
|
print("\n--- Migrando Todas as Tarefas (Q1, Zeus, etc) ---")
|
|
PROJECT_ID = 9 # ID do projeto Sprints no OpenProject
|
|
|
|
# Buscar WPs existentes no OP para não duplicar
|
|
existing_wps = requests.get(f"{OP_URL}/api/v3/projects/{PROJECT_ID}/work_packages", auth=OP_AUTH).json()
|
|
op_existing_parents = {wp['subject']: wp['id'] for wp in existing_wps.get('_embedded', {}).get('elements', []) if wp['subject'].startswith('PAI: ')}
|
|
op_existing_tasks = {wp['subject']: wp['id'] for wp in existing_wps.get('_embedded', {}).get('elements', [])}
|
|
|
|
# Buscar itens do board Sprints
|
|
sprints_data = monday_query("{ boards(ids: 18400111743) { items_page { items { id name } } } }")
|
|
sprints_items = sprints_data['data']['boards'][0]['items_page']['items']
|
|
|
|
sprint_parents_op = {} # ID Monday -> ID OP
|
|
|
|
for item in sprints_items:
|
|
subject = f"PAI: {item['name']}"
|
|
if subject in op_existing_parents:
|
|
sprint_parents_op[str(item['id'])] = op_existing_parents[subject]
|
|
print(f"Pai '{item['name']}' já existe (ID OP: {op_existing_parents[subject]})")
|
|
else:
|
|
payload = {
|
|
"subject": subject,
|
|
"description": {"format": "markdown", "raw": f"Projeto {item['name']} migrado do Monday"},
|
|
"_links": {
|
|
"project": {"href": f"/api/v3/projects/{PROJECT_ID}"}
|
|
}
|
|
}
|
|
res = requests.post(f"{OP_URL}/api/v3/work_packages", auth=OP_AUTH, json=payload)
|
|
if res.status_code == 201:
|
|
wp_id = res.json()['id']
|
|
sprint_parents_op[str(item['id'])] = wp_id
|
|
print(f"Pai '{item['name']}' criado: ID {wp_id}")
|
|
else:
|
|
print(f"Erro ao criar PAI {item['name']}: {res.text}")
|
|
|
|
# Buscar todas as tasks e relacioná-las
|
|
tasks_data = monday_query("{ boards(ids: 18400111746) { items_page { items { id name column_values { id text ... on BoardRelationValue { linked_item_ids } } } } } }")
|
|
tasks = tasks_data['data']['boards'][0]['items_page']['items']
|
|
|
|
print(f"\nVerificando {len(tasks)} tarefas para vinculação...")
|
|
for task in tasks:
|
|
linked_sprint_ids = []
|
|
for cv in task['column_values']:
|
|
if cv['id'] == "task_sprint" and cv.get('linked_item_ids'):
|
|
linked_sprint_ids = cv['linked_item_ids']
|
|
|
|
if linked_sprint_ids:
|
|
for lid in linked_sprint_ids:
|
|
lid_str = str(lid)
|
|
if lid_str in sprint_parents_op:
|
|
parent_id = sprint_parents_op[lid_str]
|
|
|
|
if task['name'] not in op_existing_tasks:
|
|
payload = {
|
|
"subject": task['name'],
|
|
"description": {"format": "markdown", "raw": "Tarefa migrada do Monday"},
|
|
"_links": {
|
|
"project": {"href": f"/api/v3/projects/{PROJECT_ID}"},
|
|
"parent": {"href": f"/api/v3/work_packages/{parent_id}"}
|
|
}
|
|
}
|
|
res = requests.post(f"{OP_URL}/api/v3/work_packages", auth=OP_AUTH, json=payload)
|
|
if res.status_code == 201:
|
|
print(f" -> OK: '{task['name']}' vinculada ao pai ID {parent_id}")
|
|
op_existing_tasks[task['name']] = res.json()['id']
|
|
else:
|
|
print(f" -> Erro na tarefa '{task['name']}': {res.text}")
|
|
else:
|
|
print(f" -> Aviso: Tarefa '{task['name']}' já existe (ignorada).")
|
|
else:
|
|
print(f" -> Tarefa solta (sem vínculo): '{task['name']}'")
|
|
|
|
if __name__ == '__main__':
|
|
run()
|