From 9e19286a9fd3650815ae4df38d60a2c54edd6de8 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 30 Oct 2021 08:31:53 +0200 Subject: [PATCH] gitlab_project_members: improve project name matching (#3602) (#3635) * Update gitlab_project_members.py The actual search method doesn't accept path with namespace for project_name. If you have many project with same name, this module gitlab_project_members can't work. * Update gitlab_project_members.py * Update gitlab_project_members.py * Update gitlab_project_members.py * Create 3602-fix-gitlab_project_members-improve-search-method * Rename 3602-fix-gitlab_project_members-improve-search-method to 3602-fix-gitlab_project_members-improve-search-method.yml (cherry picked from commit cdfc4dcf498e6fdf8f10922480556cee2c3ad3eb) Co-authored-by: paytroff <93038288+paytroff@users.noreply.github.com> --- ...-gitlab_project_members-improve-search-method.yml | 2 ++ .../source_control/gitlab/gitlab_project_members.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/3602-fix-gitlab_project_members-improve-search-method.yml 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)