Skip to content

Conversation

michelle0927
Copy link
Collaborator

@michelle0927 michelle0927 commented Feb 17, 2025

Resolves #15608

Summary by CodeRabbit

  • New Features
    • Gmail integration updated to the latest versions for enhanced performance.
    • Email filtering now supports multiple labels, allowing for broader and more precise processing.
    • Introduced options to exclude specific labels for refined control over incoming messages.

Copy link

vercel bot commented Feb 17, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Skipped Deployments
Name Status Preview Comments Updated (UTC)
docs-v2 ⬜️ Ignored (Inspect) Visit Preview Feb 17, 2025 5:36pm
pipedream-docs ⬜️ Ignored (Inspect) Feb 17, 2025 5:36pm
pipedream-docs-redirect-do-not-edit ⬜️ Ignored (Inspect) Feb 17, 2025 5:36pm

Copy link
Contributor

coderabbitai bot commented Feb 17, 2025

Walkthrough

This pull request updates the Gmail integration across several components. The package version in package.json is bumped from 0.2.0 to 0.2.1. In multiple source files, support for handling multiple labels is introduced by renaming the singular label property to a labels array. The filtering logic is enhanced to iterate over and process multiple label IDs, and an excludeLabels property is added where appropriate. These changes affect the polling history, new email receipt, and new labeled email components.

Changes

File(s) Change Summary
components/gmail/package.json Updated package version of @pipedream/gmail from 0.2.0 to 0.2.1.
components/gmail/sources/common/polling-history.mjs Modified methods (getHistoryId, emitHistories, emitRecentMessages, and emitFullMessage) to support multiple labels using an array (this.labels and this.labelIds) and to filter out messages based on this.excludeLabels.
components/gmail/sources/new-email-received/new-email-received.mjs
components/gmail/sources/new-labeled-email/new-labeled-email.mjs
Updated version numbers (from 0.1.9 to 0.1.10 and from 0.0.6 to 0.0.7 respectively), renamed the label property to labels (supporting multiple values), added the excludeLabels property in one file, and refactored message filtering and metadata generation logic to accommodate multiple labels.

Sequence Diagram(s)

sequenceDiagram
    participant GmailComponent as Gmail Component
    participant PollingModule as Polling-History Module
    participant GmailAPI as Gmail API

    GmailComponent->>PollingModule: Call getHistoryId()
    PollingModule->>GmailAPI: Request history for each label in this.labels
    GmailAPI-->>PollingModule: Return history responses per label
    PollingModule->>PollingModule: Iterate responses to calculate maxHistoryId
    PollingModule-->>GmailComponent: Return updated maxHistoryId
Loading
sequenceDiagram
    participant NewEmail as New Email Module
    participant GmailAPI as Gmail API

    NewEmail->>GmailAPI: Call getHistoryResponses(startHistoryId) for each label
    GmailAPI-->>NewEmail: Return array of history responses
    NewEmail->>NewEmail: Filter messages using labels and excludeLabels
    NewEmail->>NewEmail: Determine latest historyId from responses
Loading

Possibly related PRs

  • Gmail - updates to new-email-received trigger #14364: The changes in the main PR, which update the version of the @pipedream/gmail package, are related to the retrieved PR that also updates the version of the same package, indicating a direct connection at the code level.
  • Gmail - Improve New Email Received event shape #13992: The changes in the main PR are related to the modifications in the components/gmail/sources/common/polling-history.mjs file, as both PRs involve updates to the same file and enhance the handling of message history and labels.
  • Gmail - Update verify-client-id #13959: The changes in the main PR are related to the updates in the components/gmail/package.json file, specifically the version update of the @pipedream/gmail package, which is also reflected in the retrieved PR.

Suggested labels

User submitted

Suggested reviewers

  • dannyroosevelt
  • GTFalcao

Poem

I’m a bunny in a code-filled field,
Hopping through updates with a heart that’s thrilled.
Labels now multiply like carrots in a row,
Filtering emails with a magical glow.
Version leaps and cleaner flows set my tail a-swirl –
Cheers to change from this festive little rabbit!
🐇🌟

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

components/gmail/sources/common/polling-history.mjs

