# -*- coding: utf-8 -*- """Spielt workflows/pruefen-und-importieren.json in n8n ein, Credentials und Webhook-IDs bleiben erhalten.""" import json, os, urllib.request w = json.load(open('workflows/pruefen-und-importieren.json', encoding='utf-8')) wid_ = json.load(open('build/workflow-ids.json'))['pruefen-und-importieren'] H = {'X-N8N-API-KEY': os.environ['N8N_API_KEY'], 'Content-Type': 'application/json'} base = os.environ['N8N_BASE_URL'].rstrip('/') + '/api/v1/workflows/' + wid_ alt = json.load(urllib.request.urlopen(urllib.request.Request(base, headers=H))) cred = {n['name']: n.get('credentials') for n in alt['nodes'] if n.get('credentials')} hook = {n['name']: n.get('webhookId') for n in alt['nodes'] if n.get('webhookId')} for n in w['nodes']: if n['name'] in cred: n['credentials'] = cred[n['name']] if n['name'] in hook: n['webhookId'] = hook[n['name']] erlaubt = ('executionOrder', 'saveManualExecutions', 'callerPolicy', 'errorWorkflow', 'timezone', 'saveDataErrorExecution', 'saveDataSuccessExecution', 'saveExecutionProgress', 'executionTimeout') body = {'name': alt['name'], 'nodes': w['nodes'], 'connections': w['connections'], 'settings': {k: v for k, v in (alt.get('settings') or {}).items() if k in erlaubt}} d = json.load(urllib.request.urlopen(urllib.request.Request(base, data=json.dumps(body).encode(), headers=H, method='PUT'))) print('aktualisiert', d['id'], 'aktiv' if d['active'] else 'inaktiv', len(d['nodes']), 'Nodes')