120 lines
4.6 KiB
Python
120 lines
4.6 KiB
Python
import requests
|
|
import json
|
|
|
|
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():
|
|
print("--- Transformando itens em Projetos Reais ---")
|
|
|
|
# Lista de nomes que você quer como projetos
|
|
project_names = [
|
|
"Zeus", "THOR(q1fit)", "Q1FIT", "SaveinMed", "PHOTUM",
|
|
"Q1Vestuario", "GoHorseJobs", "Q1 SITE", "Q1food", "Q1Agenda"
|
|
]
|
|
|
|
op_project_mapping = {} # Nome -> ID do Projeto no OP
|
|
|
|
for name in project_names:
|
|
print(f"Criando projeto: {name}")
|
|
identifier = name.lower().replace('(', '').replace(')', '').replace(' ', '-').replace('ã', 'a').replace('ç', 'c')[:100]
|
|
|
|
# Verificar se projeto já existe
|
|
check_res = requests.get(f"{OP_URL}/api/v3/projects", auth=OP_AUTH).json()
|
|
existing = next((p for p in check_res['_embedded']['elements'] if p['name'] == name), None)
|
|
|
|
if existing:
|
|
op_project_mapping[name] = existing['id']
|
|
print(f" Projeto já existe: ID {existing['id']}")
|
|
else:
|
|
payload = {
|
|
"name": name,
|
|
"identifier": identifier,
|
|
"description": {"format": "markdown", "raw": f"Projeto {name} migrado do Monday"}
|
|
}
|
|
res = requests.post(f"{OP_URL}/api/v3/projects", auth=OP_AUTH, json=payload)
|
|
if res.status_code == 201:
|
|
op_id = res.json()['id']
|
|
op_project_mapping[name] = op_id
|
|
print(f" OK: Criado com ID {op_id}")
|
|
else:
|
|
print(f" Erro ao criar {name}: {res.text}")
|
|
|
|
# Agora vamos buscar as tarefas do board de 'Tasks' e 'Subelementos' do Monday
|
|
# E colocá-las nos projetos certos
|
|
print("\n--- Migrando Tarefas para os Novos Projetos ---")
|
|
|
|
# Mapeamento do Monday (ID do item no board Sprints -> Nome do Projeto)
|
|
# Vou buscar os IDs dinamicamente do board Sprints
|
|
sprints_data = monday_query("{ boards(ids: 18400111743) { items_page { items { id name } } } }")
|
|
monday_id_to_name = {str(item['id']): item['name'] for item in sprints_data['data']['boards'][0]['items_page']['items']}
|
|
|
|
# Buscar todas as tasks (incluindo subitems)
|
|
query = """
|
|
{
|
|
boards(ids: 18400111746) {
|
|
items_page {
|
|
items {
|
|
id
|
|
name
|
|
column_values {
|
|
id
|
|
... on BoardRelationValue { linked_item_ids }
|
|
}
|
|
subitems {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
tasks_data = monday_query(query)
|
|
tasks = tasks_data['data']['boards'][0]['items_page']['items']
|
|
|
|
for task in tasks:
|
|
# Descobrir a qual projeto essa task pertence
|
|
target_project_name = None
|
|
for cv in task['column_values']:
|
|
if cv['id'] == "task_sprint" and cv.get('linked_item_ids'):
|
|
for lid in cv['linked_item_ids']:
|
|
if str(lid) in monday_id_to_name:
|
|
target_project_name = monday_id_to_name[str(lid)]
|
|
break
|
|
|
|
if target_project_name and target_project_name in op_project_mapping:
|
|
proj_id = op_project_mapping[target_project_name]
|
|
|
|
# Criar a tarefa no projeto certo
|
|
def create_wp(subject, p_id, parent_id=None):
|
|
wp_payload = {
|
|
"subject": subject,
|
|
"_links": {"project": {"href": f"/api/v3/projects/{p_id}"}}
|
|
}
|
|
if parent_id:
|
|
wp_payload["_links"]["parent"] = {"href": f"/api/v3/work_packages/{parent_id}"}
|
|
|
|
# Evitar duplicados simples (opcional)
|
|
r = requests.post(f"{OP_URL}/api/v3/work_packages", auth=OP_AUTH, json=wp_payload)
|
|
return r.json()['id'] if r.status_code == 201 else None
|
|
|
|
print(f"Movendo '{task['name']}' para projeto '{target_project_name}'")
|
|
parent_id = create_wp(task['name'], proj_id)
|
|
|
|
# Migrar subitems se houver
|
|
for sub in task.get('subitems', []):
|
|
print(f" -> Subitem: {sub['name']}")
|
|
create_wp(sub['name'], proj_id, parent_id)
|
|
|
|
if __name__ == "__main__":
|
|
run()
|