Oops! Something went wrong! :(

ESLint: 8.57.1

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'jsonc-eslint-parser' imported from /eslint.config.mjs
at packageResolve (node:internal/modules/esm/resolve:839:9)
at moduleResolve (node:internal/modules/esm/resolve:908:18)
at defaultResolve (node:internal/modules/esm/resolve:1038:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:557:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:525:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:246:38)
at ModuleJob._link (node:internal/modules/esm/module_job:126:49)

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
components/gmail/sources/new-labeled-email/new-labeled-email.mjs (1)

34-37: Consider adding label information to the summary.

While the ID generation change is good, removing the label information from the summary reduces visibility into which labels triggered the event.

-        summary: `A new message with ID: ${message.id} was labeled"`,
+        summary: `A new message with ID: ${message.id} was labeled with: ${message.labelIds.join(', ')}"`,
components/gmail/sources/common/polling-history.mjs (2)

29-31: Consider adding null check for labelIds.

The optional chaining on labels is good, but consider adding a null check for labelIds to prevent undefined behavior.

-      if (this.labels?.length) {
+      if (this.labels?.length && this.labelIds) {
         params.labelIds = this.labelIds;
       }

52-79: Potential performance optimization for label processing.

The current implementation processes labels sequentially. Consider using Promise.all for parallel processing when appropriate.

-      for (let i = 0; i < length; i++) {
-        if (this.labels) {
-          opts.labelId = this.labels[i];
-        }
-
-        const {
-          history, historyId,
-        } = await this.gmail.listHistory(opts);
+      const historyPromises = Array.from({ length }, async (_, i) => {
+        if (!this.labels) return null;
+        return this.gmail.listHistory({
+          ...opts,
+          labelId: this.labels[i],
+        });
+      });
+      
+      const results = await Promise.all(historyPromises);
+      for (const { history, historyId } of results.filter(Boolean)) {
components/gmail/sources/new-email-received/new-email-received.mjs (1)

414-427: Consider simplifying the filterHistory method.

The nested if conditions and repeated checks could be simplified for better readability.

-      let filteredHistory = history.filter((item) => item.messagesAdded?.length);
-      if (this.labels) {
-        filteredHistory = filteredHistory.filter((item) =>
-          item.messagesAdded[0].message.labelIds &&
-          item.messagesAdded[0].message.labelIds.some((i) => this.labels.includes(i)));
-      }
-      if (this.excludeLabels) {
-        filteredHistory = filteredHistory.filter((item) =>
-          item.messagesAdded[0].message.labelIds &&
-          !(item.messagesAdded[0].message.labelIds.some((i) => this.excludeLabels.includes(i))));
-      }
+      return history.filter((item) => {
+        const labelIds = item.messagesAdded?.[0]?.message?.labelIds;
+        if (!item.messagesAdded?.length || !labelIds) return false;
+        
+        const hasRequiredLabel = !this.labels || labelIds.some(i => this.labels.includes(i));
+        const hasExcludedLabel = this.excludeLabels && labelIds.some(i => this.excludeLabels.includes(i));
+        
+        return hasRequiredLabel && !hasExcludedLabel;
+      });
🧰 Tools
🪛 Biome (1.9.4)

[error] 418-419: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a3b4c4d and 6156f80.

📒 Files selected for processing (4)
  • components/gmail/package.json (1 hunks)
  • components/gmail/sources/common/polling-history.mjs (3 hunks)
  • components/gmail/sources/new-email-received/new-email-received.mjs (9 hunks)
  • components/gmail/sources/new-labeled-email/new-labeled-email.mjs (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • components/gmail/package.json
🧰 Additional context used
🪛 Biome (1.9.4)
components/gmail/sources/new-email-received/new-email-received.mjs

[error] 418-419: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

components/gmail/sources/new-labeled-email/new-labeled-email.mjs

[error] 42-42: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 43-45: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (5)
components/gmail/sources/new-labeled-email/new-labeled-email.mjs (3)

11-11: Version bump looks appropriate.

The version increment from 0.0.6 to 0.0.7 is appropriate for the feature enhancement of multiple label support.


16-23: LGTM: Labels property correctly configured for multiple selection.

The conversion from single label to multiple labels is well implemented with appropriate type and label changes.


41-45:

❓ Verification inconclusive

Verify the label filtering logic.

The multiple label filtering logic looks correct but should be tested with edge cases:

  1. Messages with multiple matching labels
  2. Empty labels array
  3. Non-existent labels

🏁 Script executed:

#!/bin/bash
# Search for existing test cases covering label filtering
rg -A 5 "test.*label.*filter|filterHistory.*test" 

Length of output: 51


Review Verification: Label Filtering Logic Requires Test Coverage

The label filtering logic in components/gmail/sources/new-labeled-email/new-labeled-email.mjs (lines 41–45) appears to be implemented correctly. However, a search for test cases that cover the following edge cases:

  • Messages with multiple matching labels,
  • An empty labels array,
  • Non-existent labels

did not yield any results. This absence suggests that automated validation for these scenarios may be lacking.

Please ensure that tests are added (or manually verified) to confirm that the filtering behaves correctly in these edge cases.

🧰 Tools
🪛 Biome (1.9.4)

[error] 42-42: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 43-45: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

components/gmail/sources/common/polling-history.mjs (1)

103-105: Good addition of excludeLabels filtering.

The implementation of excludeLabels is clean and efficient, using Array.some for optimal performance.

components/gmail/sources/new-email-received/new-email-received.mjs (1)

75-98: Well-structured properties for label handling.

The implementation of labels and excludeLabels properties is clean and well-documented. Good default value for labels.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
components/gmail/sources/common/polling-history.mjs (2)

52-79: Consider using array methods for cleaner implementation.

While the current implementation is correct, it could be more concise and maintainable using array methods.

Here's a suggested refactor:

-      const length = this.labels?.length > 0
-        ? this.labels.length
-        : 1;
-      let maxHistoryId = 0;
-
-      for (let i = 0; i < length; i++) {
-        if (this.labels) {
-          opts.labelId = this.labels[i];
-        }
-
-        const {
-          history, historyId,
-        } = await this.gmail.listHistory(opts);
-
-        if (!history) {
-          continue;
-        }
-
-        maxHistoryId = Math.max(maxHistoryId, historyId);
-        const responseArray = this.filterHistory(history);
-
-        for (const item of responseArray) {
-          await this.emitFullMessage(item.messages[0].id);
-        }
-      }
-      if (maxHistoryId > 0) {
-        this._setLastHistoryId(maxHistoryId);
+      const labelIds = this.labels?.length ? this.labels : [null];
+      let maxHistoryId = 0;
+
+      await Promise.all(labelIds.map(async (labelId) => {
+        const currentOpts = labelId ? { ...opts, labelId } : opts;
+        const { history, historyId } = await this.gmail.listHistory(currentOpts);
+
+        if (history) {
+          maxHistoryId = Math.max(maxHistoryId, historyId);
+          const responseArray = this.filterHistory(history);
+          await Promise.all(responseArray.map(
+            item => this.emitFullMessage(item.messages[0].id)
+          ));
+        }
+      }));
+
+      if (maxHistoryId > 0) {
+        this._setLastHistoryId(maxHistoryId);

Benefits:

  • More declarative and easier to understand
  • Parallel processing of labels using Promise.all
  • Cleaner handling of the no-labels case

103-105: Add validation for excludeLabels array.

While the exclusion logic is well implemented, consider adding validation to ensure excludeLabels is a valid array when present.

Here's a suggested improvement:

-        if (this.excludeLabels && message.labelIds.some((i) => this.excludeLabels.includes(i))) {
+        if (Array.isArray(this.excludeLabels) && 
+            message.labelIds?.some((i) => this.excludeLabels.includes(i))) {
           return;
         }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6156f80 and 1769554.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (1)
  • components/gmail/sources/common/polling-history.mjs (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Verify TypeScript components
  • GitHub Check: pnpm publish
  • GitHub Check: Publish TypeScript components
  • GitHub Check: Lint Code Base
🔇 Additional comments (2)
components/gmail/sources/common/polling-history.mjs (2)

29-30: LGTM! Safe handling of multiple labels.

The change from single label to array with optional chaining provides safe and flexible label handling.


85-86: LGTM! Consistent handling of multiple labels.

The implementation maintains consistency with other methods in handling multiple labels.

Copy link
Collaborator

@jcortes jcortes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @michelle0927 lgtm! Ready for QA!

@michelle0927 michelle0927 merged commit fe19b54 into master Feb 18, 2025
11 checks passed
@michelle0927 michelle0927 deleted the issue-15608 branch February 18, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Filter for labels in Gmail trigger
2 participants