-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment_handler.py
More file actions
165 lines (135 loc) Β· 6.33 KB
/
comment_handler.py
File metadata and controls
165 lines (135 loc) Β· 6.33 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
#!/usr/bin/env python3
"""
Comment Handler - Creates formatted PR comments and summaries.
"""
from typing import Dict
from github import Repository
from github.PullRequest import PullRequest
class CommentHandler:
"""Handles creation of PR comments and summaries."""
def __init__(self, repo: Repository):
"""
Initialize comment handler.
Args:
repo: GitHub repository object
"""
self.repo = repo
def create_pr_summary(self, pr: PullRequest, status: Dict, all_passed: bool = False, details: Dict = None) -> str:
"""
Create a summary comment for a PR.
Args:
pr: Pull request object
status: Workflow status dictionary
all_passed: Whether all checks truly passed (including branch workflows)
details: Additional details about check status
Returns:
Formatted markdown comment
"""
lines = [
"## π€ faneX-ID Bot - PR Summary\n",
f"**PR:** #{pr.number} - {pr.title}\n",
f"**Author:** @{pr.user.login}\n",
f"**Branch:** `{pr.head.ref}` β `{pr.base.ref}`\n",
]
# Add overall status
if details:
if details.get("truly_all_passed"):
lines.append("\nβ
**All checks passed successfully!**\n\n")
else:
lines.append("\nβ οΈ **Some checks are still running or have failed.**\n\n")
if details.get("running_workflows"):
lines.append(f"β³ **{len(details['running_workflows'])} workflow(s) still running:**\n")
for wf in details["running_workflows"][:10]:
lines.append(f"- {wf['name']}: {wf['status']}\n")
if len(details["running_workflows"]) > 10:
lines.append(f"- ... and {len(details['running_workflows']) - 10} more\n")
lines.append("\n")
if details.get("failed_workflows"):
lines.append(f"β **{len(details['failed_workflows'])} workflow(s) failed:**\n")
for wf in details["failed_workflows"][:10]:
lines.append(f"- {wf['name']}: {wf.get('conclusion', 'failure')}\n")
if len(details["failed_workflows"]) > 10:
lines.append(f"- ... and {len(details['failed_workflows']) - 10} more\n")
lines.append("\n")
if details.get("pending_checks"):
lines.append(f"β³ **{len(details['pending_checks'])} check(s) still running**\n\n")
if details.get("failed_checks"):
lines.append(f"β **{len(details['failed_checks'])} check(s) failed**\n\n")
# Add CI/CD status
if status.get("workflows"):
lines.append("\n### π CI/CD Status\n\n")
lines.append("| Workflow | Status |\n")
lines.append("|----------|--------|\n")
for workflow in status["workflows"]:
name = workflow["name"]
state = workflow["status"]
# Fix: Handle None values properly
conclusion = workflow.get("conclusion") or "unknown"
# Emoji based on status
if conclusion == "success":
emoji = "β
"
elif conclusion == "failure":
emoji = "β"
elif conclusion == "cancelled":
emoji = "β οΈ"
elif state == "in_progress":
emoji = "π"
else:
emoji = "β³"
# Fix: Handle None values properly
if conclusion and conclusion != "unknown":
status_text = f"{emoji} {conclusion.upper()}"
else:
status_text = f"{emoji} {state.upper() if state else 'UNKNOWN'}"
workflow_url = workflow.get("url", "")
if workflow_url:
lines.append(f"| [`{name}`]({workflow_url}) | {status_text} |\n")
else:
lines.append(f"| `{name}` | {status_text} |\n")
# Summary
total = len(status["workflows"])
success = sum(1 for w in status["workflows"] if w.get("conclusion") == "success")
failed = sum(1 for w in status["workflows"] if w.get("conclusion") == "failure")
in_progress = sum(1 for w in status["workflows"] if w.get("status") == "in_progress")
lines.append(f"\n**Summary:** {success}/{total} passed, {failed} failed, {in_progress} in progress\n")
# Add helpful commands if there are failures
if failed > 0 or (details and (details.get("failed_workflows") or details.get("failed_checks"))):
lines.append("\n### π§ Quick Actions\n\n")
lines.append("If workflows failed, you can retry them:\n\n")
lines.append("- `/retry` - Retry all failed workflows\n")
lines.append("- `/retry <workflow-name>` - Retry a specific workflow\n")
lines.append("- `/status` - Check current status\n")
else:
lines.append("\nβ³ CI/CD checks are running...\n")
# Add helpful links
lines.append("\n### π Useful Commands\n\n")
lines.append("- `/help` - Show all available commands\n")
lines.append("- `/status` - Get current CI/CD status\n")
lines.append("- `/retry` - Retry failed workflows\n")
return "".join(lines)
def create_error_comment(self, error_message: str, context: str = "") -> str:
"""
Create an error comment.
Args:
error_message: Error message
context: Additional context
Returns:
Formatted error comment
"""
lines = [
"## β Error\n\n",
f"**Message:** {error_message}\n",
]
if context:
lines.append(f"\n**Context:** {context}\n")
lines.append("\nPlease check the logs and try again.\n")
return "".join(lines)
def create_success_comment(self, message: str) -> str:
"""
Create a success comment.
Args:
message: Success message
Returns:
Formatted success comment
"""
return f"## β
Success\n\n{message}\n"