Skip to content

Commit c1451e3

Browse files
authored
Merge pull request #1011 from firebase/app-distribution-event-arc
Add example code for App Distribution Alert (New iOS Tester Device)
2 parents eb22a2f + 5b5e9ad commit c1451e3

File tree

3 files changed

+1633
-1558
lines changed

3 files changed

+1633
-1558
lines changed

2nd-gen/alerts-to-discord/functions/index.js

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
* limitations under the License.
1515
*/
1616
// [START v2import]
17+
1718
const {
1819
onNewFatalIssuePublished,
1920
} = require("firebase-functions/v2/alerts/crashlytics");
21+
const {
22+
onNewTesterIosDevicePublished,
23+
} = require("firebase-functions/v2/alerts/appDistribution");
2024
const logger = require("firebase-functions/logger");
2125
// [END v2import]
2226

@@ -56,13 +60,14 @@ async function postMessageToDiscord(botName, messageBody) {
5660
* to Discord whenever a new fatal issue occurs.
5761
*/
5862
// [START v2CrashlyticsAlertTrigger]
59-
exports.postmessagetodiscord = onNewFatalIssuePublished(async (event) => {
63+
exports.postfatalissuetodiscord = onNewFatalIssuePublished(async (event) => {
6064
// [END v2CrashlyticsAlertTrigger]
6165
// [START v2CrashlyticsEventPayload]
6266
// construct a helpful message to send to Discord
67+
const appId = event.appId;
6368
const {id, title, subtitle, appVersion} = event.data.payload.issue;
6469
const message = `
65-
🚨 New fatal issue in version ${appVersion} 🚨
70+
🚨 New fatal issue for ${appId} in version ${appVersion} 🚨
6671
6772
**${title}**
6873
@@ -77,7 +82,7 @@ id: \`${id}\`
7782
const response = await postMessageToDiscord("Crashlytics Bot", message);
7883
if (response.ok) {
7984
logger.info(
80-
`Posted fatal Crashlytics alert ${id} to Discord`,
85+
`Posted fatal Crashlytics alert ${id} for ${appId} to Discord`,
8186
event.data.payload,
8287
);
8388
} else {
@@ -86,7 +91,49 @@ id: \`${id}\`
8691
// [END v2SendToDiscord]
8792
} catch (error) {
8893
logger.error(
89-
`Unable to post fatal Crashlytics alert ${id} to Discord`,
94+
`Unable to post fatal Crashlytics alert ${id} for ${appId} to Discord`,
95+
error,
96+
);
97+
}
98+
});
99+
100+
/**
101+
* function triggered by App Distribution that publishes a message
102+
* to Discord whenever a new iOS tester device is registered.
103+
*/
104+
// [START v2AppDistributionAlertTrigger]
105+
exports.postnewduuidtodiscord = onNewTesterIosDevicePublished(async (event) => {
106+
// [END v2AppDistributionAlertTrigger]
107+
// [START v2AppDistributionEventPayload]
108+
// construct a helpful message to send to Discord
109+
const appId = event.appId;
110+
const {
111+
testerDeviceIdentifier,
112+
testerDeviceModelName,
113+
testerEmail,
114+
testerName,
115+
} = event.data.payload;
116+
const message = `
117+
📱 New iOS device registered by ${testerName} <${testerEmail}> for ${appId}
118+
119+
UDID **${testerDeviceIdentifier}** for ${testerDeviceModelName}
120+
`;
121+
// [END v2AppDistributionEventPayload]
122+
123+
try {
124+
// [START v2SendNewTesterIosDeviceToDiscord]
125+
const response = await postMessageToDiscord("AppDistribution Bot", message);
126+
if (response.ok) {
127+
logger.info(
128+
`Posted iOS device registration alert for ${testerEmail} to Discord`,
129+
);
130+
} else {
131+
throw new Error(response.error);
132+
}
133+
// [END v2SendNewTesterIosDeviceToDiscord]
134+
} catch (error) {
135+
logger.error(
136+
`Unable to post iOS device registration for ${testerEmail} to Discord`,
90137
error,
91138
);
92139
}

2nd-gen/alerts-to-discord/functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"main": "index.js",
1717
"dependencies": {
1818
"firebase-admin": "^10.1.0",
19-
"firebase-functions": "^3.20.1",
19+
"firebase-functions": "^3.21.1",
2020
"node-fetch": "^2.6.7"
2121
},
2222
"devDependencies": {

0 commit comments

Comments
 (0)