I found https://github.com/python-gitlab/python-gitlab, but I was unable to understand the examples in the doc.
Advertisement
Answer
That’s right there are no tests we can find in the doc. Here’s a basic answer for your question.
If you would like a complete working script, I have attached it here: https://github.com/torpidsnake/common_scripts/blob/main/automation_to_create_push_merge_in_gitlab/usecase_gitlab_python.py
Breaking down the steps below:
Create an authkey for you: Follow the steps here: https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
Create a gitlab server instance of your project
server = gitlab.Gitlab('https://gitlab.example.com', private_token=YOUR_API_TOKEN)
project = server.projects.get(PROJECT_ID)
Create a branch using:
branch = project.branches.create(
{"branch": branch_name, "ref": project.default_branch}
)
Upload a file using:
project.files.create(
{
"file_path": file_name,
"branch": branch.name,
"content": "data to be written",
"encoding": "text", # or 'base64'; useful for binary files
"author_email": AUTHOR_EMAIL, # Optional
"author_name": AUTHOR_NAME, # Optional
"commit_message": "Create file",
}
)
Create a merge request using:
project.mergerequests.create(
{
"source_branch": branch.name,
"target_branch": project.default_branch,
"title": "merge request title",
}
)