1

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': 'master',
        '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()
Sn_Srm
  • 101
  • 1
  • 8

1 Answers1

2

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.

kwiknik
  • 570
  • 3
  • 7
  • Both branches exist actually- I edited the code to avoid confusion though – Sn_Srm Sep 16 '22 at 13:38
  • In that case, I suspect there's some detail causing the issue that's not apparent in what you've posted. Based on what you've shared, there's no reason the code wouldn't work when functionalised. – kwiknik Sep 16 '22 at 14:08