Skip to content

Commit d98f5d6

Browse files
authored
fix(node): Generate a Sentry Release string from env if its not provided (#3393)
* Added a function that gets a sentry release from env dynamically if release is not provided on init * Fixing PR comments * Removed redundant documentation comment * Removed redundant export statement * Fixed failing test because if no release set, it is empty string not undefined * Added logic that checks if no release can be generated from environment, then release should be undefined * Removed test that ensures initialization does not throw error if global sentry release is set
1 parent 2ee5d46 commit d98f5d6

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

packages/node/src/sdk.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,9 @@ export function init(options: NodeOptions = {}): void {
9393
}
9494

9595
if (options.release === undefined) {
96-
const global = getGlobalObject<Window>();
97-
// Prefer env var over global
98-
if (process.env.SENTRY_RELEASE) {
99-
options.release = process.env.SENTRY_RELEASE;
100-
}
101-
// This supports the variable that sentry-webpack-plugin injects
102-
else if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
103-
options.release = global.SENTRY_RELEASE.id;
96+
const detectedRelease = getSentryRelease();
97+
if (detectedRelease !== undefined) {
98+
options.release = detectedRelease;
10499
}
105100
}
106101

@@ -152,3 +147,32 @@ export async function close(timeout?: number): Promise<boolean> {
152147
}
153148
return Promise.reject(false);
154149
}
150+
151+
/**
152+
* A function that returns a Sentry release string dynamically from env variables
153+
*/
154+
function getSentryRelease(): string | undefined {
155+
// Always read first as Sentry takes this as precedence
156+
if (process.env.SENTRY_RELEASE) {
157+
return process.env.SENTRY_RELEASE;
158+
}
159+
160+
// This supports the variable that sentry-webpack-plugin injects
161+
const global = getGlobalObject();
162+
if (global.SENTRY_RELEASE && global.SENTRY_RELEASE.id) {
163+
return global.SENTRY_RELEASE.id;
164+
}
165+
166+
return (
167+
// GitHub Actions - https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
168+
process.env.GITHUB_SHA ||
169+
// Netlify - https://docs.netlify.com/configure-builds/environment-variables/#build-metadata
170+
process.env.COMMIT_REF ||
171+
// Vercel - https://vercel.com/docs/v2/build-step#system-environment-variables
172+
process.env.VERCEL_GIT_COMMIT_SHA ||
173+
// Zeit (now known as Vercel)
174+
process.env.ZEIT_GITHUB_COMMIT_SHA ||
175+
process.env.ZEIT_GITLAB_COMMIT_SHA ||
176+
process.env.ZEIT_BITBUCKET_COMMIT_SHA
177+
);
178+
}

packages/node/test/index.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@ describe('SentryNode initialization', () => {
278278
// Unsure if this is needed under jest.
279279
global.SENTRY_RELEASE = undefined;
280280
});
281-
test('initialization proceeds as normal if global.SENTRY_RELEASE is not set', () => {
282-
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
283-
init({ dsn });
284-
expect(global.__SENTRY__.hub._stack[0].client.getOptions().release).toBeUndefined();
285-
});
286281

287282
describe('SDK metadata', () => {
288283
it('should set SDK data when Sentry.init() is called', () => {

0 commit comments

Comments
 (0)