python gitlab example
See Library page](https://python-gitlab.readthedocs.io/en/stable/index.html)
Create branch
py
def create_branch(self, project: str, from_branch: str, branch_name: str) -> object:
""" Create a new branch in gitlab repository.
:param project: Gitlab project name.
:param from_branch: Gitlab branch name.
:param branch_name: Gitlab branch name.
:return: Gitlab branch instance.
"""
project = self.get_project(project)
try:
return project.branches.create({'branch': branch_name, 'ref': from_branch})
except GitlabCreateError as message:
if 'Branch already exists' not in message.error_message:
raise GitlabCreateError(message)
return project.branches.get(branch_name)
Connection
py
def _connect(self):
""" Get gitlab connection
:return: Gitlab instance
:rtype: gitlab.client.Gitlab
"""
return gitlab.Gitlab(GITLAB_URL, private_token=self._get_personal_access_token())
Commit
py
def _commit(self, project: str, branch_name: str, commit_msg: str, actions: list[dict]) -> object:
""" Create a new commit in gitlab repository.
:param project: Gitlab project name.
:param branch_name: Gitlab branch name.
:param commit_msg: Commit message.
:param actions: List of actions to perform.
:return: Gitlab commit instance.
"""
if isinstance(project, str):
project = self.get_project(project)
data = {
'branch': branch_name,
'commit_message': commit_msg,
'actions': actions
}
return project.commits.create(data)
Delete branch
py
def delete_branch(self, project: str, branch_name: str) -> object:
""" Delete a branch in gitlab repository.
:param project: Gitlab project name.
:param branch_name: Gitlab branch name.
:return: Gitlab branch instance.
"""
return self.get_project(project).branches.delete(branch_name)
Get file content from gitlab
py
def get_file_content_from_gitlab(self,
project: str,
branch_name: str,
file_path: str,
decode_type: str ='utf-8'
) -> str:
""" Get file content from gitlab repository.
:param project: Gitlab project name.
:param branch_name: Gitlab branch name.
:param file_path: File path.
:param decode_type: File decode type.
:return: File content as utf8.
"""
return self.get_project(project).files.get(
file_path=file_path,
ref=branch_name
).decode().decode(decode_type)
Create merge request
py
def create_merge_request(
self,
project: str,
source_branch_name: str,
target_branch_name: str,
title: str,
description: str,
remove_source_branch: bool = True
) -> object:
""" Create a merge request in gitlab repository.
:param project: Gitlab project name.
:param source_branch_name: Gitlab branch name.
:param target_branch_name: Gitlab branch name.
:param title: Merge request title.
:param description: Merge request description.
:param remove_source_branch: Remove source branch after merge.
:return: Gitlab merge request instance.
"""
project = self.get_project(project)
data = {
'source_branch': source_branch_name,
'target_branch': target_branch_name,
'title': title,
'description': description,
'remove_source_branch': remove_source_branch
}
return project.mergerequests.create(data)
Close merge request
py
def close_merge_request(self, project: str, merge_request_id: int) -> object:
""" Close a merge request in gitlab repository.
:param project: Gitlab project name.
:param merge_request_id: Merge request id.
:return: Gitlab merge request instance.
"""
project = self.get_project(project)
mr = project.mergerequests.get(merge_request_id)
# close merge request
mr.state_event = 'close'
mr.save()
return mr
File exists in gitlab
py
def file_exists_in_gitlab(self, project: str, branch_name: str, file_path: str) -> bool:
""" Check if file exists in gitlab repository.
:param project: Gitlab project name.
:param branch_name: Gitlab branch name.
:param file_path: File path.
:return: True if file exists.
"""
try:
self.get_file_content_from_gitlab(project, branch_name, file_path)
return True
except Exception:
return False
Replace file from another repo
py
def replace_file_from_another_repo(
self,
source_project: str,
source_branch_name: str,
destination_project: str,
destination_branch_name: str,
source_file_name: str,
destination_file_name: str,
old_destination_file_name: str = None
) -> None:
""" Rename a file in gitlab repository.
:param source_project: Gitlab project name.
:param source_branch_name: Gitlab branch name.
:param destination_project: Gitlab project name.
:param destination_branch_name: Gitlab branch name.
:param source_file_name: File path.
:param destination_file_name: File path.
:param old_destination_file_name: File path.
:return: None
"""
raw_content = self.get_file_content_from_gitlab(
project=source_project,
branch_name=source_branch_name,
file_path=source_file_name
)
commit_msg = f'Create {destination_file_name} from {source_project}/{source_branch_name}/{source_file_name}' \
f' and remove {old_destination_file_name}.'
# test if file exist in repository gitlab
if not self.file_exists_in_gitlab(
project=destination_project,
branch_name=destination_branch_name,
file_path=old_destination_file_name,
):
return
self._commit(
project=destination_project,
branch_name=destination_branch_name,
commit_msg=commit_msg,
actions=[{
'action': 'create',
'file_path': destination_file_name,
'content' : raw_content
},
{
'action': 'delete',
'file_path': old_destination_file_name
}
])