Skip to content

Commit 4dc650e

Browse files
author
Izaak Gough
committed
fix: preserve crashlytics options
1 parent 534b3b0 commit 4dc650e

2 files changed

Lines changed: 137 additions & 12 deletions

File tree

src/firebase_functions/options.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,23 @@ def _endpoint(
652652
)
653653

654654

655+
def _alert_options_to_firebase_alert_options(
656+
options: EventHandlerOptions,
657+
alert_type: str | AlertType,
658+
app_id: str | None = None,
659+
) -> FirebaseAlertOptions:
660+
option_values = {
661+
field.name: getattr(options, field.name)
662+
for field in _dataclasses.fields(options)
663+
if field.name not in {"alert_type", "app_id"}
664+
}
665+
return FirebaseAlertOptions(
666+
**option_values,
667+
alert_type=alert_type,
668+
app_id=app_id,
669+
)
670+
671+
655672
@_dataclasses.dataclass(frozen=True, kw_only=True)
656673
class AppDistributionOptions(EventHandlerOptions):
657674
"""
@@ -669,9 +686,10 @@ def _endpoint(
669686
**kwargs,
670687
) -> _manifest.ManifestEndpoint:
671688
assert kwargs["alert_type"] is not None
672-
return FirebaseAlertOptions(
673-
alert_type=kwargs["alert_type"],
674-
app_id=self.app_id,
689+
return _alert_options_to_firebase_alert_options(
690+
self,
691+
kwargs["alert_type"],
692+
self.app_id,
675693
)._endpoint(**kwargs)
676694

677695

@@ -692,9 +710,10 @@ def _endpoint(
692710
**kwargs,
693711
) -> _manifest.ManifestEndpoint:
694712
assert kwargs["alert_type"] is not None
695-
return FirebaseAlertOptions(
696-
alert_type=kwargs["alert_type"],
697-
app_id=self.app_id,
713+
return _alert_options_to_firebase_alert_options(
714+
self,
715+
kwargs["alert_type"],
716+
self.app_id,
698717
)._endpoint(**kwargs)
699718

700719

@@ -715,9 +734,10 @@ def _endpoint(
715734
**kwargs,
716735
) -> _manifest.ManifestEndpoint:
717736
assert kwargs["alert_type"] is not None
718-
return FirebaseAlertOptions(
719-
alert_type=kwargs["alert_type"],
720-
app_id=self.app_id,
737+
return _alert_options_to_firebase_alert_options(
738+
self,
739+
kwargs["alert_type"],
740+
self.app_id,
721741
)._endpoint(**kwargs)
722742

723743

@@ -733,8 +753,9 @@ def _endpoint(
733753
**kwargs,
734754
) -> _manifest.ManifestEndpoint:
735755
assert kwargs["alert_type"] is not None
736-
return FirebaseAlertOptions(
737-
alert_type=kwargs["alert_type"],
756+
return _alert_options_to_firebase_alert_options(
757+
self,
758+
kwargs["alert_type"],
738759
)._endpoint(**kwargs)
739760

740761

tests/test_options.py

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
from pytest import raises
1919

20-
from firebase_functions import https_fn, options, params
20+
from firebase_functions import alerts_fn, https_fn, options, params
21+
from firebase_functions.alerts import app_distribution_fn, billing_fn, crashlytics_fn, performance_fn
2122
from firebase_functions.private.serving import functions_as_yaml, merge_required_apis
2223

2324
# pylint: disable=protected-access
2425

26+
ALERT_SECRET = params.SecretParam("GITLAB_PERSONAL_ACCESS_TOKEN")
27+
2528

2629
@https_fn.on_call()
2730
def asamplefunction(_):
@@ -196,3 +199,104 @@ def test_invoker_with_no_element_throws():
196199
AssertionError, match="HttpsOptions: Invalid option for invoker - must be a non-empty list."
197200
):
198201
options.HttpsOptions(invoker=[])._endpoint(func_name="test")
202+
203+
204+
def _assert_alert_endpoint_options(endpoint, expected_alert_type, expect_app_id: str | None = None):
205+
assert endpoint.region == ["europe-west1"]
206+
assert endpoint.maxInstances == 1
207+
assert endpoint.secretEnvironmentVariables == [{"key": "GITLAB_PERSONAL_ACCESS_TOKEN"}]
208+
assert endpoint.eventTrigger["retry"] is True
209+
assert endpoint.eventTrigger["eventFilters"]["alerttype"] == expected_alert_type
210+
if expect_app_id is None:
211+
assert "appid" not in endpoint.eventTrigger["eventFilters"]
212+
else:
213+
assert endpoint.eventTrigger["eventFilters"]["appid"] == expect_app_id
214+
215+
216+
def test_crashlytics_options_preserved_in_alert_endpoint():
217+
@crashlytics_fn.on_new_fatal_issue_published(
218+
secrets=[ALERT_SECRET],
219+
region="europe-west1",
220+
max_instances=1,
221+
retry=True,
222+
app_id="app-123",
223+
)
224+
def sample(_event):
225+
return None
226+
227+
_assert_alert_endpoint_options(
228+
sample.__firebase_endpoint__,
229+
"crashlytics.newFatalIssue",
230+
expect_app_id="app-123",
231+
)
232+
233+
234+
def test_app_distribution_options_preserved_in_alert_endpoint():
235+
@app_distribution_fn.on_new_tester_ios_device_published(
236+
secrets=[ALERT_SECRET],
237+
region="europe-west1",
238+
max_instances=1,
239+
retry=True,
240+
app_id="app-123",
241+
)
242+
def sample(_event):
243+
return None
244+
245+
_assert_alert_endpoint_options(
246+
sample.__firebase_endpoint__,
247+
"appDistribution.newTesterIosDevice",
248+
expect_app_id="app-123",
249+
)
250+
251+
252+
def test_performance_options_preserved_in_alert_endpoint():
253+
@performance_fn.on_threshold_alert_published(
254+
secrets=[ALERT_SECRET],
255+
region="europe-west1",
256+
max_instances=1,
257+
retry=True,
258+
app_id="app-123",
259+
)
260+
def sample(_event):
261+
return None
262+
263+
_assert_alert_endpoint_options(
264+
sample.__firebase_endpoint__,
265+
"performance.threshold",
266+
expect_app_id="app-123",
267+
)
268+
269+
270+
def test_billing_options_preserved_in_alert_endpoint():
271+
@billing_fn.on_plan_update_published(
272+
secrets=[ALERT_SECRET],
273+
region="europe-west1",
274+
max_instances=1,
275+
retry=True,
276+
)
277+
def sample(_event):
278+
return None
279+
280+
_assert_alert_endpoint_options(
281+
sample.__firebase_endpoint__,
282+
"billing.planUpdate",
283+
)
284+
285+
286+
def test_firebase_alert_options_preserved_in_alert_endpoint():
287+
@alerts_fn.on_alert_published(
288+
alert_type=alerts_fn.AlertType.CRASHLYTICS_NEW_FATAL_ISSUE,
289+
secrets=[ALERT_SECRET],
290+
region="europe-west1",
291+
max_instances=1,
292+
retry=True,
293+
app_id="app-123",
294+
)
295+
def sample(_event):
296+
return None
297+
298+
_assert_alert_endpoint_options(
299+
sample.__firebase_endpoint__,
300+
"crashlytics.newFatalIssue",
301+
expect_app_id="app-123",
302+
)

0 commit comments

Comments
 (0)