-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomation_engine.py
More file actions
177 lines (145 loc) · 6.42 KB
/
automation_engine.py
File metadata and controls
177 lines (145 loc) · 6.42 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
"""
Automation Engine
محرك الأتمتة - يشغل كل المهام
"""
import asyncio
from datetime import datetime
from database import SessionLocal, Automation
from connectors.github_connector import GitHubConnector
from connectors.azure_connector import AzureDevOpsConnector
from connectors.http_connector import HTTPConnector
from connectors.teams_connector import TeamsConnector
class AutomationEngine:
def __init__(self):
self.github = GitHubConnector()
self.azure = AzureDevOpsConnector()
self.http = HTTPConnector()
self.teams = TeamsConnector()
async def run_automation(self, user_id="default"):
"""
تشغيل عملية الأتمتة الكاملة
1. إنشاء Issue في GitHub
2. تشغيل Pipeline في Azure
3. إرسال طلب HTTP للسيرفر
4. إرسال إشعار Teams
"""
db = SessionLocal()
automation_record = Automation(
user_id=user_id,
trigger="Start automation",
status="running"
)
db.add(automation_record)
db.commit()
results = {
"automation_id": automation_record.id,
"started_at": datetime.now().isoformat(),
"steps": []
}
try:
# Step 1: إنشاء GitHub Issue
print("🚀 Step 1: إنشاء Issue في GitHub...")
github_result = self.github.create_issue(
title="Automated Issue",
body=f"تم إنشاء هذا Issue تلقائياً بواسطة CometX Bot\n\nوقت الإنشاء: {datetime.now()}"
)
results["steps"].append({
"step": 1,
"name": "GitHub Issue",
"result": github_result
})
if github_result.get("success"):
automation_record.github_issue_url = github_result.get("issue_url")
print(f"✅ {github_result.get('message')}")
else:
print(f"❌ خطأ في GitHub: {github_result.get('error')}")
# Step 2: تشغيل Azure Pipeline
print("\n🚀 Step 2: تشغيل Pipeline في Azure DevOps...")
azure_result = self.azure.trigger_pipeline()
results["steps"].append({
"step": 2,
"name": "Azure Pipeline",
"result": azure_result
})
if azure_result.get("success"):
automation_record.azure_pipeline_url = azure_result.get("build_url")
print(f"✅ {azure_result.get('message')}")
else:
print(f"❌ خطأ في Azure: {azure_result.get('error')}")
# Step 3: إرسال طلب HTTP
print("\n🚀 Step 3: إرسال طلب HTTP للسيرفر...")
http_result = await self.http.send_deploy_request({
"source": "CometX Automation Bot",
"timestamp": datetime.now().isoformat(),
"github_issue": github_result.get("issue_url"),
"azure_pipeline": azure_result.get("build_url")
})
results["steps"].append({
"step": 3,
"name": "HTTP Deploy",
"result": http_result
})
if http_result.get("success"):
automation_record.deploy_status = f"Success - {http_result.get('status_code')}"
print(f"✅ {http_result.get('message')}")
else:
automation_record.deploy_status = f"Failed - {http_result.get('error')}"
print(f"❌ خطأ في HTTP: {http_result.get('error')}")
# Step 4: إرسال إشعار Teams
print("\n🚀 Step 4: إرسال إشعار لـ Microsoft Teams...")
teams_result = self.teams.send_automation_complete(
github_url=github_result.get("issue_url"),
azure_url=azure_result.get("build_url"),
deploy_status=automation_record.deploy_status
)
results["steps"].append({
"step": 4,
"name": "Teams Notification",
"result": teams_result
})
if teams_result.get("success"):
automation_record.teams_notification = "sent"
print(f"✅ {teams_result.get('message')}")
else:
automation_record.teams_notification = "failed"
print(f"❌ خطأ في Teams: {teams_result.get('error')}")
# تحديث الحالة
automation_record.status = "completed"
results["status"] = "completed"
results["completed_at"] = datetime.now().isoformat()
print("\n✅ ✅ ✅ اكتملت عملية الأتمتة بنجاح! ✅ ✅ ✅")
except Exception as e:
automation_record.status = "failed"
automation_record.error_message = str(e)
results["status"] = "failed"
results["error"] = str(e)
print(f"\n❌ ❌ ❌ فشلت عملية الأتمتة: {e}")
# إرسال إشعار خطأ
self.teams.send_error_notification(str(e))
finally:
db.commit()
db.close()
return results
async def test_automation():
"""اختبار عملية الأتمتة"""
print("=" * 60)
print("🤖 CometX Automation Bot - Test Run")
print("=" * 60)
engine = AutomationEngine()
results = await engine.run_automation(user_id="test_user")
print("\n" + "=" * 60)
print("📊 النتائج:")
print("=" * 60)
print(f"Automation ID: {results['automation_id']}")
print(f"Status: {results['status']}")
print(f"Started: {results['started_at']}")
if 'completed_at' in results:
print(f"Completed: {results['completed_at']}")
print("\nالخطوات:")
for step in results['steps']:
print(f"\n Step {step['step']}: {step['name']}")
print(f" Success: {step['result'].get('success', False)}")
if 'message' in step['result']:
print(f" Message: {step['result']['message']}")
if __name__ == "__main__":
asyncio.run(test_automation())