diff --git a/bugwarrior/docs/services/gmail.rst b/bugwarrior/docs/services/gmail.rst index f9725953..c7847813 100644 --- a/bugwarrior/docs/services/gmail.rst +++ b/bugwarrior/docs/services/gmail.rst @@ -41,6 +41,17 @@ You do not need to specify the ``login_name``, but it can be useful to avoid accidentally fetching data from the wrong account. (This also allows multiple targets with the same login to share the same authentication token.) +Thread Limit +----------------- + +Gmail search results are capped at 100 max results by default, but may be +adjusted to meet your needs by specifying ``email_limit``: + +.. config:: + :fragment: gmail + + gmail.thread_limit = 1000 + Authentication -------------- diff --git a/bugwarrior/services/gmail.py b/bugwarrior/services/gmail.py index 8141639d..fc9ccdbe 100644 --- a/bugwarrior/services/gmail.py +++ b/bugwarrior/services/gmail.py @@ -24,6 +24,7 @@ class GmailConfig(config.ServiceConfig): '~/.gmail_client_secret.json') query: str = 'label:Starred' login_name: str = 'me' + thread_limit: int = 100 class GmailIssue(Issue): @@ -178,12 +179,24 @@ def get_labels(self): def get_threads(self): thread_service = self.gmail_api.users().threads() + threads = [] - result = thread_service.list( - userId=self.config.login_name, q=self.config.query).execute() - return [ - thread_service.get(userId='me', id=thread['id']).execute() - for thread in result.get('threads', [])] + pageToken = None + + while len(threads) < self.config.thread_limit: + maxResults = min(100, self.config.thread_limit - len(threads)) + + result = thread_service.list(userId=self.config.login_name, q=self.config.query, + maxResults=maxResults, pageToken=pageToken).execute() + + for thread in result.get('threads', []): + threads.append(thread_service.get(userId='me', id=thread['id']).execute()) + + pageToken = result.get('nextPageToken', None) + if not pageToken: + break + + return threads def annotations(self, issue): sender = issue.extra['last_sender_name']