diff --git a/changelogs/fragments/3602-fix-gitlab_project_members-improve-search-method.yml b/changelogs/fragments/3602-fix-gitlab_project_members-improve-search-method.yml new file mode 100644 index 0000000000..4d22049473 --- /dev/null +++ b/changelogs/fragments/3602-fix-gitlab_project_members-improve-search-method.yml @@ -0,0 +1,2 @@ +bugfixes: + - gitlab_project_members - ``get_project_id`` return the project id by matching ``full_path`` or ``name`` (https://github.com/ansible-collections/community.general/pull/3602). diff --git a/plugins/modules/source_control/gitlab/gitlab_project_members.py b/plugins/modules/source_control/gitlab/gitlab_project_members.py index 0ae8f4b25c..38cc7a80ec 100644 --- a/plugins/modules/source_control/gitlab/gitlab_project_members.py +++ b/plugins/modules/source_control/gitlab/gitlab_project_members.py @@ -48,7 +48,7 @@ options: type: str project: description: - - The name of the GitLab project the member is added to/removed from. + - The name (or full path) of the GitLab project the member is added to/removed from. required: true type: str gitlab_user: @@ -118,9 +118,13 @@ class GitLabProjectMembers(object): self._gitlab = gl def get_project(self, project_name): - project_exists = self._gitlab.projects.list(search=project_name) - if project_exists: - return project_exists[0].id + try: + project_exists = self._gitlab.projects.get(project_name) + return project_exists.id + except gitlab.exceptions.GitlabGetError as e: + project_exists = self._gitlab.projects.list(search=project_name) + if project_exists: + return project_exists[0].id def get_user_id(self, gitlab_user): user_exists = self._gitlab.users.list(username=gitlab_user)