简体   繁体   中英

Python Gitlab commit functionality works from main but the same thing doesn't work when put into a function

I have been trying to make a git commit using gitlab python module and this code works fine when executed from main -

private_token = "xxxxxx"
gl = gitlab.Gitlab('https://gitlab.xyz.net/', private_token)
project_id = 10000
my_project = gl.projects.get(project_id)

data = {
    'branch': 'master',
    'commit_message': 'Commit message 1',
    'actions': [
        {
            'action': 'create',
            'file_path': 'gitUpload.txt',
            'content': "Hello this is upload test",
        },

    ]
}
commit = my_project.commits.create(data)

When I create a function for the same code and call it from main, it gives me the following error -

gitlab.exceptions.GitlabCreateError: 400: You can only create or edit files when you are on a branch

def create_commit():
    private_token = 'xxxxxx'
    gl = gitlab.Gitlab('https://gitlab.xyz.net/', private_token)
    project_id = 10000
    my_project = gl.projects.get(project_id)

    data = {
        'branch': 'main',
        'commit_message': 'Commit message 2',
        'actions': [
            {
                'action': 'create',
                'file_path': 'gitUpload.txt',
                'content': 'Hello this is a test2',
            },

        ]
    }
    commit = my_project.commits.create(data)    


create_commit()

You specify master branch in the first example but main in the second.

Gitlab recently switched from a default branch name of master to become main .

Only one exists in a given project, unless you've explicitly also created the other.

So the solution is to modify your second example to state master instead of main .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM