How to create a new branch, push a text file and send merge request to a gitlab repository using Python?

Question:

I found
https://github.com/python-gitlab/python-gitlab, but I was unable to understand the examples in the doc.

Asked By: LearnerJS

||

Answers:

Looking at python-gitlab, I don’t see some of the things you are looking for. In that case, I suggest you break it apart and do the individual steps using more basic tools and libraries.

The first two parts you don’t need to use Gitlab API to do. You can basically use Python to do the clone, branch, edit, and commit calls using git.exe and against your disk. In some ways, that is easier since you can duplicate the calls yourself. You could use GitPython.

I’d recommend you do that through one of these methods instead of trying to do it via Gitlab API. It’s easier to understand, debug, and investigate if you do the branch work locally (or even inside a CI).

Once you push up the code into a branch, you can use Gitlab’s API to create a merge request via REST (such as the requests library). The description for creating the MR is at https://docs.gitlab.com/ee/api/merge_requests.html#create-mr and most of the fields are optional so the minimum looks like:

{
  "id": "some-user%2Fsome-project",
  "source_branch": "name_of_your_mr_branch",
  "target_branch": "main",
  "title": "Automated Merge Request..."
}

This is an authenticated POST call (to create). Between those links, you should have most of what you need to do this.

Answered By: dmoonfire

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",
    }
)
Answered By: Shashwat
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.