-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab-mr-processor.py
More file actions
181 lines (143 loc) · 4.9 KB
/
Copy pathgitlab-mr-processor.py
File metadata and controls
181 lines (143 loc) · 4.9 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
import json
import os
import urllib.request
def post_review(body):
object_attributes = body.get("object_attributes", {})
mr_iid = object_attributes.get("iid")
project_id = body.get("project", {}).get("id")
mr_title = object_attributes.get("title", "")
mr_author = body.get("user", {}).get("name", "Unknown")
GITLAB_TOKEN = os.environ["GITLAB_TOKEN"]
GITLAB_URL = os.environ["GITLAB_URL"]
# Fetch diff
diff_url = f"{GITLAB_URL}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/diffs"
req = urllib.request.Request(
diff_url,
headers={"PRIVATE-TOKEN": GITLAB_TOKEN}
)
with urllib.request.urlopen(req) as resp:
diffs = json.loads(resp.read())
diff_text = ""
for d in diffs:
diff_text += f"\n\n### File: {d.get('new_path')}\n{d.get('diff', '')}"
if not diff_text.strip():
print("Empty diff — skipping")
return
# LangGraph pipeline
from langgraph.graph import StateGraph, END
from langchain_aws import ChatBedrock
from langchain_core.messages import HumanMessage
from typing import TypedDict
class ReviewState(TypedDict):
diff: str
security_issues: str
quality_issues: str
bug_issues: str
llm = ChatBedrock(
model_id="amazon.nova-lite-v1:0",
region_name="us-east-1"
)
def security_agent(state: ReviewState) -> ReviewState:
prompt = f"""You are a security code reviewer.
Analyze this diff ONLY for security issues.
Hardcoded secrets, injection risks, broken auth.
For each issue use EXACTLY this format:
ISSUE: <description>
SEVERITY: <High | Medium | Low>
FILE: <filename>
LINE: <line number or N/A>
SUGGESTION: <fix>
If nothing found write: NO SECURITY ISSUES FOUND
Diff:
{state['diff']}"""
response = llm.invoke([HumanMessage(content=prompt)])
return {**state, "security_issues": response.content}
def quality_agent(state: ReviewState) -> ReviewState:
prompt = f"""You are a code quality reviewer.
Analyze this diff ONLY for quality issues.
Poor naming, duplication, complexity, missing error handling.
For each issue use EXACTLY this format:
ISSUE: <description>
SEVERITY: <High | Medium | Low>
FILE: <filename>
LINE: <line number or N/A>
SUGGESTION: <fix>
If nothing found write: NO QUALITY ISSUES FOUND
Diff:
{state['diff']}"""
response = llm.invoke([HumanMessage(content=prompt)])
return {**state, "quality_issues": response.content}
def bug_agent(state: ReviewState) -> ReviewState:
prompt = f"""You are a bug detection expert.
Analyze this diff ONLY for bugs.
Null pointers, off-by-one, race conditions, unhandled exceptions.
For each issue use EXACTLY this format:
ISSUE: <description>
SEVERITY: <High | Medium | Low>
FILE: <filename>
LINE: <line number or N/A>
SUGGESTION: <fix>
If nothing found write: NO BUGS FOUND
Diff:
{state['diff']}"""
response = llm.invoke([HumanMessage(content=prompt)])
return {**state, "bug_issues": response.content}
def aggregator(state: ReviewState) -> ReviewState:
return state
# Build graph
builder = StateGraph(ReviewState)
builder.add_node("security", security_agent)
builder.add_node("quality", quality_agent)
builder.add_node("bugs", bug_agent)
builder.add_node("aggregate", aggregator)
builder.set_entry_point("security")
builder.add_edge("security", "quality")
builder.add_edge("quality", "bugs")
builder.add_edge("bugs", "aggregate")
builder.add_edge("aggregate", END)
graph = builder.compile()
result = graph.invoke({
"diff": diff_text,
"security_issues": "",
"quality_issues": "",
"bug_issues": ""
})
# Build comment
comment = f"""## 🤖 AI Code Review — {mr_title}
> **Author:** {mr_author} | **MR:** !{mr_iid}
---
### 🔒 Security Review
{result['security_issues']}
---
### 🧹 Code Quality Review
{result['quality_issues']}
---
### 🐛 Bug Detection
{result['bug_issues']}
---
*Reviewed by AI pipeline — Security · Quality · Bug agents*"""
# Post comment to GitLab
comment_url = f"{GITLAB_URL}/api/v4/projects/{project_id}/merge_requests/{mr_iid}/notes"
payload = json.dumps({"body": comment}).encode()
post_req = urllib.request.Request(
comment_url,
data=payload,
headers={
"PRIVATE-TOKEN": GITLAB_TOKEN,
"Content-Type": "application/json"
},
method="POST"
)
with urllib.request.urlopen(post_req) as resp:
print(f"Comment posted: {resp.getcode()} for MR !{mr_iid}")
def lambda_handler(event, context):
print("Processor Lambda started")
try:
# Event payload from Lambda A
body = json.loads(event.get("body", "{}"))
post_review(body)
print("Review completed successfully")
except Exception as e:
print(f"ERROR in processor: {str(e)}")
import traceback
traceback.print_exc()