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 aa45f50
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 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'
email_limit: int = 100


class GmailIssue(Issue):
Expand Down Expand Up @@ -178,12 +179,28 @@ 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.email_limit:
maxResults = min(100, max(self.config.email_limit,
self.config.email_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[:self.config.email_limit]

def annotations(self, issue):
sender = issue.extra['last_sender_name']
Expand Down

0 comments on commit aa45f50

Please sign in to comment.