Skip to content

Commit 22f2c35

Browse files
committed
Implementing refactorings
1 parent 5f94ca6 commit 22f2c35

9 files changed

Lines changed: 94 additions & 366 deletions

File tree

functions/src/alerts/crashlytics.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11

2-
import {firestore} from "firebase-admin";
32
import {logger} from "firebase-functions/v2";
43
import {crashlytics} from "firebase-functions/v2/alerts";
54
import {post} from "request";
65
import {AppCrash} from "../models/app-crash";
7-
import {AppInfo, IAppInfo} from "../models/app-info";
86
import {Webhook} from "../models/webhook";
9-
import {makeFirebaseAppsSettingsUrl, makeFirestoreAppInfoUrl} from "../urls";
107
import {EnvConfig} from "../utils/env-config";
118
import {DiscordWebhook} from "../webhook-plugins/discord";
129
import {GoogleChatWebhook} from "../webhook-plugins/google-chat";
@@ -48,35 +45,13 @@ async function handleCrashlyticsEvent(appCrash: AppCrash):
4845
Promise<object | void> {
4946
logger.debug("[handleCrashlyticsEvent]", appCrash);
5047

51-
// Update and ensure that there is a Firestore document for this app id
52-
await firestore()
53-
.collection("apps")
54-
.doc(appCrash.appId)
55-
.set({
56-
appId: appCrash.appId,
57-
lastIssue: firestore.FieldValue.serverTimestamp(),
58-
issueCount: firestore.FieldValue.increment(1),
59-
}, {merge: true});
60-
61-
62-
const appInfoSnap = await firestore()
63-
.collection("apps")
64-
.doc(appCrash.appId)
65-
.get();
66-
67-
logger.debug("[handleCrashlyticsEvent] App info", appInfoSnap.data());
68-
69-
const appInfo = new AppInfo(appInfoSnap.data() as IAppInfo);
70-
if (!appInfo.bundleId) {
71-
// Will need to add this information explicitly by copying the bundle id
72-
// from Firebase Console project overview. The console log below will
73-
// provide links to add the configuration.
74-
logger.warn(
75-
"[handleCrashlyticsEvent] No bundle id for app. Fix it manually", {
76-
appInfo,
77-
settings: makeFirebaseAppsSettingsUrl(),
78-
firestore: makeFirestoreAppInfoUrl(appInfo),
48+
if (appCrash.appId !== EnvConfig.appId) {
49+
logger.debug(
50+
"[handleCrashlyticsEvent] Skipping crash for different app", {
51+
appId: appCrash.appId,
52+
expectedAppId: EnvConfig.appId,
7953
});
54+
return;
8055
}
8156

8257
const webhooks: Webhook[] = EnvConfig.webhooks.map(webhookPluginFromUrl);
@@ -88,8 +63,9 @@ async function handleCrashlyticsEvent(appCrash: AppCrash):
8863
const promises = [];
8964
for (const webhook of webhooks) {
9065
logger.debug("[handleCrashlyticsEvent] Webhook", webhook);
66+
const crashlyticsMessage = webhook.createCrashlyticsMessage(appCrash);
9167
const webhookPayload = {
92-
body: JSON.stringify(webhook.createCrashlyticsMessage(appInfo, appCrash)),
68+
body: JSON.stringify(crashlyticsMessage),
9369
};
9470

9571
try {

functions/src/models/app-info.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

functions/src/models/github.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

functions/src/models/webhook.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {AppInfo} from "./app-info";
21
import {AppCrash} from "./app-crash";
32
import {Localization} from "../utils/localization";
43

@@ -27,12 +26,10 @@ export abstract class Webhook implements IWebhook {
2726
/**
2827
* Create message payload for the webhook to send a crashlytics message
2928
*
30-
* @param {AppInfo} appInfo
3129
* @param {AppCrash} appCrash
3230
* @return {object} Webhook body payload
3331
*/
3432
public abstract createCrashlyticsMessage(
35-
appInfo: AppInfo,
3633
appCrash: AppCrash,
3734
): object;
3835
}

functions/src/urls.ts

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {AppInfo} from "./models/app-info";
21
import {AppCrash} from "./models/app-crash";
32
import {EnvConfig} from "./utils/env-config";
43

@@ -7,14 +6,11 @@ export const crashlyticsImgUrl = "https://github.com/oddbit/firebase-alerts/raw/
76
/**
87
* Generate a URL to Firebase Console for the issue
98
*
10-
* @param {AppInfo} appInfo
119
* @param {AppCrash} appCrash
1210
* @return {string} URL to Firebase console
1311
*/
14-
export function makeCrashlyticsIssueUrl(
15-
appInfo: AppInfo,
16-
appCrash: AppCrash,): string {
17-
return `https://console.firebase.google.com/project/${EnvConfig.projectId}/crashlytics/app/${appInfo.platform}:${appInfo.bundleId}/issues/${appCrash.issueId}`;
12+
export function makeCrashlyticsIssueUrl(appCrash: AppCrash): string {
13+
return `https://console.firebase.google.com/project/${EnvConfig.projectId}/crashlytics/app/${EnvConfig.platform}:${EnvConfig.bundleId}/issues/${appCrash.issueId}`;
1814
}
1915

2016
/**
@@ -27,43 +23,37 @@ export function makeFirebaseAppsSettingsUrl(): string {
2723
}
2824

2925
/**
30-
* Generate a URL to Firestore app info document
26+
* Make a repository URL to create an issue from this app crash
3127
*
32-
* @param {AppInfo} appInfo
33-
* @return {string} URL to Firebase console
34-
*/
35-
export function makeFirestoreAppInfoUrl(appInfo: AppInfo): string {
36-
return `https://console.firebase.google.com/project/${EnvConfig.projectId}/firestore/data/~2F${process.env.EXT_INSTANCE_ID}-apps~2F${appInfo.appId}`;
37-
}
38-
39-
/**
40-
* Make an Github URL to create an issue from this app crash
41-
*
42-
* @param {AppInfo} appInfo
4328
* @param {AppCrash} appCrash
4429
* @return {string} URL to create a github issue
4530
*/
46-
export function makeGithubIssueUrl(
47-
appInfo: AppInfo,
48-
appCrash: AppCrash,): string {
49-
const attributes = [
50-
`title=${encodeURI(appCrash.issueTitle)}`,
51-
`labels=${appCrash.tags.map((tag) => encodeURI(tag)).join(",")}`,
52-
];
53-
54-
return `https://github.com/${appInfo.github?.repo}/issues/new?${attributes.join("&")}`;
31+
export function makeRepositoryIssueUrl(appCrash: AppCrash): string {
32+
const repositoryUrl = EnvConfig.repositoryUrl;
33+
if (repositoryUrl.startsWith("https://github.com")) {
34+
const attributes = [
35+
`title=${encodeURI(appCrash.issueTitle)}`,
36+
`labels=${appCrash.tags.map((tag) => encodeURI(tag)).join(",")}`,
37+
];
38+
39+
return `${repositoryUrl}/issues/new?${attributes.join("&")}`;
40+
}
41+
42+
throw new Error("Unknown repository type: " + repositoryUrl);
5543
}
5644

5745
/**
58-
* Make an Github URL to search for issues from this app crash
46+
* Make a repository URL to search for issues from this app crash
5947
*
60-
* @param {AppInfo} appInfo
6148
* @param {AppCrash} appCrash
6249
* @return {string} URL to search for github issues
6350
*/
64-
export function makeGithubSearchUrl(
65-
appInfo: AppInfo,
66-
appCrash: AppCrash,): string {
67-
return `https://github.com/${appInfo.github?.repo}/issues?q=${encodeURI(appCrash.issueTitle)}`;
51+
export function makeRepositorySearchUrl(appCrash: AppCrash): string {
52+
const repositoryUrl = EnvConfig.repositoryUrl;
53+
if (repositoryUrl.startsWith("https://github.com")) {
54+
return `${repositoryUrl}/issues?q=${encodeURI(appCrash.issueTitle)}`;
55+
}
56+
57+
throw new Error("Unknown repository type: " + repositoryUrl);
6858
}
6959

functions/src/utils/localization.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,14 @@ const l10n = {
3333
"labelBundleId": {
3434
"en": "Bundle Id",
3535
},
36-
"missingBundleId": {
37-
"en": "Missing bundle id",
38-
},
3936
"openCrashlyticsIssue": {
4037
"en": "Open Crashlytics",
4138
},
42-
"openFirebaseAppsSettings": {
43-
"en": "Open Firebase Apps List",
44-
},
45-
"openFirestoreAppInfo": {
46-
"en": "Update Firestore Document",
47-
},
48-
"createGithubIssue": {
49-
"en": "Create Github Issue",
39+
"createIssue": {
40+
"en": "Create Issue",
5041
},
51-
"searchGithubIssue": {
52-
"en": "Search Similar Github Issues",
42+
"searchIssue": {
43+
"en": "Search Similar Issues",
5344
},
5445
"descriptionViewInCrashlytics": {
5546
"en": "View this issue in Firebase console",
@@ -60,12 +51,6 @@ const l10n = {
6051
"descriptionSearchSimilarIssues": {
6152
"en": "Search for similar reports of this issue",
6253
},
63-
"descriptionOpenFirebaseAppsSettings": {
64-
"en": "Go to Firebase console and copy the apps bundle id",
65-
},
66-
"descriptionOpenFirestoreAppInfo": {
67-
"en": "Open Firestore and update app info document with bundle id",
68-
},
6954
};
7055

7156

0 commit comments

Comments
 (0)