Add GitLab protections to prevent quick actions in PR questions

This commit is contained in:
mrT23
2025-03-29 19:35:39 +03:00
parent ddf94c14a3
commit 7d47bd5f5e

View File

@ -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: