From cfe794947d92bfcce324c3ac3a9c8a137dee5af8 Mon Sep 17 00:00:00 2001 From: "Hussam.lawen" Date: Thu, 15 Feb 2024 21:35:51 +0200 Subject: [PATCH] Gitlab /ask line works --- pr_agent/git_providers/gitlab_provider.py | 5 +++++ pr_agent/servers/gitlab_webhook.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index c4a9b488..b08fd336 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -181,6 +181,11 @@ class GitLabProvider(GitProvider): def edit_comment(self, comment, body: str): self.mr.notes.update(comment.id,{'body': body} ) + def reply_to_comment_from_comment_id(self, comment_id: int, body: str): + discussion = self.mr.discussions.get(comment_id) + discussion.notes.create({'body': body}) + self.mr.notes.create({'body': body, 'in_reply_to_discussion_id': comment_id}) + def publish_inline_comment(self, body: str, relevant_file: str, relevant_line_in_file: str): edit_type, found, source_line_no, target_file, target_line_no = self.search_line(relevant_file, relevant_line_in_file) diff --git a/pr_agent/servers/gitlab_webhook.py b/pr_agent/servers/gitlab_webhook.py index a5d5a115..165cd0a3 100644 --- a/pr_agent/servers/gitlab_webhook.py +++ b/pr_agent/servers/gitlab_webhook.py @@ -64,7 +64,25 @@ async def gitlab_webhook(background_tasks: BackgroundTasks, request: Request): mr = data['merge_request'] url = mr.get('url') body = data.get('object_attributes', {}).get('note') + if data.get('object_attributes', {}).get('type') == 'DiffNote' and '/ask' in body: + line_range_ = data['object_attributes']['position']['line_range'] + + # if line_range_['start']['type'] == 'new': + start_line = line_range_['start']['new_line'] + end_line = line_range_['end']['new_line'] + # else: + # start_line = line_range_['start']['old_line'] + # end_line = line_range_['end']['old_line'] + + question = body.replace('/ask', '').strip() + path = data['object_attributes']['position']['new_path'] + side = 'RIGHT'# if line_range_['start']['type'] == 'new' else 'LEFT' + comment_id = data['object_attributes']["discussion_id"] + get_logger().info(f"Handling line comment") + body = f"/ask_line --line_start={start_line} --line_end={end_line} --side={side} --file_name={path} --comment_id={comment_id} {question}" + handle_request(background_tasks, url, body, log_context) + return JSONResponse(status_code=status.HTTP_200_OK, content=jsonable_encoder({"message": "success"}))