Skip to content

Add alerting provider to the Firebase Functions SDK #1024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Parallelizes network calls that occur when validating authorization for onCall handlers.
- Adds new regions to V2 API
- Fixes bug where the emulator crashed when given app without an `options` property.
- Adds new alerting providers
18 changes: 17 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
"./v2/https": "./lib/v2/providers/https.js",
"./v2/params": "./lib/v2/params/index.js",
"./v2/pubsub": "./lib/v2/providers/pubsub.js",
"./v2/storage": "./lib/v2/providers/storage.js"
"./v2/storage": "./lib/v2/providers/storage.js",
"./v2/alerts": "./lib/v2/providers/alerts/index.js",
"./v2/alerts/appDistribution": "./lib/v2/providers/alerts/appDistribution.js",
"./v2/alerts/billing": "./lib/v2/providers/alerts/billing.js",
"./v2/alerts/crashlytics": "./lib/v2/providers/alerts/crashlytics.js"
},
"typesVersions": {
"*": {
Expand Down Expand Up @@ -114,6 +118,18 @@
],
"v2/storage": [
"lib/v2/providers/storage"
],
"v2/alerts": [
"lib/v2/providers/alerts"
],
"v2/alerts/appDistribution": [
"lib/v2/providers/alerts/appDistribution"
],
"v2/alerts/billing": [
"lib/v2/providers/alerts/billing"
],
"v2/alerts/crashlytics": [
"lib/v2/providers/alerts/crashlytics"
]
}
},
Expand Down
183 changes: 183 additions & 0 deletions spec/v2/providers/alerts/alerts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { expect } from 'chai';
import * as options from '../../../../src/v2/options';
import * as alerts from '../../../../src/v2/providers/alerts';
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';

const ALERT_TYPE = 'new-alert-type';
const APPID = '123456789';

describe('alerts', () => {
describe('onAlertPublished', () => {
it('should create the function without opts', () => {
const result = alerts.onAlertPublished(ALERT_TYPE, () => 42);

expect(result.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should create the function with opts', () => {
const result = alerts.onAlertPublished(
{
...FULL_OPTIONS,
alertType: ALERT_TYPE,
appId: APPID,
},
() => 42
);

expect(result.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
appId: APPID,
},
retry: false,
},
});
});

it('should have a .run method', () => {
const func = alerts.onAlertPublished(ALERT_TYPE, (event) => event);

const res = func.run('input' as any);

expect(res).to.equal('input');
});
});

describe('getEndpointAnnotation', () => {
beforeEach(() => {
process.env.GCLOUD_PROJECT = 'aProject';
});

afterEach(() => {
options.setGlobalOptions({});
delete process.env.GCLOUD_PROJECT;
});

it('should define the endpoint without appId and opts', () => {
expect(alerts.getEndpointAnnotation({}, ALERT_TYPE)).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should define a complex endpoint without appId', () => {
expect(
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE)
).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
},
retry: false,
},
});
});

it('should define a complex endpoint', () => {
expect(
alerts.getEndpointAnnotation({ ...FULL_OPTIONS }, ALERT_TYPE, APPID)
).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
appId: APPID,
},
retry: false,
},
});
});

it('should merge global & specific opts', () => {
options.setGlobalOptions({
concurrency: 20,
region: 'europe-west1',
minInstances: 1,
});
const specificOpts = {
region: 'us-west1',
minInstances: 3,
};

expect(
alerts.getEndpointAnnotation(specificOpts, ALERT_TYPE, APPID)
).to.deep.equal({
platform: 'gcfv2',
labels: {},
concurrency: 20,
region: ['us-west1'],
minInstances: 3,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: ALERT_TYPE,
appId: APPID,
},
retry: false,
},
});
});
});

describe('getOptsAndAlertTypeAndApp', () => {
it('should parse a string', () => {
const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(
ALERT_TYPE
);

expect(opts).to.deep.equal({});
expect(alertType).to.equal(ALERT_TYPE);
expect(appId).to.be.undefined;
});

it('should parse an options object without appId', () => {
const myOpts: alerts.FirebaseAlertOptions = {
alertType: ALERT_TYPE,
region: 'us-west1',
};

const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(alertType).to.equal(myOpts.alertType);
expect(appId).to.be.undefined;
});

it('should parse an options object with appId', () => {
const myOpts: alerts.FirebaseAlertOptions = {
alertType: ALERT_TYPE,
appId: APPID,
region: 'us-west1',
};

const [opts, alertType, appId] = alerts.getOptsAndAlertTypeAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(alertType).to.equal(myOpts.alertType);
expect(appId).to.be.equal(myOpts.appId);
});
});
});
127 changes: 127 additions & 0 deletions spec/v2/providers/alerts/appDistribution.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { expect } from 'chai';
import * as alerts from '../../../../src/v2/providers/alerts';
import * as appDistribution from '../../../../src/v2/providers/alerts/appDistribution';
import { FULL_ENDPOINT, FULL_OPTIONS } from '../helpers';

const APPID = '123456789';
const myHandler = () => 42;

describe('appDistribution', () => {
describe('onNewTesterIosDevicePublished', () => {
it('should create a function with alertType & appId', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
APPID,
myHandler
);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
appId: APPID,
},
retry: false,
},
});
});

it('should create a function with opts', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
{ ...FULL_OPTIONS },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
},
retry: false,
},
});
});

it('should create a function with appid in opts', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
{ ...FULL_OPTIONS, appId: APPID },
myHandler
);

expect(func.__endpoint).to.deep.equal({
...FULL_ENDPOINT,
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
appId: APPID,
},
retry: false,
},
});
});

it('should create a function without opts or appId', () => {
const func = appDistribution.onNewTesterIosDevicePublished(myHandler);

expect(func.__endpoint).to.deep.equal({
platform: 'gcfv2',
labels: {},
eventTrigger: {
eventType: alerts.eventType,
eventFilters: {
alertType: appDistribution.newTesterIosDeviceAlert,
},
retry: false,
},
});
});

it('should create a function with a run method', () => {
const func = appDistribution.onNewTesterIosDevicePublished(
APPID,
(event) => event
);

const res = func.run('input' as any);

expect(res).to.equal('input');
});
});

describe('getOptsAndApp', () => {
it('should parse a string', () => {
const [opts, appId] = appDistribution.getOptsAndApp(APPID);

expect(opts).to.deep.equal({});
expect(appId).to.equal(APPID);
});

it('should parse an options object without appId', () => {
const myOpts: appDistribution.AppDistributionOptions = {
region: 'us-west1',
};

const [opts, appId] = appDistribution.getOptsAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(appId).to.be.undefined;
});

it('should parse an options object with appId', () => {
const myOpts: appDistribution.AppDistributionOptions = {
appId: APPID,
region: 'us-west1',
};

const [opts, appId] = appDistribution.getOptsAndApp(myOpts);

expect(opts).to.deep.equal({ region: 'us-west1' });
expect(appId).to.equal(APPID);
});
});
});
Loading