Info-Tech

Google Apps Script for Gmail to filter and form email from Gitlab

gmail-gitlab-filtering.gs is a Google Apps Script for Gmail to form and filter email from GitLab.

Receiving all messages from a mailing record and filtering by whether I am an instantaneous recipient (in To: or Cc: ) has been an efficient procedure for me to each

  1. follow pattern at gigantic
  2. now no longer cross over requests for my input

With mailing lists, I salvage this with a aggregate of Gmail filters:

Fits: to:([email protected])
Discontinuance this: Comply with note "mattst88"

Fits: record:mesa-dev.lists.freedesktop.org
Discontinuance this: Comply with note "mesa-dev"

Fits: -{to:[email protected]}
Discontinuance this: Skip Inbox

Thus, mail from mailing lists is labeled precisely and doesn’t litter my inbox. Any messages with me in To: or Cc: cause the thread to be labeled with a non-public mattst88 note and appear in the inbox.

I truly luxuriate in now no longer been ready to replicate this with GitLab and Gmail, given the barriers of each.

Emails from GitLab comprise many headers that will even be former to filter the message. Of the headers GitLab makes use of, Gmail’s filtering system can simplest filter on Listing-Identification.

GitLab will even be configured to email updates below diverse conditions: any job, magnificent for threads you luxuriate in participated in, magnificent for comments that philosophize you, etc.

This leaves me with numerous receiving notifications magnificent for threads I’m occupied with or for all threads but without the flexibility to without anguish to find requests directed to me.

Google Apps Script supplies a process of automating many operations on a Gmail account (sending email, procuring, labeling, etc.) via the GmailApp class. The gmail-gitlab-filtering.gs script is speed every 10 minutes via a trigger on scripts.google.com and performs filtering and labeling essentially essentially based on the X-GitLab headers.

This allows me to precisely note notifications that are directed to me, to boot to dynamically salvage labels for notifications bought from unusual initiatives.

How

Electronic mail from GitLab is labeled utilizing Gmail’s default filtering with a high-level note and skips the inbox. In my case, all mail from [email protected] is given the freedesktop note:

Fits: from:([email protected])
Discontinuance this: Skip Inbox, Comply with note "freedesktop"

Threads with this note are regarded as unprocessed.

The gmail-gitlab-filtering.gs script searches for threads with that note, and inspects the headers of each message utilizing the GmailMessage.getHeader() characteristic.

The script records the value of all X-GitLab-Mission-Route headers and whether any message in the thread contained a X-GitLab-NotificationReason header.

Threads containing a X-GitLab-NotificationReason header purchase the personal note and are moved to the inbox. Threads without X-GitLab-NotificationReason header remain archived and the personal note is removed. All threads are labeled with ${unprocessedLabel}/${X-GitLab-Mission-Route}, and the note is dynamically created if wanted. The unprocessed note is persistently removed as the closing step, in case the script’s runtime exceeds the allowed timelimit (see below).

As an instance, if I luxuriate in been to collect a notification of a merge question in the mesa/shader-db mission that mentioned me, the script would transfer the thread to the inbox, plod away the mattst88 note intact, note the mail freedesktop/mesa/shader-db (growing the note if wanted), and purchase away the freedesktop note.

Implementation notes and barriers

No longer regarded as one of many barriers cause issues for my utilization. I gather a pair of hundred GitLab notifications per day.

Google Apps Scripts are self-discipline to quotas.

Whereas growing this script, I hit two quotas:

  • Script runtime: 6 min / execution
  • Electronic mail read/write (aside from send): 20,000 / day

To suit into the 6 minutes / execution limit, the script operates on a maximum of 240 threads per execution. Glimpse the maxThreads variable. I arrived at the 240 number empirically; it is now no longer definitive.

I reached the 20,000 / day email read/write quota for the length of pattern and request to never attain all of it every other time now that the script functions.

To provide a employ to efficiency (and presumably steer positive of hitting quotas), the script creates lists of GmailThreads to allow them to also be passed in batches to GmailApp.moveThreadsToInbox, GmailLabel.addToThreads, and GmailLabel.removeFromThreads †. It be unclear to me which functions use quota and how significant they use.

† These functions gather simplest 100 threads per call, so the script calls them a pair of events on .gash()s of the lists.

Google Apps Scripts don’t purple meat up async/wait for

I before the entirety although I’d be ready to present a employ to efficiency of the script by utilizing async/wait for. Whereas the V8 Runtime acknowledges the principle phrases it doesn’t salvage use of them. An upstream worm is filed right here.

I stumbled on a attention-grabbing work-round: Async.gs. It works by scheduling a script execution in the kill. I didn’t experiment with it.

  1. Develop a mission on script.google.com
  2. Paste gmail-gitlab-filtering.gs into the default Code.gs file
  3. Regulate as wanted for your particular filtering principles
  4. Residing up a trigger to speed the script periodically. I adopted the instructions from Marcin Jasion‘s Learn the intention to note GitLab notification in Gmail by headers? article.

I muddled by writing the script with the help of the Apps Script documentation and the Mozilla Net Doctors JavaScript Reference. Enhancements to the code are welcome.

Content Protection by DMCA.com

Back to top button