community: prevent gitlab commit on main branch for Gitlab tool (#27750)

### About

- **Description:** In the Gitlab utilities used for the Gitlab tool
there is no check to prevent pushing to the main branch, as this is
already done for Github (for example here:
5a2cfb49e0/libs/community/langchain_community/utilities/github.py (L587)).
This PR add this check as already done for Github.
- **Issue:** None
- **Dependencies:** None
This commit is contained in:
Lorenzo 2024-10-30 19:50:13 +01:00 committed by GitHub
parent 0a472e2a2d
commit 3dfdb3e6fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -207,6 +207,12 @@ class GitLabAPIWrapper(BaseModel):
Returns:
str: A success or failure message
"""
if self.gitlab_branch == self.gitlab_base_branch:
return (
"You're attempting to commit directly"
f"to the {self.gitlab_base_branch} branch, which is protected. "
"Please create a new branch and try again."
)
file_path = file_query.split("\n")[0]
file_contents = file_query[len(file_path) + 2 :]
try:
@ -253,6 +259,12 @@ class GitLabAPIWrapper(BaseModel):
Returns:
A success or failure message
"""
if self.gitlab_branch == self.gitlab_base_branch:
return (
"You're attempting to commit directly"
f"to the {self.gitlab_base_branch} branch, which is protected. "
"Please create a new branch and try again."
)
try:
file_path = file_query.split("\n")[0]
old_file_contents = (
@ -299,6 +311,12 @@ class GitLabAPIWrapper(BaseModel):
Returns:
str: Success or failure message
"""
if self.gitlab_branch == self.gitlab_base_branch:
return (
"You're attempting to commit directly"
f"to the {self.gitlab_base_branch} branch, which is protected. "
"Please create a new branch and try again."
)
try:
self.gitlab_repo_instance.files.delete(
file_path, self.gitlab_branch, "Delete " + file_path