Skip to content

Commit

Permalink
Merge pull request #29736 from storybookjs/tmeasday/anonymous-id-tweaks
Browse files Browse the repository at this point in the history
Telemetry: Improve anonymous id calculation
  • Loading branch information
shilman authored Dec 2, 2024
2 parents 35afa71 + 1cc488e commit 8bab9e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
22 changes: 21 additions & 1 deletion code/core/src/telemetry/anonymous-id.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { normalizeGitUrl } from './anonymous-id';
import { normalizeGitUrl, unhashedProjectId } from './anonymous-id';

describe('normalizeGitUrl', () => {
it('trims off https://', () => {
Expand Down Expand Up @@ -69,6 +69,12 @@ describe('normalizeGitUrl', () => {
);
});

it('adds .git if missing', () => {
expect(normalizeGitUrl('https://github.com/storybookjs/storybook')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off #hash', () => {
expect(normalizeGitUrl('https://github.com/storybookjs/storybook.git#next')).toEqual(
'github.com/storybookjs/storybook.git'
Expand All @@ -85,3 +91,17 @@ describe('normalizeGitUrl', () => {
);
});
});

describe('unhashedProjectId', () => {
it('does not touch unix paths', () => {
expect(
unhashedProjectId('https://github.com/storybookjs/storybook.git\n', 'path/to/storybook')
).toBe('github.com/storybookjs/storybook.gitpath/to/storybook');
});

it('normalizes windows paths', () => {
expect(
unhashedProjectId('https://github.com/storybookjs/storybook.git\n', 'path\\to\\storybook')
).toBe('github.com/storybookjs/storybook.gitpath/to/storybook');
});
});
21 changes: 14 additions & 7 deletions code/core/src/telemetry/anonymous-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { relative } from 'node:path';
import { getProjectRoot } from '@storybook/core/common';

import { execSync } from 'child_process';
import slash from 'slash';

import { oneWayHash } from './one-way-hash';

Expand All @@ -16,7 +17,18 @@ export function normalizeGitUrl(rawUrl: string) {
// Now strip off scheme
const urlWithoutScheme = urlWithoutUser.replace(/^.*\/\//, '');

return urlWithoutScheme.replace(':', '/');
// Ensure the URL ends in `.git`
const urlWithExtension = urlWithoutScheme.endsWith('.git')
? urlWithoutScheme
: `${urlWithoutScheme}.git`;

return urlWithExtension.replace(':', '/');
}

// we use a combination of remoteUrl and working directory
// to separate multiple storybooks from the same project (e.g. monorepo)
export function unhashedProjectId(remoteUrl: string, projectRootPath: string) {
return `${normalizeGitUrl(remoteUrl)}${slash(projectRootPath)}`;
}

let anonymousProjectId: string;
Expand All @@ -25,7 +37,6 @@ export const getAnonymousProjectId = () => {
return anonymousProjectId;
}

let unhashedProjectId;
try {
const projectRoot = getProjectRoot();

Expand All @@ -36,11 +47,7 @@ export const getAnonymousProjectId = () => {
stdio: `pipe`,
});

// we use a combination of remoteUrl and working directory
// to separate multiple storybooks from the same project (e.g. monorepo)
unhashedProjectId = `${normalizeGitUrl(String(originBuffer))}${projectRootPath}`;

anonymousProjectId = oneWayHash(unhashedProjectId);
anonymousProjectId = oneWayHash(unhashedProjectId(String(originBuffer), projectRootPath));
} catch (_) {
//
}
Expand Down

0 comments on commit 8bab9e1

Please sign in to comment.