93 lines
3.4 KiB
Python
93 lines
3.4 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_sync_owners():
|
|
print("--- Sincronizando Responsáveis e Descrições ---")
|
|
|
|
# 1. Obter mapeamento de usuários OP (Nome -> ID)
|
|
# Como o email está difícil via API do Monday, vamos mapear por Nome exato
|
|
op_users = requests.get(f"{OP_URL}/api/v3/users", auth=OP_AUTH).json()
|
|
user_mapping = {u['name']: u['id'] for u in op_users['_embedded']['elements']}
|
|
|
|
# 2. Mapear Projetos Reais no OP
|
|
op_projects = requests.get(f"{OP_URL}/api/v3/projects", auth=OP_AUTH).json()
|
|
project_mapping = {p['name']: p['id'] for p in op_projects['_embedded']['elements']}
|
|
|
|
# 3. Mapeamento de Sprints do Monday
|
|
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']}
|
|
|
|
# 4. Buscar Tasks no Monday
|
|
query = """
|
|
{
|
|
boards(ids: 18400111746) {
|
|
items_page {
|
|
items {
|
|
id
|
|
name
|
|
column_values {
|
|
id
|
|
text
|
|
type
|
|
... on BoardRelationValue { linked_item_ids }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"""
|
|
tasks_data = monday_query(query)
|
|
tasks = tasks_data['data']['boards'][0]['items_page']['items']
|
|
|
|
for task in tasks:
|
|
target_project_name = None
|
|
owner_name = None
|
|
description_parts = []
|
|
|
|
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)]
|
|
|
|
if cv['id'] == "task_owner" and cv['text']:
|
|
owner_name = cv['text']
|
|
|
|
if cv['text'] and cv['type'] not in ["people", "board_relation"]:
|
|
description_parts.append(f"**{cv['id'].replace('task_', '').capitalize()}**: {cv['text']}")
|
|
|
|
if target_project_name and target_project_name in project_mapping:
|
|
p_id = project_mapping[target_project_name]
|
|
|
|
owner_id = user_mapping.get(owner_name) if owner_name else None
|
|
|
|
wp_payload = {
|
|
"subject": task['name'],
|
|
"description": {"format": "markdown", "raw": "\n".join(description_parts) or "Migrado do Monday.com"},
|
|
"_links": {
|
|
"project": {"href": f"/api/v3/projects/{p_id}"}
|
|
}
|
|
}
|
|
|
|
if owner_id:
|
|
wp_payload["_links"]["assignee"] = {"href": f"/api/v3/users/{owner_id}"}
|
|
print(f"Sincronizando: {task['name']} (Atribuído a: {owner_name})")
|
|
else:
|
|
print(f"Sincronizando: {task['name']} (Sem responsável)")
|
|
|
|
requests.post(f"{OP_URL}/api/v3/work_packages", auth=OP_AUTH, json=wp_payload)
|
|
|
|
if __name__ == "__main__":
|
|
run_sync_owners()
|