-2

我想使用 python-gitlab 更新我的 Gitlab 存储库中的多个文件。我知道如何更新单个文件,但我不想一个一个地更新每个文件。

通常,我们使用以下命令实现此目的:

  1. 进行更改
  2. git添加
  3. Git 提交
  4. git推送

有没有办法可以使用 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')
4

1 回答 1

1

从这里参考https://python-gitlab.readthedocs.io/en/stable/gl_objects/commits.html

从上面的链接看这部分

# 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)
于 2021-02-21T14:13:48.297 回答