Skip to content

Commit

Permalink
Fetch all projects, not just the first 100. (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Steffen Allner <[email protected]>
  • Loading branch information
icemac and sallner authored Dec 5, 2023
1 parent 193530d commit 3c04903
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ Change log
----------------

- Prefer exact matches in case of multiple ones.

- Fetch all projects, not just the first 100.
12 changes: 9 additions & 3 deletions gtimelog2tick.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,15 @@ def read_config(config_file: pathlib.Path) -> dict:
'midnight': midnight,
}

raw_projects = call(config, 'get', '/projects.json')
tick_projects = [Project(x['name'], x['id'], None)
for x in raw_projects]
page = 1
tick_projects = []
while True:
raw_projects = call(config, 'get', f'/projects.json?page={page}')
if not raw_projects:
break
tick_projects.extend(
[Project(x['name'], x['id'], None) for x in raw_projects])
page += 1
config['tick_projects'] = tick_projects
return config

Expand Down
12 changes: 11 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def __init__(self, mock, user='User Name'):
self.idseq = map(str, itertools.count(1))
self.dtformat = '%Y-%m-%dT%H:%M:%S%z'
self.routes = {
'get /projects.json': Route(self.list_projects),
r'get /projects.json\?page={page}': Route(self.list_projects, {
'page': r'[0-9]+'
}),
'get /projects/{id}/tasks.json': Route(self.list_tasks, {
'id': r'[0-9]+',
}),
Expand Down Expand Up @@ -130,6 +132,14 @@ def _add_worklog(self, user, started, seconds, comment):

def list_projects(self, request, context):
context.headers['content-type'] = 'application/json'
page, = self._get_url_params(
request, r'get /projects.json\?page={page}')

if int(page) > 1:
# We do not have multiple pages in the fixtures,
# but as we pass the page parameter all the time
# it should be fine.
return []

return [
{
Expand Down

0 comments on commit 3c04903

Please sign in to comment.