Skip to content

Commit a44dcb8

Browse files
tstirrat15HazAT
authored andcommitted
Support sentry-webpack-plugin release injection in browser and node SDKs (getsentry#2132)
* Add clarification in the contributing.md doc * Add release setting via global var
1 parent 5bfb758 commit a44dcb8

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

CONTRIBUTING.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ in the project root.
4646

4747
## Running the Test Suite
4848

49-
You can run all test at once by calling `yarn test` in the project root or in individual sub packages.
49+
You can run all test at once by calling `yarn test` in the project root or in individual sub packages. Note that you must run `yarn build` before the test command will work.
5050

5151
## Lint
5252

53-
You can run all test at once by calling `yarn lint` in the project root or in individual sub packages.
53+
You can run all test at once by calling `yarn lint` in the project root or in individual sub packages. Note that you must run `yarn build` before the lint command will work.
5454

5555
## Contributing Back Code
5656

packages/browser/src/sdk.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
2+
import { getGlobalObject } from '@sentry/utils';
23

34
import { BrowserOptions } from './backend';
45
import { BrowserClient, ReportDialogOptions } from './client';
@@ -76,6 +77,13 @@ export function init(options: BrowserOptions = {}): void {
7677
if (options.defaultIntegrations === undefined) {
7778
options.defaultIntegrations = defaultIntegrations;
7879
}
80+
if (options.release === undefined) {
81+
const window = getGlobalObject<Window>() as any;
82+
// This supports the variable that sentry-webpack-plugin injects
83+
if (window.SENTRY_RELEASE && window.SENTRY_RELEASE.id) {
84+
options.release = window.SENTRY_RELEASE.id;
85+
}
86+
}
7987
initAndBind(BrowserClient, options);
8088
}
8189

packages/browser/test/index.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,19 @@ describe('SentryBrowser', () => {
166166
});
167167
});
168168
});
169+
170+
171+
describe('SentryBrowser initialization', () => {
172+
it('should use window.SENTRY_RELEASE to set release on initialization if available', () => {
173+
global.SENTRY_RELEASE = { id: 'foobar' };
174+
init({ dsn });
175+
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).to.equal('foobar');
176+
// Manually tear down global set. Is there a nicer way to do this?
177+
global.SENTRY_RELEASE = undefined;
178+
});
179+
it('should have initialization proceed as normal if window.SENTRY_RELEASE is not set', () => {
180+
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
181+
init({ dsn });
182+
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).to.be.undefined;
183+
});
184+
})

packages/node/src/sdk.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getCurrentHub, initAndBind, Integrations as CoreIntegrations } from '@sentry/core';
22
import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
3+
import { getGlobalObject } from '@sentry/utils';
34
import * as domain from 'domain';
45

56
import { NodeOptions } from './backend';
@@ -84,8 +85,16 @@ export function init(options: NodeOptions = {}): void {
8485
options.dsn = process.env.SENTRY_DSN;
8586
}
8687

87-
if (options.release === undefined && process.env.SENTRY_RELEASE) {
88-
options.release = process.env.SENTRY_RELEASE;
88+
if (options.release === undefined) {
89+
const global = getGlobalObject<Window>() as any;
90+
// Prefer env var over global
91+
if (process.env.SENTRY_RELEASE) {
92+
options.release = process.env.SENTRY_RELEASE;
93+
}
94+
// This supports the variable that sentry-webpack-plugin injects
95+
else if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
96+
options.release = global.SENTRY_RELEASE.id;
97+
}
8998
}
9099

91100
if (options.environment === undefined && process.env.SENTRY_ENVIRONMENT) {

packages/node/test/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,18 @@ describe('SentryNode', () => {
238238
});
239239
});
240240
});
241+
242+
describe('SentryNode initialization', () => {
243+
test('global.SENTRY_RELEASE is used to set release on initialization if available', () => {
244+
global.SENTRY_RELEASE = { id: 'foobar' };
245+
init({ dsn });
246+
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toEqual('foobar');
247+
// Unsure if this is needed under jest.
248+
global.SENTRY_RELEASE = undefined;
249+
});
250+
test('initialization proceeds as normal if global.SENTRY_RELEASE is not set', () => {
251+
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
252+
init({ dsn });
253+
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined();
254+
});
255+
})

0 commit comments

Comments
 (0)