Skip to content

Commit

Permalink
gmail: add pagination support
Browse files Browse the repository at this point in the history
Co-authored-by: ryneeverett <[email protected]>
  • Loading branch information
mdujava and ryneeverett committed Feb 22, 2024
1 parent bfe238a commit f6e9dc3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 11 additions & 0 deletions bugwarrior/docs/services/gmail.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------

Expand Down
23 changes: 18 additions & 5 deletions bugwarrior/services/gmail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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']
Expand Down

0 comments on commit f6e9dc3

Please sign in to comment.