pythongitlabpython-gitlab

Python Gitlab API invite group to project


I'm trying to add a gitlab group to a project after the project is created. I can see in the Gitlab API documentation that it's possible to invite a MEMBER to a group to or a project (https://docs.gitlab.com/ee/api/invitations.html)

It's however possible to invite a group to a project from the Gitlab Dashboard when navigating the Members menu, under Project information.

enter image description here

How would I go about adding a group to a project after it is created using the python-gitlab API?


Solution

  • I found the solution at https://python-gitlab.readthedocs.io/en/stable/gl_objects/projects.html#project-members

    It is important to note the Gitlab have two types for projects as stated in 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)
    

    To share the project with a group you need the manageable_project object.

    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)
    

    The result: enter image description here