Skip to content

Commit e1038db

Browse files
committed
refactor: extract getActiveEnvVarName() shared helper in db/auth.ts
Replace the duplicated 3-line env var name extraction pattern in login, logout, and refresh commands with a single getActiveEnvVarName() helper exported from db/auth.ts. Addresses BugBot feedback about code duplication across commands.
1 parent aa85330 commit e1038db

File tree

5 files changed

+45
-17
lines changed

5 files changed

+45
-17
lines changed

src/commands/auth/login.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { getCurrentUser, getUserRegions } from "../../lib/api-client.js";
33
import { buildCommand, numberParser } from "../../lib/command.js";
44
import {
55
clearAuth,
6-
ENV_SOURCE_PREFIX,
7-
getAuthConfig,
6+
getActiveEnvVarName,
87
isAuthenticated,
98
isEnvTokenActive,
109
setAuthToken,
@@ -52,10 +51,7 @@ export const loginCommand = buildCommand({
5251
// Check if already authenticated
5352
if (await isAuthenticated()) {
5453
if (isEnvTokenActive()) {
55-
const config = getAuthConfig();
56-
const envVar = config?.source.startsWith(ENV_SOURCE_PREFIX)
57-
? config.source.slice(ENV_SOURCE_PREFIX.length)
58-
: "SENTRY_AUTH_TOKEN";
54+
const envVar = getActiveEnvVarName();
5955
stdout.write(
6056
`Authentication is provided via ${envVar} environment variable. ` +
6157
`Unset ${envVar} to use OAuth-based login instead.\n`

src/commands/auth/logout.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import type { SentryContext } from "../../context.js";
88
import { buildCommand } from "../../lib/command.js";
99
import {
1010
clearAuth,
11-
ENV_SOURCE_PREFIX,
12-
getAuthConfig,
11+
getActiveEnvVarName,
1312
isAuthenticated,
1413
isEnvTokenActive,
1514
} from "../../lib/db/auth.js";
@@ -34,10 +33,7 @@ export const logoutCommand = buildCommand({
3433
}
3534

3635
if (isEnvTokenActive()) {
37-
const config = getAuthConfig();
38-
const envVar = config?.source.startsWith(ENV_SOURCE_PREFIX)
39-
? config.source.slice(ENV_SOURCE_PREFIX.length)
40-
: "SENTRY_AUTH_TOKEN";
36+
const envVar = getActiveEnvVarName();
4137
stdout.write(
4238
`Authentication is provided via ${envVar} environment variable.\n` +
4339
`Unset ${envVar} to log out.\n`

src/commands/auth/refresh.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import type { SentryContext } from "../../context.js";
88
import { buildCommand } from "../../lib/command.js";
99
import {
10-
ENV_SOURCE_PREFIX,
10+
getActiveEnvVarName,
1111
getAuthConfig,
1212
isEnvTokenActive,
1313
refreshToken,
@@ -69,10 +69,7 @@ Examples:
6969

7070
// Env var tokens can't be refreshed
7171
if (isEnvTokenActive()) {
72-
const config = getAuthConfig();
73-
const envVar = config?.source.startsWith(ENV_SOURCE_PREFIX)
74-
? config.source.slice(ENV_SOURCE_PREFIX.length)
75-
: "SENTRY_AUTH_TOKEN";
72+
const envVar = getActiveEnvVarName();
7673
throw new AuthError(
7774
"invalid",
7875
"Cannot refresh an environment variable token.\n" +

src/lib/db/auth.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ export function isEnvTokenActive(): boolean {
5959
return getEnvToken() !== undefined;
6060
}
6161

62+
/**
63+
* Get the name of the active env var providing authentication.
64+
* Returns the specific variable name (e.g. "SENTRY_AUTH_TOKEN" or "SENTRY_TOKEN").
65+
*
66+
* **Important**: Call only after checking {@link isEnvTokenActive} returns true.
67+
* Falls back to "SENTRY_AUTH_TOKEN" if no env source is active, which is a safe
68+
* default for error messages but may be misleading if used unconditionally.
69+
*/
70+
export function getActiveEnvVarName(): string {
71+
const config = getAuthConfig();
72+
if (config?.source.startsWith(ENV_SOURCE_PREFIX)) {
73+
return config.source.slice(ENV_SOURCE_PREFIX.length);
74+
}
75+
return "SENTRY_AUTH_TOKEN";
76+
}
77+
6278
export function getAuthConfig(): AuthConfig | undefined {
6379
const envToken = getEnvToken();
6480
if (envToken) {

test/lib/db/auth.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
99
import {
1010
type AuthSource,
11+
getActiveEnvVarName,
1112
getAuthConfig,
1213
getAuthToken,
1314
isAuthenticated,
@@ -155,6 +156,28 @@ describe("env var auth: isEnvTokenActive", () => {
155156
});
156157
});
157158

159+
describe("env var auth: getActiveEnvVarName", () => {
160+
test("returns SENTRY_AUTH_TOKEN when that var is set", () => {
161+
process.env.SENTRY_AUTH_TOKEN = "test_token";
162+
expect(getActiveEnvVarName()).toBe("SENTRY_AUTH_TOKEN");
163+
});
164+
165+
test("returns SENTRY_TOKEN when only that var is set", () => {
166+
process.env.SENTRY_TOKEN = "test_token";
167+
expect(getActiveEnvVarName()).toBe("SENTRY_TOKEN");
168+
});
169+
170+
test("prefers SENTRY_AUTH_TOKEN when both are set", () => {
171+
process.env.SENTRY_AUTH_TOKEN = "primary";
172+
process.env.SENTRY_TOKEN = "secondary";
173+
expect(getActiveEnvVarName()).toBe("SENTRY_AUTH_TOKEN");
174+
});
175+
176+
test("falls back to SENTRY_AUTH_TOKEN when no env var is set", () => {
177+
expect(getActiveEnvVarName()).toBe("SENTRY_AUTH_TOKEN");
178+
});
179+
});
180+
158181
describe("env var auth: refreshToken", () => {
159182
test("returns env token without refreshing", async () => {
160183
process.env.SENTRY_AUTH_TOKEN = "env_token";

0 commit comments

Comments
 (0)