Skip to content

Commit a4d9701

Browse files
authored
Merge pull request #9837 from gitbutlerapp/backend-app-info
Provide app name/version via generic backend (getAppInfo)
2 parents 58ac898 + 46b2ba6 commit a4d9701

File tree

10 files changed

+39
-19
lines changed

10 files changed

+39
-19
lines changed

apps/desktop/src/components/AppUpdater.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('AppUpdater', () => {
1616
const MockSettingsService = getSettingsdServiceMock();
1717
const settingsService = new MockSettingsService();
1818
const eventContext = new EventContext();
19-
const posthog = new PostHogWrapper(settingsService, eventContext);
19+
const posthog = new PostHogWrapper(settingsService, backend, eventContext);
2020

2121
beforeEach(() => {
2222
vi.useFakeTimers();

apps/desktop/src/components/ShareIssueModal.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import { page } from '$app/stores';
3+
import { BACKEND } from '$lib/backend';
34
import { FILE_SERVICE } from '$lib/files/fileService';
45
import { GIT_SERVICE } from '$lib/git/gitService';
56
import { SHORTCUT_SERVICE } from '$lib/shortcuts/shortcutService';
@@ -9,7 +10,6 @@
910
import { HTTP_CLIENT } from '@gitbutler/shared/network/httpClient';
1011
1112
import { Button, Checkbox, Modal, Textarea, Textbox, chipToasts } from '@gitbutler/ui';
12-
import { getVersion } from '@tauri-apps/api/app';
1313
1414
type Feedback = {
1515
id: number;
@@ -26,6 +26,7 @@
2626
const dataSharingService = inject(DATA_SHARING_SERVICE);
2727
const gitService = inject(GIT_SERVICE);
2828
const user = inject(USER);
29+
const backend = inject(BACKEND);
2930
3031
export function show() {
3132
modal?.show();
@@ -65,7 +66,8 @@
6566
6667
// put together context information to send with the feedback
6768
let context = '';
68-
const appVersion = await getVersion();
69+
const appInfo = await backend.getAppInfo();
70+
const appVersion = appInfo.version;
6971
const indexLength = await gitIndexLength();
7072
context += 'GitButler Version: ' + appVersion + '\n';
7173
context += 'Browser: ' + navigator.userAgent + '\n';

apps/desktop/src/lib/analytics/analytics.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { PostHogWrapper } from '$lib/analytics/posthog';
22
import { initSentry } from '$lib/analytics/sentry';
33
import { AppSettings } from '$lib/config/appSettings';
4-
import { getName, getVersion } from '@tauri-apps/api/app';
54
import posthog from 'posthog-js';
65

76
export function initAnalyticsIfEnabled(appSettings: AppSettings, postHog: PostHogWrapper) {
@@ -14,12 +13,7 @@ export function initAnalyticsIfEnabled(appSettings: AppSettings, postHog: PostHo
1413
});
1514
appSettings.appMetricsEnabled.onDisk().then(async (enabled) => {
1615
if (enabled) {
17-
if (import.meta.env.VITE_BUILD_TARGET === 'web') {
18-
postHog.init('gitbutler-web', '0.0.0');
19-
} else {
20-
const [appName, appVersion] = await Promise.all([getName(), getVersion()]);
21-
postHog.init(appName, appVersion);
22-
}
16+
await postHog.init();
2317
}
2418
});
2519
appSettings.appNonAnonMetricsEnabled.onDisk().then((enabled) => {

apps/desktop/src/lib/analytics/posthog.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { InjectionToken } from '@gitbutler/shared/context';
22
import { PostHog, posthog, type Properties } from 'posthog-js';
33
import type { EventContext } from '$lib/analytics/eventContext';
4+
import type { IBackend } from '$lib/backend';
45
import type { SettingsService } from '$lib/config/appSettingsV2';
56
import type { RepoInfo } from '$lib/url/gitUrl';
67
import { PUBLIC_POSTHOG_API_KEY } from '$env/static/public';
@@ -12,6 +13,7 @@ export class PostHogWrapper {
1213

1314
constructor(
1415
private settingsService: SettingsService,
16+
private backend: IBackend,
1517
private eventContext: EventContext
1618
) {}
1719

@@ -21,7 +23,8 @@ export class PostHogWrapper {
2123
this._instance?.capture(eventName, newProperties);
2224
}
2325

24-
async init(appName: string, appVersion: string) {
26+
async init() {
27+
const appInfo = await this.backend.getAppInfo();
2528
this._instance = posthog.init(PUBLIC_POSTHOG_API_KEY, {
2629
api_host: 'https://eu.posthog.com',
2730
autocapture: false,
@@ -34,8 +37,8 @@ export class PostHogWrapper {
3437
}
3538
});
3639
posthog.register({
37-
appName,
38-
appVersion
40+
appName: appInfo.name,
41+
appVersion: appInfo.version
3942
});
4043
}
4144

apps/desktop/src/lib/backend/backend.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ export type OpenDialogReturn<T extends OpenDialogOptions> = T['directory'] exten
8686
? string[] | null
8787
: string | null;
8888

89+
export type AppInfo = {
90+
name: string;
91+
version: string;
92+
};
8993
export interface IBackend {
9094
systemTheme: Readable<string | null>;
9195
invoke: <T>(command: string, ...args: any[]) => Promise<T>;
@@ -99,4 +103,5 @@ export interface IBackend {
99103
joinPath: (path: string, ...paths: string[]) => Promise<string>;
100104
filePicker<T extends OpenDialogOptions>(options?: T): Promise<OpenDialogReturn<T>>;
101105
homeDirectory(): Promise<string>;
106+
getAppInfo: () => Promise<AppInfo>;
102107
}

apps/desktop/src/lib/backend/tauri.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isReduxError } from '$lib/state/reduxError';
2-
import { getVersion as tauriGetVersion } from '@tauri-apps/api/app';
2+
import { getName, getVersion, getVersion as tauriGetVersion } from '@tauri-apps/api/app';
33
import { invoke as invokeTauri } from '@tauri-apps/api/core';
44
import { listen as listenTauri } from '@tauri-apps/api/event';
55
import { documentDir as documentDirTauri } from '@tauri-apps/api/path';
@@ -10,7 +10,7 @@ import { readFile as tauriReadFile } from '@tauri-apps/plugin-fs';
1010
import { relaunch as relaunchTauri } from '@tauri-apps/plugin-process';
1111
import { check as tauriCheck } from '@tauri-apps/plugin-updater';
1212
import { readable } from 'svelte/store';
13-
import type { IBackend } from '$lib/backend/backend';
13+
import type { AppInfo, IBackend } from '$lib/backend/backend';
1414
import type { EventCallback, EventName } from '@tauri-apps/api/event';
1515

1616
export default class Tauri implements IBackend {
@@ -36,6 +36,7 @@ export default class Tauri implements IBackend {
3636
relaunch = relaunchTauri;
3737
documentDir = documentDirTauri;
3838
joinPath = joinPathTauri;
39+
getAppInfo = tauriGetAppInfo;
3940
async filePicker<T extends OpenDialogOptions>() {
4041
return await filePickerTauri<T>();
4142
}
@@ -46,6 +47,11 @@ export default class Tauri implements IBackend {
4647
}
4748
}
4849

50+
async function tauriGetAppInfo(): Promise<AppInfo> {
51+
const [appName, appVersion] = await Promise.all([getName(), getVersion()]);
52+
return { name: appName, version: appVersion };
53+
}
54+
4955
async function tauriInvoke<T>(command: string, params: Record<string, unknown> = {}): Promise<T> {
5056
// This commented out code can be used to delay/reject an api call
5157
// return new Promise<T>((resolve, reject) => {

apps/desktop/src/lib/backend/web.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isReduxError } from '$lib/state/reduxError';
22
import { getCookie } from '$lib/utils/cookies';
33
import { readable } from 'svelte/store';
44
import path from 'path';
5-
import type { IBackend, OpenDialogOptions, OpenDialogReturn } from '$lib/backend/backend';
5+
import type { AppInfo, IBackend, OpenDialogOptions, OpenDialogReturn } from '$lib/backend/backend';
66

77
export default class Web implements IBackend {
88
systemTheme = readable<string | null>(null);
@@ -16,11 +16,19 @@ export default class Web implements IBackend {
1616
documentDir = webDocumentDir;
1717
joinPath = webJoinPath;
1818
homeDirectory = webHomeDirectory;
19+
getAppInfo = webGetAppInfo;
1920
async filePicker<T extends OpenDialogOptions>(options?: T): Promise<OpenDialogReturn<T>> {
2021
return await webFilePicker<T>(options);
2122
}
2223
}
2324

25+
async function webGetAppInfo(): Promise<AppInfo> {
26+
return await Promise.resolve({
27+
name: 'gitbutler-web',
28+
version: '0.0.0'
29+
});
30+
}
31+
2432
async function webHomeDirectory(): Promise<string> {
2533
// This needs to be implemented for the web version
2634
return await Promise.resolve('/tmp/gitbutler');

apps/desktop/src/lib/forge/forgeFactory.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EventContext } from '$lib/analytics/eventContext';
22
import { PostHogWrapper } from '$lib/analytics/posthog';
3+
import createBackend from '$lib/backend';
34
import { DefaultForgeFactory } from '$lib/forge/forgeFactory.svelte';
45
import { GitHub } from '$lib/forge/github/github';
56
import { GitLab } from '$lib/forge/gitlab/gitlab';
@@ -12,9 +13,10 @@ import type { ThunkDispatch, UnknownAction } from '@reduxjs/toolkit';
1213

1314
describe.concurrent('DefaultforgeFactory', () => {
1415
const MockSettingsService = getSettingsdServiceMock();
16+
const backend = createBackend();
1517
const settingsService = new MockSettingsService();
1618
const eventContext = new EventContext();
17-
const posthog = new PostHogWrapper(settingsService, eventContext);
19+
const posthog = new PostHogWrapper(settingsService, backend, eventContext);
1820
const gitHubApi: GitHubApi = {
1921
endpoints: {},
2022
reducerPath: 'github',

apps/desktop/src/lib/updater/updater.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Updater', () => {
1818
const shortcuts = new ShortcutService(backend);
1919
const settingsService = new MockSettingsService();
2020
const eventContext = new EventContext();
21-
const posthog = new PostHogWrapper(settingsService, eventContext);
21+
const posthog = new PostHogWrapper(settingsService, backend, eventContext);
2222

2323
beforeEach(() => {
2424
vi.useFakeTimers();

apps/desktop/src/routes/+layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const load: LayoutLoad = async () => {
3838
const eventContext = new EventContext();
3939
// Awaited and will block initial render, but it is necessary in order to respect the user
4040
// settings on telemetry.
41-
const posthog = new PostHogWrapper(settingsService, eventContext);
41+
const posthog = new PostHogWrapper(settingsService, backend, eventContext);
4242
const appSettings = await loadAppSettings();
4343
initAnalyticsIfEnabled(appSettings, posthog);
4444

0 commit comments

Comments
 (0)