Python Gitlab API 邀请群组加入项目

Python Gitlab API invite group to project

我正在尝试在项目创建后将 gitlab 组添加到项目中。我可以在 Gitlab API 文档中看到可以邀请成员加入组或项目 (https://docs.gitlab.com/ee/api/invitations.html)

但是,在导航“项目信息”下的“成员”菜单时,可以从 Gitlab 仪表板邀请一个组加入项目。

在使用 python-gitlab API 创建项目后,我该如何将组添加到项目中?

我在 https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html#project-members

找到了解决方案

重要的是要注意 Gitlab 有两种类型的项目,如 https://python-gitlab.readthedocs.io/en/stable/gl_objects/groups.html#group-members

中所述

GroupProject objects returned by this API call are very limited, and do not provide all the features of Project objects. If you need to manipulate projects, create a new Project object:

first_group_project = group.projects.list()[0]

manageable_project = gl.projects.get(first_group_project.id, lazy=True)

要与群组共享项目,您需要 manageable_project 对象。

gl = gitlab.Gitlab(settings.GITLABURL, settings.GITLABTOKEN, api_version=4, ssl_verify=False)

gitlab_group = self.gl.groups.list(search="group_name")[0]
project_object = self.gl.projects.get(gitlab_group.id, lazy=True)

project_object.share(project_object.id, gitlab.MAINTAINER_ACCESS)

结果: