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
JavaScript
x
3
1
server = gitlab.Gitlab('https://gitlab.example.com', private_token=YOUR_API_TOKEN)
2
project = server.projects.get(PROJECT_ID)
3
Create a branch using:
JavaScript
1
4
1
branch = project.branches.create(
2
{"branch": branch_name, "ref": project.default_branch}
3
)
4
Upload a file using:
JavaScript
1
12
12
1
project.files.create(
2
{
3
"file_path": file_name,
4
"branch": branch.name,
5
"content": "data to be written",
6
"encoding": "text", # or 'base64'; useful for binary files
7
"author_email": AUTHOR_EMAIL, # Optional
8
"author_name": AUTHOR_NAME, # Optional
9
"commit_message": "Create file",
10
}
11
)
12
Create a merge request using:
JavaScript
1
8
1
project.mergerequests.create(
2
{
3
"source_branch": branch.name,
4
"target_branch": project.default_branch,
5
"title": "merge request title",
6
}
7
)
8