From 7d47bd5f5e1d96c5b53ff65c724422c13ad4ba1e Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 29 Mar 2025 19:35:39 +0300 Subject: [PATCH 1/3] Add GitLab protections to prevent quick actions in PR questions --- pr_agent/tools/pr_questions.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pr_agent/tools/pr_questions.py b/pr_agent/tools/pr_questions.py index 6f400b96..3333fb3a 100644 --- a/pr_agent/tools/pr_questions.py +++ b/pr_agent/tools/pr_questions.py @@ -9,7 +9,7 @@ from pr_agent.algo.pr_processing import get_pr_diff, retry_with_fallback_models from pr_agent.algo.token_handler import TokenHandler from pr_agent.algo.utils import ModelType from pr_agent.config_loader import get_settings -from pr_agent.git_providers import get_git_provider +from pr_agent.git_providers import get_git_provider, GitLabProvider from pr_agent.git_providers.git_provider import get_main_pr_language from pr_agent.log import get_logger from pr_agent.servers.help import HelpMessage @@ -116,10 +116,21 @@ class PRQuestions: model=model, temperature=get_settings().config.temperature, system=system_prompt, user=user_prompt) return response + def gitlab_protctions(self, model_answer: str) -> str: + github_quick_actions_MR = ["/approve", "/close", "/merge", "/reopen", "/unapprove", "/title", "/assign", + "/copy_metadata", "/target_branch"] + if any(action in model_answer for action in github_quick_actions_MR): + str_err = "Model answer contains GitHub quick actions, which are not supported in GitLab" + get_logger().error(str_err) + return str_err + def _prepare_pr_answer(self) -> str: model_answer = self.prediction.strip() # sanitize the answer so that no line will start with "/" model_answer_sanitized = model_answer.replace("\n/", "\n /") + model_answer_sanitized = model_answer_sanitized.replace("\r/", "\r /") + if isinstance(self.git_provider, GitLabProvider): + model_answer_sanitized = self.gitlab_protctions(model_answer_sanitized) if model_answer_sanitized.startswith("/"): model_answer_sanitized = " " + model_answer_sanitized if model_answer_sanitized != model_answer: From 02d9aed7fef8136fa41009a2d1a8b9634915839d Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 29 Mar 2025 19:39:56 +0300 Subject: [PATCH 2/3] Fix GitLab provider to use default branch instead of target branch for repo settings --- pr_agent/git_providers/gitlab_provider.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index 331bdf65..590aa32e 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -515,7 +515,8 @@ class GitLabProvider(GitProvider): def get_repo_settings(self): try: - contents = self.gl.projects.get(self.id_project).files.get(file_path='.pr_agent.toml', ref=self.mr.target_branch).decode() + main_branch = self.gl.projects.get(self.id_project).default_branch + contents = self.gl.projects.get(self.id_project).files.get(file_path='.pr_agent.toml', ref=main_branch).decode() return contents except Exception: return "" From 7bd0fefee43610b9a6e64c73743dc2fefbaf30c1 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 29 Mar 2025 19:53:46 +0300 Subject: [PATCH 3/3] Fix GitLab protections function and return value in PR questions --- pr_agent/tools/pr_questions.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pr_agent/tools/pr_questions.py b/pr_agent/tools/pr_questions.py index 3333fb3a..7cdb7984 100644 --- a/pr_agent/tools/pr_questions.py +++ b/pr_agent/tools/pr_questions.py @@ -116,13 +116,14 @@ class PRQuestions: model=model, temperature=get_settings().config.temperature, system=system_prompt, user=user_prompt) return response - def gitlab_protctions(self, model_answer: str) -> str: + def gitlab_protections(self, model_answer: str) -> str: github_quick_actions_MR = ["/approve", "/close", "/merge", "/reopen", "/unapprove", "/title", "/assign", "/copy_metadata", "/target_branch"] if any(action in model_answer for action in github_quick_actions_MR): str_err = "Model answer contains GitHub quick actions, which are not supported in GitLab" get_logger().error(str_err) return str_err + return model_answer def _prepare_pr_answer(self) -> str: model_answer = self.prediction.strip() @@ -130,7 +131,7 @@ class PRQuestions: model_answer_sanitized = model_answer.replace("\n/", "\n /") model_answer_sanitized = model_answer_sanitized.replace("\r/", "\r /") if isinstance(self.git_provider, GitLabProvider): - model_answer_sanitized = self.gitlab_protctions(model_answer_sanitized) + model_answer_sanitized = self.gitlab_protections(model_answer_sanitized) if model_answer_sanitized.startswith("/"): model_answer_sanitized = " " + model_answer_sanitized if model_answer_sanitized != model_answer: