【问题标题】:Update multiple files python-gitlab更新多个文件 python-gitlab
【发布时间】:2021-05-07 10:57:49
【问题描述】:

我想使用 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')

【问题讨论】:

  • 对您更改的每个文件重复第 2 步怎么样?
  • 我觉得我的问题不太清楚,我已经添加了使用 python-gitlab 更新单个文件的代码

标签: python git gitlab python-gitlab


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2022-10-14
    • 2020-09-06
    • 1970-01-01
    • 2022-10-21
    相关资源
    最近更新 更多