Skip to content

Commit 3487ca7

Browse files
📝 Add docstrings to fix_gitlab_new_mr
Docstrings generation was requested by @mimo-x. * #31 (comment) The following files were modified: * `gitlab_integration/gitlab_fetcher.py` * `gitlab_integration/webhook_listener.py` * `review_engine/handler/default_handler.py`
1 parent 77d5be0 commit 3487ca7

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

gitlab_integration/gitlab_fetcher.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,11 @@ def __init__(self, project_id, branch_name = ""):
111111

112112
def get_info(self):
113113
"""
114-
Get the project information
115-
:return: Project information
114+
Fetches project information from GitLab.
115+
116+
Sends an HTTP GET request using the instance's project_id to retrieve project details.
117+
Returns a dictionary of project information on success, or logs an error and returns None
118+
if the request fails.
116119
"""
117120
# URL for the GitLab API endpoint
118121
url = f"{GITLAB_SERVER_URL}/api/v4/projects/{self.project_id}"
@@ -196,6 +199,23 @@ def find_files_by_keyword(self, keyword, branch_name="main"):
196199
# 构建带有身份验证信息的 URL
197200
def _build_authenticated_url(self, repo_url):
198201
# 如果 URL 使用 https
202+
"""
203+
Build an authenticated GitLab repository URL using a private token.
204+
205+
This method embeds OAuth2 credentials into the provided repository URL by
206+
injecting the GitLab private token. If the URL begins with "http://" or "https://",
207+
the method constructs a new URL with the token included. Otherwise, it raises a
208+
ValueError for unsupported URL schemes.
209+
210+
Args:
211+
repo_url: The original repository URL (must begin with "http://" or "https://").
212+
213+
Returns:
214+
An authenticated URL string with embedded OAuth2 credentials.
215+
216+
Raises:
217+
ValueError: If the URL does not start with a supported scheme.
218+
"""
199219
token = GITLAB_PRIVATE_TOKEN
200220
if repo_url.startswith("https://"):
201221
return f"https://oauth2:{token}@{repo_url[8:]}"
@@ -207,7 +227,19 @@ def _build_authenticated_url(self, repo_url):
207227

208228
def is_merge_request_opened(gitlab_payload) -> bool:
209229
"""
210-
判断是否是merge request打开事件
230+
Determines if a merge request is open.
231+
232+
Checks whether the provided GitLab payload indicates an open merge request by verifying that its
233+
"object_attributes" have either a state of "opened" with a merge status of "preparing" or a state
234+
of "merged" with a merge status of "can_be_merged". If any error occurs during the evaluation, the
235+
error is logged and False is returned.
236+
237+
Args:
238+
gitlab_payload: Dictionary containing merge request attributes, including an "object_attributes"
239+
key with state and merge_status information.
240+
241+
Returns:
242+
bool: True if the merge request meets one of the open criteria; otherwise, False.
211243
"""
212244
try:
213245
gitlab_merge_request_old = gitlab_payload.get("object_attributes").get("state") == "opened" and gitlab_payload.get("object_attributes").get("merge_status") == "preparing"

gitlab_integration/webhook_listener.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
class WebhookListener:
1313
def __init__(self):
14+
"""
15+
Initializes a WebhookListener instance.
16+
"""
1417
pass
1518

1619
def handle_webhook(self):
@@ -51,7 +54,19 @@ def call_handle(self, gitlab_payload, event_type):
5154

5255
def handle_merge_request(self, gitlab_payload, reply):
5356
"""
54-
处理合并请求事件
57+
Process a merge request event triggered by a GitLab webhook.
58+
59+
If the merge request is open (as determined by is_merge_request_opened), logs the event,
60+
extracts project and merge request identifiers from the payload, and starts an asynchronous
61+
thread to process the merge using a ReviewEngine instance. Otherwise, returns a response
62+
indicating that no review is necessary.
63+
64+
Args:
65+
gitlab_payload: A dictionary containing the webhook payload with 'project' and 'object_attributes' keys.
66+
reply: Data used to initialize the ReviewEngine for merge request processing.
67+
68+
Returns:
69+
A tuple comprising a JSON response and an HTTP status code (200).
5570
"""
5671
if is_merge_request_opened(gitlab_payload):
5772
log.info("首次merge_request ", gitlab_payload)

review_engine/handler/default_handler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def merge_handle(self, gitlabMergeRequestFetcher, gitlabRepoManager, hook_info,
6565
self.default_handle(changes, merge_info, hook_info, reply, model)
6666

6767
def default_handle(self, changes, merge_info, hook_info, reply, model):
68+
"""
69+
Processes merge request changes to generate and add code review replies.
70+
71+
Evaluates the list of changes and, if the file count is within the allowed threshold, creates a code review note using concurrent processing. When review information is generated, it adds detailed reply messages—including project and merge request details—to the reply object. If the review note is not produced (indicating that changes have already been merged) or if too many files are changed, the method adds a corresponding fallback or warning reply. If no valid changes are provided, it logs an error with contextual merge request information.
72+
"""
6873
if changes and len(changes) <= MAX_FILES:
6974
# Code Review 信息
7075
review_info = chat_review(changes, generate_review_note, model)

0 commit comments

Comments
 (0)