python-gitlab - 如何修改项目设置?

发布于 2025-01-12 22:27:15 字数 564 浏览 4 评论 0原文

我正在编写一个 python 模块来在 gitlab 中创建项目,但我不知道如何更改默认项目设置,例如 remove_source_branch_after_merge

我尝试将参数传递给 projects.create() 调用,但它似乎被忽略。

project = gl.projects.create({'name': reponame, 'namespace_id': group_id, 'default_branch' : default_branch, 'remove_source_branch_after_merge' : False})

我设法通过手动 POST 到 /api/v4/projects/$ID?remove_source_branch_after_merge=false 来更改设置,但我不知道如何在 python-gitlab 中执行此操作。

如何创建具有自定义设置的项目,或在 python-gitlab 中创建项目后修改项目设置?

我正在使用 python-gitlab==1.7.0

I'm writing a python module to create projects in gitlab, but I can't figure out how to change the default project settings, like remove_source_branch_after_merge.

I've tried passing the argument to the projects.create() call, but it seems to be ignored.

project = gl.projects.create({'name': reponame, 'namespace_id': group_id, 'default_branch' : default_branch, 'remove_source_branch_after_merge' : False})

I managed to change the setting by manually POST'ing to /api/v4/projects/$ID?remove_source_branch_after_merge=false but I can't figure out how to do this in python-gitlab.

How can I create a project with customized settings, or modify a project settings after its creation in python-gitlab?

I'm using python-gitlab==1.7.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2025-01-19 22:27:15

要回答您的问题,可以使用 save() 在创建对象后修改属性:

import gitlab

gl = gitlab.Gitlab("https://gitlab.example.com", private_token=token)
project = gl.projects.create(
    {
        "name": reponame,
        "namespace_id": group_id,
        "default_branch": default_branch,
        "remove_source_branch_after_merge": False,
    }
)

# Enable remove after merge
project.remove_source_branch_after_merge = True
project.save()

# Or disable again
project.remove_source_branch_after_merge = False
project.save()

但是,我认为您的初始创建调用应该工作,所以也许检查任何拼写错误。 1.7.0 已经很老了,我刚刚检查过它在 3.2.0 上是否有效。您还可以使用 gl.enable_debug() 获取详细输出并检查是否将正确的参数发送到 API。

To answer your question, modifying attributes after an object has been created can be done with save():

import gitlab

gl = gitlab.Gitlab("https://gitlab.example.com", private_token=token)
project = gl.projects.create(
    {
        "name": reponame,
        "namespace_id": group_id,
        "default_branch": default_branch,
        "remove_source_branch_after_merge": False,
    }
)

# Enable remove after merge
project.remove_source_branch_after_merge = True
project.save()

# Or disable again
project.remove_source_branch_after_merge = False
project.save()

However, I think your initial create call should work, so maybe check for any typos. 1.7.0 is quite old, and I just checked this works on 3.2.0. You can also use gl.enable_debug() to get verbose output and check that the right parameters are sent to the API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文