Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/firebase_functions/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,27 @@ def _endpoint(
)


def _alert_options_to_firebase_alert_options(
options: EventHandlerOptions,
alert_type: str | AlertType,
) -> FirebaseAlertOptions:
app_id = getattr(options, "app_id", None)

# Restrict to fields supported by FirebaseAlertOptions
allowed_fields = {f.name for f in _dataclasses.fields(FirebaseAlertOptions)}

option_values = {
field.name: getattr(options, field.name)
for field in _dataclasses.fields(options)
if field.name in allowed_fields and field.name not in {"alert_type", "app_id"}
}
return FirebaseAlertOptions(
**option_values,
alert_type=alert_type,
app_id=app_id,
)
Comment thread
IzaakGough marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think there can be a possibility of a future regression here. If a developer adds a custom options field to a specific subclass (e.g., AppDistributionOptions) that is not present on FirebaseAlertOptions, the current implementation will include it in option_values and attempt to pass it to the FirebaseAlertOptions constructor. This will raise a runtime error. Maybe we can try to filter option_values to only include fields defined in firebaseAlertOptions?

Ai suggested something like this:

def _alert_options_to_firebase_alert_options(
    options: EventHandlerOptions,
    alert_type: str | AlertType,
) -> FirebaseAlertOptions:
    app_id = getattr(options, "app_id", None)
    
    # Restrict to fields supported by FirebaseAlertOptions
    allowed_fields = {f.name for f in _dataclasses.fields(FirebaseAlertOptions)}
    
    option_values = {
        field.name: getattr(options, field.name)
        for field in _dataclasses.fields(options)
        if field.name in allowed_fields and field.name not in {"alert_type", "app_id"}
    }
    return FirebaseAlertOptions(
        **option_values,
        alert_type=alert_type,
        app_id=app_id,
    )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I think the suggested snippet here looks good. Will add this.



@_dataclasses.dataclass(frozen=True, kw_only=True)
class AppDistributionOptions(EventHandlerOptions):
"""
Expand All @@ -669,9 +690,9 @@ def _endpoint(
**kwargs,
) -> _manifest.ManifestEndpoint:
assert kwargs["alert_type"] is not None
return FirebaseAlertOptions(
alert_type=kwargs["alert_type"],
app_id=self.app_id,
return _alert_options_to_firebase_alert_options(
self,
kwargs["alert_type"],
)._endpoint(**kwargs)
Comment thread
IzaakGough marked this conversation as resolved.


Expand All @@ -692,9 +713,9 @@ def _endpoint(
**kwargs,
) -> _manifest.ManifestEndpoint:
assert kwargs["alert_type"] is not None
return FirebaseAlertOptions(
alert_type=kwargs["alert_type"],
app_id=self.app_id,
return _alert_options_to_firebase_alert_options(
self,
kwargs["alert_type"],
)._endpoint(**kwargs)
Comment thread
IzaakGough marked this conversation as resolved.


Expand All @@ -715,9 +736,9 @@ def _endpoint(
**kwargs,
) -> _manifest.ManifestEndpoint:
assert kwargs["alert_type"] is not None
return FirebaseAlertOptions(
alert_type=kwargs["alert_type"],
app_id=self.app_id,
return _alert_options_to_firebase_alert_options(
self,
kwargs["alert_type"],
)._endpoint(**kwargs)
Comment thread
IzaakGough marked this conversation as resolved.


Expand All @@ -733,8 +754,9 @@ def _endpoint(
**kwargs,
) -> _manifest.ManifestEndpoint:
assert kwargs["alert_type"] is not None
return FirebaseAlertOptions(
alert_type=kwargs["alert_type"],
return _alert_options_to_firebase_alert_options(
self,
kwargs["alert_type"],
)._endpoint(**kwargs)


Expand Down
111 changes: 110 additions & 1 deletion tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@

from pytest import raises

from firebase_functions import https_fn, options, params
from firebase_functions import alerts_fn, https_fn, options, params
from firebase_functions.alerts import (
app_distribution_fn,
billing_fn,
crashlytics_fn,
performance_fn,
)
from firebase_functions.private.serving import functions_as_yaml, merge_required_apis

# pylint: disable=protected-access

ALERT_SECRET = params.SecretParam("GITLAB_PERSONAL_ACCESS_TOKEN")


@https_fn.on_call()
def asamplefunction(_):
Expand Down Expand Up @@ -196,3 +204,104 @@ def test_invoker_with_no_element_throws():
AssertionError, match="HttpsOptions: Invalid option for invoker - must be a non-empty list."
):
options.HttpsOptions(invoker=[])._endpoint(func_name="test")


def _assert_alert_endpoint_options(endpoint, expected_alert_type, expect_app_id: str | None = None):
assert endpoint.region == ["europe-west1"]
assert endpoint.maxInstances == 1
assert endpoint.secretEnvironmentVariables == [{"key": "GITLAB_PERSONAL_ACCESS_TOKEN"}]
assert endpoint.eventTrigger["retry"] is True
assert endpoint.eventTrigger["eventFilters"]["alerttype"] == expected_alert_type
if expect_app_id is None:
assert "appid" not in endpoint.eventTrigger["eventFilters"]
else:
assert endpoint.eventTrigger["eventFilters"]["appid"] == expect_app_id


def test_crashlytics_options_preserved_in_alert_endpoint():
@crashlytics_fn.on_new_fatal_issue_published(
secrets=[ALERT_SECRET],
region="europe-west1",
max_instances=1,
retry=True,
app_id="app-123",
)
def sample(_event):
return None

_assert_alert_endpoint_options(
sample.__firebase_endpoint__,
"crashlytics.newFatalIssue",
expect_app_id="app-123",
)


def test_app_distribution_options_preserved_in_alert_endpoint():
@app_distribution_fn.on_new_tester_ios_device_published(
secrets=[ALERT_SECRET],
region="europe-west1",
max_instances=1,
retry=True,
app_id="app-123",
)
def sample(_event):
return None

_assert_alert_endpoint_options(
sample.__firebase_endpoint__,
"appDistribution.newTesterIosDevice",
expect_app_id="app-123",
)


def test_performance_options_preserved_in_alert_endpoint():
@performance_fn.on_threshold_alert_published(
secrets=[ALERT_SECRET],
region="europe-west1",
max_instances=1,
retry=True,
app_id="app-123",
)
def sample(_event):
return None

_assert_alert_endpoint_options(
sample.__firebase_endpoint__,
"performance.threshold",
expect_app_id="app-123",
)


def test_billing_options_preserved_in_alert_endpoint():
@billing_fn.on_plan_update_published(
secrets=[ALERT_SECRET],
region="europe-west1",
max_instances=1,
retry=True,
)
def sample(_event):
return None

_assert_alert_endpoint_options(
sample.__firebase_endpoint__,
"billing.planUpdate",
)


def test_firebase_alert_options_preserved_in_alert_endpoint():
@alerts_fn.on_alert_published(
alert_type=alerts_fn.AlertType.CRASHLYTICS_NEW_FATAL_ISSUE,
secrets=[ALERT_SECRET],
region="europe-west1",
max_instances=1,
retry=True,
app_id="app-123",
)
def sample(_event):
return None

_assert_alert_endpoint_options(
sample.__firebase_endpoint__,
"crashlytics.newFatalIssue",
expect_app_id="app-123",
)
Loading