Skip to content

Commit f3d7074

Browse files
committed
Add regex project endpoint
Since the project query parameter is unable to search by regex, add it explicity. This addition makes it easier to narrow down project results where the regular query option "inname" is insufficent.
1 parent 1a4e71d commit f3d7074

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

gerrit/projects/projects.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ def search(self, query):
4343
result = self.gerrit.decode_response(response)
4444
return GerritProject.parse_list(result, gerrit=self.gerrit)
4545

46+
def regex(self, query):
47+
"""
48+
Regex queries projects visible to the caller.
49+
The query string must be provided by the query parameter.
50+
The start and limit parameters can be used to skip/limit results.
51+
52+
query parameter
53+
* Boundary matchers '^' and '$' are implicit.
54+
* For example: the regex 'test.*' will match any projects that start with 'test' and regex
55+
'.*test' will match any project that end with 'test'.
56+
* The match is case sensitive.
57+
58+
:param query:
59+
:return:
60+
"""
61+
endpoint = "/projects/?r=%s" % query
62+
response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
63+
result = self.gerrit.decode_response(response)
64+
return GerritProject.parse_dict(result, gerrit=self.gerrit)
65+
4666
def get(self, project_name):
4767
"""
4868
Retrieves a project.

gerrit/utils/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ def parse_list(cls, data, **kwargs):
3939
results.append(cls.parse(obj, **kwargs))
4040
return results
4141

42+
@classmethod
43+
def parse_dict(cls, data, **kwargs):
44+
"""Parse a dict of JSON objects into a result set of model instances."""
45+
results = ResultSet()
46+
data = data or []
47+
for obj in data.keys():
48+
if obj:
49+
results.append(cls.parse(data[obj], **kwargs))
50+
return results
51+
4252
def __repr__(self):
4353
key = self.attributes[0]
4454
value = getattr(self, key)

0 commit comments

Comments
 (0)