From 7d47bd5f5e1d96c5b53ff65c724422c13ad4ba1e Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 29 Mar 2025 19:35:39 +0300 Subject: [PATCH] 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: