From 745da8e325941da5579e405cca3ca6503180df70 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sun, 26 Sep 2021 19:49:02 +0200 Subject: [PATCH] Fix: GitLab API searches always return first found match (#3400) (#3449) * fix: return correct group id match only full_path or name * chore: add changelog fragment * fix: indentation multiple of four * refactor: use two loops * fix: typo of group id * fix: changelog fragment (cherry picked from commit b6b76016151d18b8d5f085bf01260d9c9c7ee267) Co-authored-by: Chris Frage --- ...i-searches-always-return-first-found-match-3386.yml | 2 ++ .../source_control/gitlab/gitlab_group_members.py | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/3400-fix-gitLab-api-searches-always-return-first-found-match-3386.yml diff --git a/changelogs/fragments/3400-fix-gitLab-api-searches-always-return-first-found-match-3386.yml b/changelogs/fragments/3400-fix-gitLab-api-searches-always-return-first-found-match-3386.yml new file mode 100644 index 0000000000..ab13b4adba --- /dev/null +++ b/changelogs/fragments/3400-fix-gitLab-api-searches-always-return-first-found-match-3386.yml @@ -0,0 +1,2 @@ +bugfixes: + - gitlab_group_members - ``get_group_id`` return the group ID by matching ``full_path``, ``path`` or ``name`` (https://github.com/ansible-collections/community.general/pull/3400). diff --git a/plugins/modules/source_control/gitlab/gitlab_group_members.py b/plugins/modules/source_control/gitlab/gitlab_group_members.py index 50779e6445..0ac5e50041 100644 --- a/plugins/modules/source_control/gitlab/gitlab_group_members.py +++ b/plugins/modules/source_control/gitlab/gitlab_group_members.py @@ -102,9 +102,13 @@ class GitLabGroup(object): # get group id if group exists def get_group_id(self, gitlab_group): - group_exists = self._gitlab.groups.list(search=gitlab_group) - if group_exists: - return group_exists[0].id + groups = self._gitlab.groups.list(search=gitlab_group) + for group in groups: + if group.full_path == gitlab_group: + return group.id + for group in groups: + if group.path == gitlab_group or group.name == gitlab_group: + return group.id # get all members in a group def get_members_in_a_group(self, gitlab_group_id):