0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Giving:

  • a Gitlab connection named gitlab_client

  • the project identifier project_id inviting the group

  • the group identifier group_id invited by the projet

you can simply use the share method:

import gitlab

gitlab_project = gl_client.projects.get(project_id)
gitlab_project.share(
    group_id=group_id,
    group_access=gitlab.const.AccessLevel.GUEST
)

You can increase the access level to REPORTER, DEVELOPER, MAINTAINER or OWNER.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.