Skip to content
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

Electron: refactor app version fields #1389

Merged
7 changes: 6 additions & 1 deletion packages/electron/src/config/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ module.exports.schema = {
},
logger: Object.assign({}, schema.logger, {
defaultValue: () => getPrefixedConsole()
})
}),
codeBundleId: {
defaultValue: () => null,
message: 'should be a string',
validate: val => (val === null || stringWithLength(val))
}
}

module.exports.mergeOptions = (mainOpts, rendererOpts) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/electron/types/notifier.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface MainConfig extends Config {

// a renderer is only allowed a subset of properties from Config
// this must match the "ALLOWED_IN_RENDERER" list in the renderer config
type AllowedRendererConfig = Pick<Config, 'onError'|'onBreadcrumb'|'logger'|'metadata'|'user'|'context'|'codeBundleId'|'plugins'|'appType'>
type AllowedRendererConfig = Pick<Config, 'onError'|'onBreadcrumb'|'logger'|'metadata'|'user'|'context'|'plugins'|'appType'>

interface RendererConfig extends AllowedRendererConfig {
codeBundleId?: string
Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-electron-app/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const native = require('bindings')('bugsnag_plugin_electron_app_bindings')
const { schema } = require('@bugsnag/core/config')

const osToAppType = new Map([
['darwin', 'macOS'],
Expand Down Expand Up @@ -51,14 +52,12 @@ module.exports = (NativeClient, process, electronApp, BrowserWindow, NativeApp =
const appStart = Math.round(process.getCreationTime() || Date.now())
let lastEnteredForeground = appStart

const version = client._config.appVersion || electronApp.getVersion()
updateApp({
inForeground: BrowserWindow.getFocusedWindow() !== null,
isLaunching: true,
releaseStage: client._config.releaseStage,
type: client._config.appType || osToAppType.get(process.platform),
version: version,
bundleVersion: NativeApp.getPackageVersion() || version
version: client._config.appVersion
})

client.addMetadata('app', {
Expand Down Expand Up @@ -134,6 +133,10 @@ module.exports = (NativeClient, process, electronApp, BrowserWindow, NativeApp =
return { markLaunchComplete }
},
configSchema: {
appVersion: {
...schema.appVersion,
defaultValue: () => NativeApp.getPackageVersion() || electronApp.getVersion() || undefined
},
launchDurationMillis: {
defaultValue: () => 5000,
message: 'should be a number ≥0',
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-electron-app/src/get_version-mac.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// if any of the intermediate values (mainBundle, infoDictionary, etc) are
// nil, the entire chain evaluates to nil
const char *value =
[[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"] UTF8String];
[[[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"] UTF8String];
if (value) {
// copy the value as the existing reference will likely be invalidated in
// the immediate future
Expand Down
27 changes: 19 additions & 8 deletions packages/plugin-electron-app/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const makeExpectedSessionApp = (customisations = {}) => ({
releaseStage: 'production',
type: undefined,
version: '1.2.3',
bundleVersion: '1.2.3',
...customisations
})

Expand Down Expand Up @@ -98,9 +97,15 @@ describe('plugin: electron app info', () => {
const process = makeProcess({ platform: 'darwin' })
const electronApp = makeElectronApp({ version: '5.4.6' })

const { sendEvent, sendSession } = makeClient({ electronApp, process })
const { sendEvent, sendSession } = makeClient({
electronApp,
process,
NativeApp: {
getPackageVersion: () => '5.4.6'
}
})

const expected = { type: 'macOS', version: '5.4.6', bundleVersion: '5.4.6' }
const expected = { type: 'macOS', version: '5.4.6' }

const event = await sendEvent()
expect(event.app).toEqual(makeExpectedEventApp(expected))
Expand All @@ -112,11 +117,17 @@ describe('plugin: electron app info', () => {

it('reports app.version and app.bundleVersion for Windows', async () => {
const process = makeProcess({ platform: 'win32' })
const electronApp = makeElectronApp({ version: '1.3.4' })
const electronApp = makeElectronApp({ version: '1.0.0' })

const { sendEvent, sendSession } = makeClient({ electronApp, process })
const { sendEvent, sendSession } = makeClient({
electronApp,
process,
NativeApp: {
getPackageVersion: () => '1.3.4'
}
})

const expected = { type: 'Windows', version: '1.3.4', bundleVersion: '1.3.4' }
const expected = { type: 'Windows', version: '1.3.4' }

const event = await sendEvent()
expect(event.app).toEqual(makeExpectedEventApp(expected))
Expand All @@ -126,13 +137,13 @@ describe('plugin: electron app info', () => {
expect(session.app).toEqual(makeExpectedSessionApp(expected))
})

it('reports app.version and app.bundleVersion for Linux', async () => {
it('reports app.version for Linux', async () => {
const process = makeProcess({ platform: 'linux' })
const electronApp = makeElectronApp({ version: '9.8.7' })

const { sendEvent, sendSession } = makeClient({ electronApp, process })

const expected = { type: 'Linux', version: '9.8.7', bundleVersion: '9.8.7' }
const expected = { type: 'Linux', version: '9.8.7' }

const event = await sendEvent()
expect(event.app).toEqual(makeExpectedEventApp(expected))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (BugsnagIpcRenderer = window.__bugsnag_ipc__) => ({

event.context = event.context || context
event.breadcrumbs = breadcrumbs
event.app = { ...event.app, ...app }
event.app = { ...event.app, ...app, codeBundleId: client._config.codeBundleId }
event.device = { ...event.device, ...device }

if (!event._user || Object.keys(event._user).length === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('plugin: electron renderer event data', () => {

expect(event.context).toBe(context)
expect(event.breadcrumbs).toStrictEqual(breadcrumbs)
expect(event.app).toStrictEqual({ ...app, releaseStage: 'production', type: undefined, version: undefined })
expect(event.app).toStrictEqual({ ...app, releaseStage: 'production', type: undefined, version: undefined, codeBundleId: undefined })
expect(event.device).toStrictEqual(device)
expect(event.getUser()).toStrictEqual(user)
expect(event.getMetadata('meta')).toStrictEqual(metadata.meta)
Expand Down
1 change: 1 addition & 0 deletions test/electron/features/renderer-config.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Feature: Setting config options in renderers
Examples:
| property | config |
| appType | { "appType": "real great" } |
| codeBundleId | { "codeBundleId": "1.0.0-r0123" } |
92 changes: 92 additions & 0 deletions test/electron/fixtures/events/renderer/config/codeBundleId.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"apiKey": "6425093c6530f554a9897d2d7d38e248",
"notifier": {
"name": "Bugsnag Electron",
"url": "https://github.com/bugsnag/bugsnag-electron",
"version": "{REGEX:^200\\.1\\.0-canary\\.[0-9a-f]{24}$}"
},
"events": [
{
"payloadVersion": "4",
"app": {
"duration": "{TYPE:number}",
"releaseStage": "production",
"inForeground": "{TYPE:boolean}",
"isLaunching": "{TYPE:boolean}",
"version": "1.0.2",
"type": "{REGEX:^Linux|macOS|Windows$}",
"codeBundleId": "1.0.0-r0123"
},
"device": {
"runtimeVersions": {
"node": "{TYPE:string}",
"chrome": "{TYPE:string}",
"electron": "{TYPE:string}"
},
"id": "{REGEX:[0-9a-f]{64}}",
"freeMemory": "{TYPE:number}",
"time": "{TIMESTAMP}",
"totalMemory": "{TYPE:number}",
"osVersion": "{REGEX:\\d+\\.\\d+}"
},
"user": {
"id": "{REGEX:[0-9a-f]{64}}"
},
"metaData": {
"app": {
"name": "Runner"
},
"device": {
"online": "{TYPE:boolean}",
"idleTime": "{TYPE:number}",
"screenResolution": {
"width": "{TYPE:number}",
"height": "{TYPE:number}"
}
}
},
"severity": "warning",
"unhandled": false,
"severityReason": {
"type": "handledException"
},
"breadcrumbs": [
{
"type": "state",
"name": "App became ready",
"timestamp": "{TIMESTAMP}"
},
{
"type": "state",
"name": "Browser window 1 created",
"timestamp": "{TIMESTAMP}",
"metaData": {
"id": 1
}
},
{
"type": "state",
"name": "Browser window 1 was shown",
"timestamp": "{TIMESTAMP}",
"metaData": {
"id": 1,
"title": "Runner"
}
}
],
"exceptions": [
{
"errorMessage": "{REGEX:ALERT!$}",
"errorClass": "Error",
"stacktrace": [
{
"lineNumber": 20,
"file": "./renderer.js"
}
],
"type": "electronrendererjs"
}
]
}
]
}
3 changes: 1 addition & 2 deletions test/electron/fixtures/events/sessions/complex-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"app": {
"releaseStage": "beta",
"version": "2.0.83-beta3",
"type": "complicated",
"bundleVersion": "{TYPE:string}"
"type": "complicated"
},
"sessions": [
{
Expand Down
3 changes: 1 addition & 2 deletions test/electron/fixtures/events/sessions/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
"app": {
"releaseStage": "production",
"version": "1.0.2",
"type": "{REGEX:^Linux|macOS|Windows$}",
"bundleVersion": "1.0.2"
"type": "{REGEX:^Linux|macOS|Windows$}"
},
"sessions": [
{
Expand Down