Skip to content

Commit

Permalink
Merge pull request #20115 from storybookjs/tom/sb-980-normalize-http-…
Browse files Browse the repository at this point in the history
…vs-ssh-git-urls-for

Deal with a lot of different edge cases for git URLs
  • Loading branch information
tmeasday authored Dec 7, 2022
2 parents dd72a50 + 7fd5ec2 commit b88b9c5
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
75 changes: 75 additions & 0 deletions code/lib/telemetry/src/anonymous-id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { normalizeGitUrl } from './anonymous-id';

describe('normalizeGitUrl', () => {
it('trims off https://', () => {
expect(normalizeGitUrl('https://github.com/storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off http://', () => {
expect(normalizeGitUrl('http://github.com/storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off git+https://', () => {
expect(normalizeGitUrl('git+https://github.com/storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off https://username@', () => {
expect(normalizeGitUrl('https://[email protected]/storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off http://username@', () => {
expect(normalizeGitUrl('http://[email protected]/storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off https://username:password@', () => {
expect(
normalizeGitUrl('https://username:[email protected]/storybookjs/storybook.git')
).toEqual('github.com/storybookjs/storybook.git');
});

it('trims off http://username:password@', () => {
expect(
normalizeGitUrl('http://username:[email protected]/storybookjs/storybook.git')
).toEqual('github.com/storybookjs/storybook.git');
});

it('trims off git://', () => {
expect(normalizeGitUrl('git://github.com/storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off git@', () => {
expect(normalizeGitUrl('[email protected]:storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off git+ssh://git@', () => {
expect(normalizeGitUrl('git+ssh://[email protected]:storybookjs/storybook.git')).toEqual(
'github.com/storybookjs/storybook.git'
);
});

it('trims off ssh://git@', () => {
expect(normalizeGitUrl('ssh://[email protected]:storybookjs/storybook.git')).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'
);
});
});
16 changes: 15 additions & 1 deletion code/lib/telemetry/src/anonymous-id.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import path from 'path';
import { execSync } from 'child_process';
import { getProjectRoot } from '@storybook/core-common';

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

export function normalizeGitUrl(rawUrl: string) {
// I don't *think* its possible to set a hash on a origin URL, but just in case
const urlWithoutHash = rawUrl.replace(/#.*$/, '');

// Strip anything ahead of an @
const urlWithoutUser = urlWithoutHash.replace(/^.*@/, '');

// Now strip off scheme
const urlWithoutScheme = urlWithoutUser.replace(/^.*\/\//, '');

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

let anonymousProjectId: string;
export const getAnonymousProjectId = () => {
if (anonymousProjectId) {
Expand All @@ -22,7 +36,7 @@ export const getAnonymousProjectId = () => {

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

anonymousProjectId = oneWayHash(unhashedProjectId);
} catch (_) {
Expand Down

0 comments on commit b88b9c5

Please sign in to comment.