Skip to content

Commit

Permalink
Add regex project endpoint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
DanSoQt committed Jul 22, 2021
1 parent 1a4e71d commit f3d7074
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gerrit/projects/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ def search(self, query):
result = self.gerrit.decode_response(response)
return GerritProject.parse_list(result, gerrit=self.gerrit)

def regex(self, query):
"""
Regex queries projects visible to the caller.
The query string must be provided by the query parameter.
The start and limit parameters can be used to skip/limit results.
query parameter
* Boundary matchers '^' and '$' are implicit.
* For example: the regex 'test.*' will match any projects that start with 'test' and regex
'.*test' will match any project that end with 'test'.
* The match is case sensitive.
:param query:
:return:
"""
endpoint = "/projects/?r=%s" % query
response = self.gerrit.requester.get(self.gerrit.get_endpoint_url(endpoint))
result = self.gerrit.decode_response(response)
return GerritProject.parse_dict(result, gerrit=self.gerrit)

def get(self, project_name):
"""
Retrieves a project.
Expand Down
10 changes: 10 additions & 0 deletions gerrit/utils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def parse_list(cls, data, **kwargs):
results.append(cls.parse(obj, **kwargs))
return results

@classmethod
def parse_dict(cls, data, **kwargs):
"""Parse a dict of JSON objects into a result set of model instances."""
results = ResultSet()
data = data or []
for obj in data.keys():
if obj:
results.append(cls.parse(data[obj], **kwargs))
return results

def __repr__(self):
key = self.attributes[0]
value = getattr(self, key)
Expand Down

0 comments on commit f3d7074

Please sign in to comment.