Skip to content

Commit 28c8e19

Browse files
committed
Normalizes machineId across VS Code clones
1 parent 02ea44d commit 28c8e19

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

src/env/browser/machine.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This is intentional as the browser environment doesn't have access to MAC addresses
2+
export function getMac(): string | undefined {
3+
return undefined;
4+
}

src/env/node/machine.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { networkInterfaces } from 'os';
2+
3+
// Sourced from https://github.com/bevry/getmac/blob/master/source/index.ts
4+
// There's issues with importing 'getmac' directly, so we referenced the relevant code here
5+
6+
const zeroRegex = /(?:[0]{1,2}[:-]){5}[0]{1,2}/;
7+
export function getMac(): string | undefined {
8+
const list = networkInterfaces();
9+
10+
for (const parts of Object.values(list)) {
11+
// for some reason beyond me, this is needed to satisfy typescript
12+
// fix https://github.com/bevry/getmac/issues/100
13+
if (!parts) continue;
14+
for (const part of parts) {
15+
if (zeroRegex.test(part.mac) === false) {
16+
return part.mac;
17+
}
18+
}
19+
}
20+
return undefined;
21+
}

src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { registerPartnerActionRunners } from './partners';
2323
import { executeCommand, registerCommands } from './system/-webview/command';
2424
import { configuration, Configuration } from './system/-webview/configuration';
2525
import { setContext } from './system/-webview/context';
26+
import { getMachineId } from './system/-webview/machine';
2627
import { Storage } from './system/-webview/storage';
2728
import { deviceCohortGroup } from './system/-webview/vscode';
2829
import { isTextDocument } from './system/-webview/vscode/documents';
@@ -56,7 +57,7 @@ export async function activate(context: ExtensionContext): Promise<GitLensApi |
5657
env.appName
5758
} (${codeVersion}) on the ${isWeb ? 'web' : 'desktop'}; language='${
5859
env.language
59-
}', logLevel='${logLevel}', defaultDateLocale='${defaultDateLocale}' (${env.machineId}|${
60+
}', logLevel='${logLevel}', defaultDateLocale='${defaultDateLocale}' (${getMachineId()}|${
6061
env.sessionId
6162
})`,
6263
);
@@ -110,7 +111,7 @@ export async function activate(context: ExtensionContext): Promise<GitLensApi |
110111
log: {
111112
message: ` activating in ${env.appName} (${codeVersion}) on the ${isWeb ? 'web' : 'desktop'}; language='${
112113
env.language
113-
}', logLevel='${logLevel}', defaultDateLocale='${defaultDateLocale}' (${env.uriScheme}|${env.machineId}|${
114+
}', logLevel='${logLevel}', defaultDateLocale='${defaultDateLocale}' (${env.uriScheme}|${getMachineId()}|${
114115
env.sessionId
115116
})`,
116117
//${context.extensionRuntime !== ExtensionRuntime.Node ? ' in a webworker' : ''}

src/plus/gk/subscriptionService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import type { RepositoriesChangeEvent } from '../../git/gitProviderService';
4848
import { executeCommand, registerCommand } from '../../system/-webview/command';
4949
import { configuration } from '../../system/-webview/configuration';
5050
import { setContext } from '../../system/-webview/context';
51+
import { getMachineId } from '../../system/-webview/machine';
5152
import { openUrl } from '../../system/-webview/vscode/uris';
5253
import { createFromDateDelta, fromNow } from '../../system/date';
5354
import { gate } from '../../system/decorators/-webview/gate';
@@ -1025,7 +1026,7 @@ export class SubscriptionService implements Disposable {
10251026
id: session.account.id,
10261027
platform: getPlatform(),
10271028
gitlensVersion: this.container.version,
1028-
machineId: env.machineId,
1029+
machineId: getMachineId(),
10291030
sessionId: env.sessionId,
10301031
vscodeEdition: env.appName,
10311032
vscodeHost: env.appHost,

src/system/-webview/machine.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { env } from 'vscode';
2+
import { getMac } from '@env/machine';
3+
import { isWeb } from '@env/platform';
4+
5+
export function getMachineId(): string {
6+
if (isWeb) {
7+
return env.machineId;
8+
}
9+
return getMac() ?? env.machineId;
10+
}

src/system/-webview/vscode.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import { getDistributionGroup } from '../string';
88
import { satisfies } from '../version';
99
import { executeCoreCommand } from './command';
1010
import { configuration } from './configuration';
11+
import { getMachineId } from './machine';
1112
import { exists } from './vscode/uris';
1213

13-
export const deviceCohortGroup = getDistributionGroup(env.machineId);
14+
export const deviceCohortGroup = getDistributionGroup(getMachineId());
1415

1516
let _hostExecutablePath: string | undefined;
1617
export async function getHostExecutablePath(): Promise<string> {

src/telemetry/telemetry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getPlatform } from '@env/platform';
66
import type { Source, TelemetryEventData, TelemetryEvents, TelemetryGlobalContext } from '../constants.telemetry';
77
import type { Container } from '../container';
88
import { configuration } from '../system/-webview/configuration';
9+
import { getMachineId } from '../system/-webview/machine';
910

1011
export interface TelemetryContext {
1112
env: string;
@@ -99,7 +100,7 @@ export class TelemetryService implements Disposable {
99100
env: container.env,
100101
extensionId: container.id,
101102
extensionVersion: container.version,
102-
machineId: env.machineId,
103+
machineId: getMachineId(),
103104
sessionId: env.sessionId,
104105
language: env.language,
105106
platform: getPlatform(),

0 commit comments

Comments
 (0)