Skip to content

Commit

Permalink
community: prevent gitlab commit on main branch for Gitlab tool (#27750)
Browse files Browse the repository at this point in the history
### 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:
https://github.com/lorenzb07/langchain/blob/5a2cfb49e045988d290a1c7e3a0c589d6b371694/libs/community/langchain_community/utilities/github.py#L587).
This PR add this check as already done for Github.
- **Issue:** None
- **Dependencies:** None
  • Loading branch information
lorenzb07 authored Oct 30, 2024
1 parent 0a472e2 commit 3dfdb3e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions libs/community/langchain_community/utilities/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ def create_file(self, file_query: str) -> str:
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:
Expand Down Expand Up @@ -253,6 +259,12 @@ def update_file(self, file_query: str) -> str:
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 = (
Expand Down Expand Up @@ -299,6 +311,12 @@ def delete_file(self, file_path: str) -> str:
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
Expand Down

0 comments on commit 3dfdb3e

Please sign in to comment.