From 9572caf473b4a60976c71e46570ee0901e7fff27 Mon Sep 17 00:00:00 2001 From: David Greaves Date: Fri, 25 Sep 2015 11:34:25 +0000 Subject: [PATCH] Add a search_users method Signed-off-by: David Greaves --- gitlab/__init__.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 2fbe2fa35..9be168d73 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -600,6 +600,35 @@ def User(self, id=None, **kwargs): """ return User._get_list_or_object(self, id, **kwargs) + def _list_users(self, url, exact, **kwargs): + r = self.rawGet(url, **kwargs) + if r.status_code != 200: + _raiseErrorFromResponse(r, GitlabListError) + + l = [] + for o in r.json(): + u=User(self, o) + if exact: + if u.username == exact: + return u + else: + l.append(u) + + if exact: + return None + return l + + def search_users(self, query, exact, **kwargs): + """Searches users by name. + + Returns a list of matching users. + + If 'exact' is true then only return one user with an exact match for the query + """ + if exact: + exact=query + return self._list_users("/users?search=" + query, exact, **kwargs) + def Team(self, id=None, **kwargs): """Creates/gets/lists team(s) known by the GitLab server.