0

I want to update multiple files in my Gitlab repository using python-gitlab. I know how to update a single file but I do not want to update each file one by one.

Normally, we achieve this using these commands:

  1. Make your changes
  2. Git add
  3. Git commit
  4. Git push

Is there a way I can achieve this using python-gitlab?

# update a single file    
file_path = "/tmp/test1.txt"
gl = gitlab.Gitlab("https://gitlab.example.com/", private_token=access_token)
project = gl.projects.get(3278)
file = project.files.get(file_path=file_path, ref="PR")
file.content = 'new content'
file.save(branch='PR', commit_message='Update testfile')

1 Answers1

3

Took reference from here https://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html

Look at this part from above link

# See https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
# for actions detail
data = {
    'branch': 'master',
    'commit_message': 'blah blah blah',
    'actions': [
        {
            'action': 'create',
            'file_path': 'README.rst',
            'content': open('path/to/file.rst').read(),
        },
        {
            # Binary files need to be base64 encoded
            'action': 'create',
            'file_path': 'logo.png',
            'content': base64.b64encode(open('logo.png').read()),
            'encoding': 'base64',
        }
    ]
}

commit = project.commits.create(data)