From 3dfdb3e6fb7bb25683f9675ddd0a6739655d88eb Mon Sep 17 00:00:00 2001 From: Lorenzo <70835436+lorenzb07@users.noreply.github.com> Date: Wed, 30 Oct 2024 19:50:13 +0100 Subject: [PATCH] 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: 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 --- .../langchain_community/utilities/gitlab.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libs/community/langchain_community/utilities/gitlab.py b/libs/community/langchain_community/utilities/gitlab.py index 1dd5894fe7..2a1d759afa 100644 --- a/libs/community/langchain_community/utilities/gitlab.py +++ b/libs/community/langchain_community/utilities/gitlab.py @@ -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