Skip to content

Commit

Permalink
gmail: add pagination support
Browse files Browse the repository at this point in the history
  • Loading branch information
mdujava committed Feb 13, 2024
1 parent bfe238a commit 220c421
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
4 changes: 4 additions & 0 deletions bugwarrior/docs/services/gmail.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Here's an example of a gmail target:
service = gmail
gmail.query = label:action OR label:readme
gmail.login_name = [email protected]
gmail.thread_limit = 100

The specified query can be any gmail search term. By default it will select
starred threads. One task is created per selected thread, not per e-mail.
Expand All @@ -41,6 +42,9 @@ 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.)

By default, the last 100 threads matching the query will be returned. You may
adjust the number of returned threads with ``gmail.thread_limit``.

Authentication
--------------

Expand Down
26 changes: 21 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,27 @@ 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())

if len(result.get('threads', [])) == 0:
break

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 220c421

Please sign in to comment.