forked from firebase/functions-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (70 loc) · 3.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const functions = require('firebase-functions');
const rp = require('request-promise');
// Helper function that posts to Slack about the new issue
function notifySlack(slackMessage) {
// See https://api.slack.com/docs/message-formatting on how
// to customize the message payload
return rp({
method: 'POST',
uri: functions.config().slack.webhook_url,
body: {
text: slackMessage,
},
json: true,
});
}
// [START on_new_issue]
exports.postOnNewIssue = functions.crashlytics.issue().onNew(async (issue) => {
const issueId = issue.issueId;
const issueTitle = issue.issueTitle;
const appName = issue.appInfo.appName;
const appPlatform = issue.appInfo.appPlatform;
const latestAppVersion = issue.appInfo.latestAppVersion;
const slackMessage = `<!here|here> There is a new issue - ${issueTitle} (${issueId}) ` +
`in ${appName}, version ${latestAppVersion} on ${appPlatform}`;
await notifySlack(slackMessage);
console.log(`Posted new issue ${issueId} successfully to Slack`);
});
// [END on_new_issue]
exports.postOnRegressedIssue = functions.crashlytics.issue().onRegressed(async (issue) => {
const issueId = issue.issueId;
const issueTitle = issue.issueTitle;
const appName = issue.appInfo.appName;
const appPlatform = issue.appInfo.appPlatform;
const latestAppVersion = issue.appInfo.latestAppVersion;
const resolvedTime = issue.resolvedTime;
const slackMessage = `<!here|here> There is a regressed issue ${issueTitle} (${issueId}) ` +
`in ${appName}, version ${latestAppVersion} on ${appPlatform}. This issue was previously ` +
`resolved at ${new Date(resolvedTime).toString()}`;
await notifySlack(slackMessage);
console.log(`Posted regressed issue ${issueId} successfully to Slack`);
});
exports.postOnVelocityAlert = functions.crashlytics.issue().onVelocityAlert(async (issue) => {
const issueId = issue.issueId;
const issueTitle = issue.issueTitle;
const appName = issue.appInfo.appName;
const appPlatform = issue.appInfo.appPlatform;
const latestAppVersion = issue.appInfo.latestAppVersion;
const crashPercentage = issue.velocityAlert.crashPercentage;
const slackMessage = `<!here|here> There is an issue ${issueTitle} (${issueId}) ` +
`in ${appName}, version ${latestAppVersion} on ${appPlatform} that is causing ` +
`${parseFloat(crashPercentage).toFixed(2)}% of all sessions to crash.`;
await notifySlack(slackMessage);
console.log(`Posted velocity alert ${issueId} successfully to Slack`);
});