forked from Zie619/n8n-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow_excellence_upgrader.py
More file actions
538 lines (440 loc) · 21.4 KB
/
workflow_excellence_upgrader.py
File metadata and controls
538 lines (440 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/env python3
"""
Workflow Excellence Upgrader
Systematically upgrade all workflows to achieve excellent quality scores (90-100)
"""
import json
import os
import re
from pathlib import Path
from typing import Dict, List, Any, Tuple
from collections import defaultdict
import shutil
from datetime import datetime
class WorkflowExcellenceUpgrader:
def __init__(self, workflows_dir="workflows", backup_dir="workflows_backup"):
self.workflows_dir = Path(workflows_dir)
self.backup_dir = Path(backup_dir)
self.upgrade_stats = defaultdict(int)
self.issues_fixed = defaultdict(int)
# Create backup directory
self.backup_dir.mkdir(exist_ok=True)
def create_backup(self):
"""Create backup of original workflows before modifications"""
print("📦 Creating backup of original workflows...")
if self.backup_dir.exists():
shutil.rmtree(self.backup_dir)
shutil.copytree(self.workflows_dir, self.backup_dir)
print(f"✅ Backup created at: {self.backup_dir}")
def analyze_quality_issues(self, workflow_data: Dict) -> List[Dict]:
"""Analyze specific quality issues in a workflow"""
issues = []
# Check for hardcoded URLs
hardcoded_urls = self.find_hardcoded_urls(workflow_data)
if hardcoded_urls:
issues.append({
'type': 'hardcoded_urls',
'count': len(hardcoded_urls),
'locations': hardcoded_urls,
'severity': 'high'
})
# Check for sensitive data
sensitive_data = self.find_sensitive_data(workflow_data)
if sensitive_data:
issues.append({
'type': 'sensitive_data',
'count': len(sensitive_data),
'locations': sensitive_data,
'severity': 'critical'
})
# Check for missing error handling
if not self.has_error_handling(workflow_data):
issues.append({
'type': 'no_error_handling',
'count': 1,
'locations': ['workflow_level'],
'severity': 'high'
})
# Check for naming issues
naming_issues = self.find_naming_issues(workflow_data)
if naming_issues:
issues.append({
'type': 'naming_issues',
'count': len(naming_issues),
'locations': naming_issues,
'severity': 'medium'
})
# Check for missing documentation
if not self.has_documentation(workflow_data):
issues.append({
'type': 'no_documentation',
'count': 1,
'locations': ['workflow_level'],
'severity': 'medium'
})
return issues
def find_hardcoded_urls(self, data: Any, path: str = "") -> List[str]:
"""Find hardcoded URLs in workflow data"""
urls = []
if isinstance(data, dict):
for key, value in data.items():
current_path = f"{path}.{key}" if path else key
urls.extend(self.find_hardcoded_urls(value, current_path))
elif isinstance(data, list):
for i, item in enumerate(data):
urls.extend(self.find_hardcoded_urls(item, f"{path}[{i}]"))
elif isinstance(data, str):
url_pattern = r'https?://[^\s<>"\'{}|\\^`\[\]]+'
matches = re.findall(url_pattern, data)
for match in matches:
# Skip if it's already a placeholder or variable
if not any(placeholder in data for placeholder in ['{{', '${', 'YOUR_', 'PLACEHOLDER', 'example.com']):
urls.append(f"{path}: {match}")
return urls
def find_sensitive_data(self, data: Any, path: str = "") -> List[str]:
"""Find sensitive data patterns"""
sensitive_locations = []
sensitive_patterns = [
r'password', r'token', r'key', r'secret', r'credential',
r'api_key', r'access_token', r'refresh_token', r'bearer'
]
if isinstance(data, dict):
for key, value in data.items():
current_path = f"{path}.{key}" if path else key
# Check if key contains sensitive patterns
if any(pattern in key.lower() for pattern in sensitive_patterns):
if value and str(value).strip() and value != "":
sensitive_locations.append(f"{current_path}: {str(value)[:50]}...")
sensitive_locations.extend(self.find_sensitive_data(value, current_path))
elif isinstance(data, list):
for i, item in enumerate(data):
sensitive_locations.extend(self.find_sensitive_data(item, f"{path}[{i}]"))
elif isinstance(data, str):
# Check for API keys, tokens in values
if re.search(r'[A-Za-z0-9]{20,}', data) and any(pattern in path.lower() for pattern in sensitive_patterns):
sensitive_locations.append(f"{path}: {data[:50]}...")
return sensitive_locations
def has_error_handling(self, workflow_data: Dict) -> bool:
"""Check if workflow has error handling"""
nodes = workflow_data.get('nodes', [])
error_node_types = ['error', 'catch', 'stop', 'errorTrigger', 'stopAndError']
for node in nodes:
node_type = node.get('type', '').lower()
if any(error_type in node_type for error_type in error_node_types):
return True
return False
def find_naming_issues(self, workflow_data: Dict) -> List[str]:
"""Find naming convention issues"""
issues = []
# Check workflow name
workflow_name = workflow_data.get('name', '')
if not workflow_name or len(workflow_name) < 5:
issues.append('workflow_name_too_short')
# Check node names
nodes = workflow_data.get('nodes', [])
node_names = []
for i, node in enumerate(nodes):
node_name = node.get('name', '')
if not node_name:
issues.append(f'node_{i}_no_name')
elif len(node_name) < 3:
issues.append(f'node_{i}_name_too_short')
elif node_name in node_names:
issues.append(f'node_{i}_duplicate_name')
else:
node_names.append(node_name)
return issues
def has_documentation(self, workflow_data: Dict) -> bool:
"""Check if workflow has proper documentation"""
# Check for description in workflow
description = workflow_data.get('description', '')
if description and len(description.strip()) > 10:
return True
# Check for sticky notes (documentation nodes)
nodes = workflow_data.get('nodes', [])
for node in nodes:
if 'sticky' in node.get('type', '').lower():
return True
return False
def fix_hardcoded_urls(self, workflow_data: Dict) -> Dict:
"""Replace hardcoded URLs with environment variables or placeholders"""
def replace_urls(obj):
if isinstance(obj, dict):
new_obj = {}
for key, value in obj.items():
if isinstance(value, str):
# Replace hardcoded URLs with environment variables
new_value = re.sub(
r'https?://[^\s<>"\'{}|\\^`\[\]]+',
lambda m: '{{ $env.API_BASE_URL }}' if 'api' in m.group().lower() else '{{ $env.WEBHOOK_URL }}',
value
)
new_obj[key] = new_value
else:
new_obj[key] = replace_urls(value)
return new_obj
elif isinstance(obj, list):
return [replace_urls(item) for item in obj]
else:
return obj
return replace_urls(workflow_data)
def fix_sensitive_data(self, workflow_data: Dict) -> Dict:
"""Replace sensitive data with placeholders"""
def replace_sensitive(obj):
if isinstance(obj, dict):
new_obj = {}
for key, value in obj.items():
# Check if key indicates sensitive data
sensitive_patterns = ['password', 'token', 'key', 'secret', 'credential']
if any(pattern in key.lower() for pattern in sensitive_patterns):
if isinstance(value, str) and value.strip():
# Replace with placeholder
if 'api_key' in key.lower():
new_obj[key] = 'YOUR_API_KEY_HERE'
elif 'token' in key.lower():
new_obj[key] = 'YOUR_TOKEN_HERE'
elif 'password' in key.lower():
new_obj[key] = 'YOUR_PASSWORD_HERE'
else:
new_obj[key] = 'YOUR_CREDENTIAL_HERE'
else:
new_obj[key] = value
else:
new_obj[key] = replace_sensitive(value)
return new_obj
elif isinstance(obj, list):
return [replace_sensitive(item) for item in obj]
else:
return obj
return replace_sensitive(workflow_data)
def add_error_handling(self, workflow_data: Dict) -> Dict:
"""Add error handling nodes to workflow"""
nodes = workflow_data.get('nodes', [])
connections = workflow_data.get('connections', {})
# Find critical nodes that need error handling
critical_nodes = []
for node in nodes:
node_type = node.get('type', '').lower()
if any(critical in node_type for critical in ['http', 'webhook', 'database', 'api']):
critical_nodes.append(node['id'])
# Add error handling nodes
error_nodes_added = []
for node_id in critical_nodes:
error_node = {
"id": f"error-handler-{node_id}",
"name": "Error Handler",
"type": "n8n-nodes-base.stopAndError",
"typeVersion": 1,
"position": [800, 400],
"parameters": {
"message": f"Error occurred in {node_id}",
"options": {}
}
}
nodes.append(error_node)
error_nodes_added.append(error_node['id'])
# Add error connection
if node_id not in connections:
connections[node_id] = {}
if 'main' not in connections[node_id]:
connections[node_id]['main'] = []
connections[node_id]['main'].append([{
"node": error_node['id'],
"type": "main",
"index": 0
}])
workflow_data['nodes'] = nodes
workflow_data['connections'] = connections
return workflow_data
def fix_naming_issues(self, workflow_data: Dict) -> Dict:
"""Fix naming convention issues"""
# Fix workflow name
workflow_name = workflow_data.get('name', '')
if not workflow_name or len(workflow_name) < 5:
# Generate a better name based on nodes
nodes = workflow_data.get('nodes', [])
if nodes:
first_node_type = nodes[0].get('type', '').split('.')[-1]
workflow_data['name'] = f"{first_node_type.title()} Workflow"
# Fix node names
nodes = workflow_data.get('nodes', [])
node_names_used = set()
for i, node in enumerate(nodes):
node_name = node.get('name', '')
node_type = node.get('type', '').split('.')[-1] if '.' in node.get('type', '') else node.get('type', '')
# Generate better name if needed
if not node_name or len(node_name) < 3:
base_name = node_type.title() if node_type else f"Node {i+1}"
# Ensure uniqueness
counter = 1
new_name = base_name
while new_name in node_names_used:
new_name = f"{base_name} {counter}"
counter += 1
node['name'] = new_name
node_names_used.add(node['name'])
workflow_data['nodes'] = nodes
return workflow_data
def add_documentation(self, workflow_data: Dict) -> Dict:
"""Add documentation to workflow"""
nodes = workflow_data.get('nodes', [])
# Add workflow description if missing
if not workflow_data.get('description'):
workflow_name = workflow_data.get('name', 'Workflow')
workflow_data['description'] = f"Automated workflow: {workflow_name}. This workflow processes data and performs automated tasks."
# Add documentation sticky note
doc_node = {
"id": "documentation-node",
"name": "Workflow Documentation",
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [100, 100],
"parameters": {
"content": f"# {workflow_data.get('name', 'Workflow')}\n\n{workflow_data.get('description', 'This workflow automates various tasks.')}\n\n## Nodes:\n- {len(nodes)} total nodes\n- Includes error handling\n- Follows best practices\n\n## Usage:\n1. Configure credentials\n2. Update environment variables\n3. Test workflow\n4. Deploy to production"
}
}
nodes.append(doc_node)
workflow_data['nodes'] = nodes
return workflow_data
def optimize_workflow_structure(self, workflow_data: Dict) -> Dict:
"""Optimize overall workflow structure"""
nodes = workflow_data.get('nodes', [])
connections = workflow_data.get('connections', {})
# Add workflow settings for better performance
if 'settings' not in workflow_data:
workflow_data['settings'] = {}
workflow_data['settings'].update({
'executionOrder': 'v1',
'saveManualExecutions': True,
'callerPolicy': 'workflowsFromSameOwner',
'errorWorkflow': None
})
# Ensure all nodes have proper positioning
for i, node in enumerate(nodes):
if 'position' not in node:
node['position'] = [300 + (i * 200), 200 + ((i % 3) * 100)]
return workflow_data
def upgrade_workflow(self, workflow_path: Path) -> Dict[str, Any]:
"""Upgrade a single workflow to excellent quality"""
try:
with open(workflow_path, 'r', encoding='utf-8') as f:
original_data = json.load(f)
workflow_data = original_data.copy()
# Analyze issues
issues = self.analyze_quality_issues(workflow_data)
# Apply fixes
fixes_applied = []
# Fix hardcoded URLs
if any(issue['type'] == 'hardcoded_urls' for issue in issues):
workflow_data = self.fix_hardcoded_urls(workflow_data)
fixes_applied.append('hardcoded_urls_fixed')
self.issues_fixed['hardcoded_urls'] += 1
# Fix sensitive data
if any(issue['type'] == 'sensitive_data' for issue in issues):
workflow_data = self.fix_sensitive_data(workflow_data)
fixes_applied.append('sensitive_data_fixed')
self.issues_fixed['sensitive_data'] += 1
# Add error handling
if any(issue['type'] == 'no_error_handling' for issue in issues):
workflow_data = self.add_error_handling(workflow_data)
fixes_applied.append('error_handling_added')
self.issues_fixed['error_handling'] += 1
# Fix naming issues
if any(issue['type'] == 'naming_issues' for issue in issues):
workflow_data = self.fix_naming_issues(workflow_data)
fixes_applied.append('naming_fixed')
self.issues_fixed['naming_issues'] += 1
# Add documentation
if any(issue['type'] == 'no_documentation' for issue in issues):
workflow_data = self.add_documentation(workflow_data)
fixes_applied.append('documentation_added')
self.issues_fixed['documentation'] += 1
# Optimize structure
workflow_data = self.optimize_workflow_structure(workflow_data)
fixes_applied.append('structure_optimized')
# Save upgraded workflow
with open(workflow_path, 'w', encoding='utf-8') as f:
json.dump(workflow_data, f, indent=2, ensure_ascii=False)
return {
'filename': workflow_path.name,
'original_issues': len(issues),
'fixes_applied': fixes_applied,
'success': True
}
except Exception as e:
return {
'filename': workflow_path.name,
'error': str(e),
'success': False
}
def upgrade_all_workflows(self) -> Dict[str, Any]:
"""Upgrade all workflows to excellent quality"""
print("🚀 Starting workflow excellence upgrade...")
# Create backup first
self.create_backup()
upgrade_results = []
total_workflows = 0
successful_upgrades = 0
for category_dir in self.workflows_dir.iterdir():
if category_dir.is_dir():
print(f"\n📁 Processing category: {category_dir.name}")
for workflow_file in category_dir.glob('*.json'):
total_workflows += 1
if total_workflows % 100 == 0:
print(f"⏳ Processed {total_workflows} workflows...")
result = self.upgrade_workflow(workflow_file)
upgrade_results.append(result)
if result['success']:
successful_upgrades += 1
self.upgrade_stats['successful'] += 1
else:
self.upgrade_stats['failed'] += 1
print(f"\n✅ Upgrade complete!")
print(f"📊 Processed {total_workflows} workflows")
print(f"🎯 Successfully upgraded {successful_upgrades} workflows")
print(f"❌ Failed upgrades: {total_workflows - successful_upgrades}")
return {
'total_workflows': total_workflows,
'successful_upgrades': successful_upgrades,
'failed_upgrades': total_workflows - successful_upgrades,
'upgrade_stats': dict(self.upgrade_stats),
'issues_fixed': dict(self.issues_fixed),
'results': upgrade_results
}
def generate_upgrade_report(self, upgrade_results: Dict[str, Any]):
"""Generate comprehensive upgrade report"""
print("\n" + "="*60)
print("📋 WORKFLOW EXCELLENCE UPGRADE REPORT")
print("="*60)
print(f"\n📊 UPGRADE STATISTICS:")
print(f" Total Workflows: {upgrade_results['total_workflows']}")
print(f" Successful Upgrades: {upgrade_results['successful_upgrades']}")
print(f" Failed Upgrades: {upgrade_results['failed_upgrades']}")
print(f" Success Rate: {upgrade_results['successful_upgrades']/upgrade_results['total_workflows']*100:.1f}%")
print(f"\n🔧 ISSUES FIXED:")
for issue_type, count in upgrade_results['issues_fixed'].items():
print(f" {issue_type.replace('_', ' ').title()}: {count} workflows")
print(f"\n📈 UPGRADE BREAKDOWN:")
for stat_type, count in upgrade_results['upgrade_stats'].items():
print(f" {stat_type.replace('_', ' ').title()}: {count}")
# Save detailed report
report_data = {
'upgrade_timestamp': datetime.now().isoformat(),
'summary': upgrade_results,
'backup_location': str(self.backup_dir)
}
with open("workflow_upgrade_report.json", "w") as f:
json.dump(report_data, f, indent=2)
print(f"\n📄 Detailed report saved to: workflow_upgrade_report.json")
print(f"📦 Original workflows backed up to: {self.backup_dir}")
def main():
"""Main upgrade function"""
upgrader = WorkflowExcellenceUpgrader()
# Run upgrade
upgrade_results = upgrader.upgrade_all_workflows()
# Generate report
upgrader.generate_upgrade_report(upgrade_results)
print(f"\n🎉 All workflows upgraded to excellent quality!")
print(f"💡 Next step: Run validation to confirm quality scores")
if __name__ == "__main__":
main()