Skip to content

Conversation

@shettyvarun268
Copy link

This PR adds the v1-compatible argument getters (context and message) directly to the v2 Pub/Sub CloudEvent class.
This allows developers to access v1-style arguments via property access, e.g., const { message, context } = event;, for a smoother migration of existing business logic to the v2 function signature.Implementation Details

The v1-compatible context getter is implemented as a lazily-computed property on the CloudEvent object. To correctly construct the v1 EventContext from the v2 event's properties, the following two internal helper functions are introduced:

  1. attachPubSubContext<T>(event: CloudEvent<MessagePublishedData<T>>, topic: string):
    Adds the v1-style context property to the v2 event object using Object.defineProperty.
    The getter computes the EventContext by mapping v2 properties (event.id, event.time) and by calling getResourceName.
    This ensures the context is only computed once upon first access.

  2. getResourceName(event: CloudEvent<MessagePublishedData<any>>, topic: string):
    Extracts the full resource name for the v1 EventContext resource object.
    It attempts to parse the project ID and topic name from event.source or falls back to environment variables and the function's topic name parameter if necessary.

This is the first of a series of PRs aimed at simplifying the migration from v1 to v2. By introducing this new
onMessagePublished function, we are providing a more robust and flexible way to handle Pub/Sub events in Cloud Functions for Firebase v2.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @shettyvarun268, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant enhancement to the v2 Pub/Sub CloudEvent handling by integrating v1-compatible argument accessors. The primary goal is to facilitate a seamless migration for developers transitioning their existing v1 Cloud Functions to the v2 signature. By providing direct property access to message and context in a v1-like manner, it reduces the friction associated with adapting business logic to the new event structure, making the upgrade path more straightforward.

Highlights

  • V1 Compatibility: Adds v1-compatible context and message getters directly to the v2 Pub/Sub CloudEvent<MessagePublishedData> class.
  • Migration Simplification: Enables direct property access (e.g., const { message, context } = event;) for smoother migration of existing v1 business logic to v2 function signatures.
  • Lazy Context Computation: Implements the v1-compatible context getter as a lazily-computed property on the CloudEvent object, ensuring it's only computed upon first access.
  • New Helper Functions: Introduces two internal helper functions: attachPubSubContext to add the v1-style context and getResourceName to extract the full resource name for the v1 EventContext, including robust fallback logic for project and topic names.
  • Strategic Initiative: This pull request is the first in a series aimed at simplifying the overall migration process from v1 to v2 Cloud Functions for Firebase.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@shettyvarun268
Copy link
Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a v1-compatibility layer for v2 Pub/Sub functions by adding a lazily-computed context property to the CloudEvent object. The implementation is well-structured, using Object.defineProperty for lazy evaluation and providing fallbacks for constructing the resource name. I've identified a bug in the resource name construction for a fallback case and a couple of areas for improvement in the tests to make them more robust and accurate. My feedback includes a high-severity fix for the resource name format and medium-severity suggestions to improve test coverage and correctness.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request successfully implements V1-compatible API for PubSub events in V2 Cloud Functions, which will greatly assist in migrating existing business logic. The changes introduce "context" and "message" getters to the CloudEvent<MessagePublishedData> class, along with robust helper functions attachPubSubContext and getResourceName to construct the V1 EventContext. The new test cases thoroughly cover the functionality, including handling existing contexts and fallback mechanisms for resource name extraction.

@jhuleatt jhuleatt requested a review from egilmorez December 5, 2025 18:57
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.

3 participants